.net core 傳送郵件
阿新 • • 發佈:2020-08-11
工廠方法模式(Factory Method),定義一個建立物件的介面,讓子類決定例項化哪一個類。工廠方法使一個類的實列化延遲到其子類。
示意圖程式碼如下:
1 public abstract class Product { 2 public abstract void show(); 3 }Product
1 public class ConcreteProduct extends Product { 2 3 @Override 4 public void show() { 5 System.out.println("ConcreteProduct...show....");ConcreteProduct6 7 } 8 9 }
1 public abstract class Creator { 2 public abstract Product factoryMethod(); 3 }Creator
1 public class ConcreteCreator extends Creator { 2 3 @Override 4 public ConcreteProduct factoryMethod() { 5 return new ConcreteProduct(); 6 7ConcreteCreator} 8 9 }
1 public class TestFactory { 2 public static void main(String[] args) { 3 Creator c = new ConcreteCreator(); 4 ConcreteProduct pro = (ConcreteProduct) c.factoryMethod(); 5 pro.show(); 6 } 7 }test