设计模式-工厂模式
-
简单工厂
简单工厂起始不是'设计模式',只是人们一直错误的以为他是设计模式。是不是真正的设计模式姑且不论,但是它的作用确实不小。下面我们来剖析一下。
UML图:
![]()
1 public abstract class Product { 2 3 4 5 protected String mDesctription = null; 6 7 8 9 public String getDesctription(){ 10 11 return mDesctription; 12 13 } 14 15 16 17 public abstract void doSomething(); 18 19 20 21 } 22 23 24 25 public class ConcreteProduct1 extends Product{ 26 27 28 29 public ConcreteProduct1() { 30 31 mDesctription = "concreteProduct 1... "; 32 33 } 34 35 36 37 @Override 38 39 public void doSomething() { 40 41 // TODO Auto-generated method stub 42 43 System.out.println("concreteProduct 1... do something!"); 44 45 } 46 47 48 49 } 50 51 public class ConcreteProduct2 extends Product{ 52 53 54 55 public ConcreteProduct2() { 56 57 mDesctription = "concreteProduct 2... "; 58 59 } 60 61 62 63 @Override 64 65 public void doSomething() { 66 67 // TODO Auto-generated method stub 68 69 System.out.println("concreteProduct 2... do something!"); 70 71 } 72 73 74 75 } 76 77 public class SimpleFactory { 78 79 80 81 private static volatile SimpleFactory mFactory = null; 82 83 84 85 private SimpleFactory() { 86 87 88 89 } 90 91 92 93 public static SimpleFactory getInstance() { 94 95 if (mFactory == null) { 96 97 synchronized (SimpleFactory.class) { 98 99 mFactory = new SimpleFactory(); 100 101 } 102 103 } 104 105 return mFactory; 106 107 } 108 109 110 111 public Product getProduct(Type type) { 112 113 switch (type) { 114 115 case P1: 116 117 return new ConcreteProduct1(); 118 119 case P2: 120 121 return new ConcreteProduct2(); 122 123 default: 124 125 break; 126 127 } 128 129 return null; 130 131 } 132 133 134 135 enum Type { 136 137 P1, P2; 138 139 } 140 141 } 142 143 public class Main { 144 145 146 147 public static void main(String[] args) { 148 149 Product p1 = SimpleFactory.getInstance().getProduct(Type.P1); 150 151 Product p2 = SimpleFactory.getInstance().getProduct(Type.P2); 152 153 System.out.println("p1 = " + p1.getDesctription()); 154 155 System.out.println("p2 = " + p2.getDesctription()); 156 157 158 159 p1.doSomething(); 160 161 p2.doSomething(); 162 163 } 164 165 }
-
工厂方法
定义:定义了一个创建对象的接口,但是由子类决定要实例化的类是哪一个。工厂方法让类把实例化推迟到子类。
UML图:
![]()
1 public abstract class Creator { 2 3 4 5 public void work(){ 6 7 Product p = createProduct(); 8 9 p.doSomething(); 10 11 } 12 13 14 15 public abstract Product createProduct(); 16 17 } 18 19 public class ConcreteCreator1 extends Creator{ 20 21 22 23 @Override 24 25 public Product createProduct() { 26 27 // TODO Auto-generated method stub 28 29 return new ConcreteProduct1(); 30 31 } 32 33 34 35 } 36 37 public class Main { 38 39 40 41 public static void main(String[] args) { 42 43 Creator creator1 = new ConcreteCreator1(); 44 45 Creator creator2 = new ConcreteCreator2(); 46 47 creator1.work(); 48 49 creator2.work(); 50 51 } 52 53 }
-
抽象工厂
定义:提供一个接口,用于创建相关或依赖对象的家族,而不需要明确指定具体类。
UML 图:
![]()
1 public interface AbstractFactory { 2 3 4 5 public ProductA getProductA(); 6 7 8 9 public ProductB getProductB(); 10 11 } 12 13 public class AFactory implements AbstractFactory{ 14 15 16 17 @Override 18 19 public ProductA getProductA() { 20 21 // TODO Auto-generated method stub 22 23 return new ConcreteProductA(); 24 25 } 26 27 28 29 @Override 30 31 public ProductB getProductB() { 32 33 // TODO Auto-generated method stub 34 35 return new ConcreteProductB(); 36 37 } 38 39 40 41 } 42 43 public class BFactory implements AbstractFactory{ 44 45 46 47 @Override 48 49 public ProductA getProductA() { 50 51 // TODO Auto-generated method stub 52 53 return new ConcreteProductA2(); 54 55 } 56 57 58 59 @Override 60 61 public ProductB getProductB() { 62 63 // TODO Auto-generated method stub 64 65 return new ConcreteProductB2(); 66 67 } 68 69 70 71 } 72 73 public interface ProductA { 74 75 76 77 public void descritption(); 78 79 } 80 81 public interface ProductB { 82 83 84 85 public void detail(); 86 87 } 88 89 public class ConcreteProductA implements ProductA{ 90 91 92 93 @Override 94 95 public void descritption() { 96 97 // TODO Auto-generated method stub 98 99 System.out.println("i make a concreteProductA Product."); 100 101 } 102 103 104 105 } 106 107 public class ConcreteProductB implements ProductB{ 108 109 110 111 @Override 112 113 public void detail() { 114 115 // TODO Auto-generated method stub 116 117 System.out.println("getting concreteProductB detail."); 118 119 }}



浙公网安备 33010602011771号