阅读《设计模式之禅》后的总结。
前言 之前的工厂模式有看过,但是没过多久就会忘,主要还是自己平常写代码业务逻辑的时候缺少思考,可能也就会用个单例模式。然后网上很多有关设计模式的介绍,感觉有些还是介绍的不够具体,或者说只介绍到了工厂模式的一部分吧。
工厂方法模式
工厂方法模式的定义:Define an interface for ceating an object ,but let subclasses decide which class to instantiate.Factory Method lets a class defer instantiation to subclasses(定义一个用于创建对象的接口,让子类决定实例化哪一个类。工厂方法使一个类的实例化延迟到器子类。)
1 2 3 4 5 6 7 8 interface Product { public function method1 () ; public function method2 () ; }
1 2 3 4 5 6 7 8 9 10 11 12 public class ConcreteProduct1 implements Product { public function method1 () { } public function method2 () { }; }
1 2 3 4 5 6 interface Creator { public creatorProduct(); }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 public class ConcreteCreator implements Creator { public function creatorProduct ($product) { try { $productObj = new $product; }catch (Exception $e){ } return $productObj; } }
1 2 3 4 5 6 7 8 public class Client { public function main () { $fc = new ConcreteCreator(); $cp1 = $fc->creatorProduct("ConcreteProduct1" ); $cp1->method1(); } }
简单工厂模式
简单工厂模式又称静态工厂模式
就是把上面的方法ConcreteCreator()改为静态就可以了。 优点很明显,就是不用实例化工厂类直接调用工厂方法就可以了。
抽象工厂模式
优点
缺点
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 <?php interface AbstractFactory { public function createProductA () ; public function createProductB () ; } class ConcreteFactory1 implements AbstractFactory { public function createProductA () { return new ProductA1(); } public function createProductB () { return new ProductB1(); } } class ConcreteFactory2 implements AbstractFactory { public function createProductA () { return new ProductA2(); } public function createProductB () { return new ProductB2(); } } interface AbstractProductA { public function getName () ; } interface AbstractProductB { public function getName () ; } class ProductA1 implements AbstractProductA { private $_name; public function __construct () { $this ->_name = 'product A1' ; } public function getName () { return $this ->_name; } } class ProductA2 implements AbstractProductA { private $_name; public function __construct () { $this ->_name = 'product A2' ; } public function getName () { return $this ->_name; } } class ProductB1 implements AbstractProductB { private $_name; public function __construct () { $this ->_name = 'product B1' ; } public function getName () { return $this ->_name; } } class ProductB2 implements AbstractProductB { private $_name; public function __construct () { $this ->_name = 'product B2' ; } public function getName () { return $this ->_name; } } class Client { public static function main () { self ::run(new ConcreteFactory1()); self ::run(new ConcreteFactory2()); } public static function run (AbstractFactory $factory) { $productA = $factory->createProductA(); $productB = $factory->createProductB(); echo $productA->getName(), '<br />' ; echo $productB->getName(), '<br />' ; } } Client::main(); ?>