【java设计模式】【创建模式Creational Pattern】工厂方法模式Factory Method Pattern(多态性工厂模式Polymorphic Factory Pattern、虚拟构造子模式Virtual Constructor Pattern)

 1 public class Test {
 2     public static void main(String[] args){
 3         Creator ca=new ConcreteCreatorA();
 4         ca.create().doSth();
 5         Creator cb=new ConcreteCreatorB();
 6         cb.create().doSth();
 7     }
 8 }
 9 interface Creator{
10     Product create();
11 }
12 class ConcreteCreatorA implements Creator{
13     @Override
14     public Product create() {
15         return new ConcreteProductA();
16     }
17 }
18 class ConcreteCreatorB implements Creator{
19     @Override
20     public Product create() {
21         return new ConcreteProductB();
22     }
23 }
24 interface Product{
25     void doSth();
26 }
27 class ConcreteProductA implements Product{
28     
29     public ConcreteProductA(){
30         System.out.println("Product A has been created.");
31     }
32     @Override
33     public void doSth() {
34         System.out.println("Product A is working……");
35     }
36 }
37 class ConcreteProductB implements Product{
38     public ConcreteProductB(){
39         System.out.println("Product B has been created.");
40     }
41     @Override
42     public void doSth() {
43         System.out.println("Product B is working……");
44     }
45 }
原文地址:https://www.cnblogs.com/xiongjiawei/p/6836688.html