1.工厂方法模式。定义一个用于创建对象的接口,让子类决定实例化哪一个类。工厂方法是一个类的实例化延迟到其子类。
工厂方法模式类结构图:
实现:
<?php //抽象产品类 abstract class AbstractProduct{ //共有的方法 public function common(){ } public abstract function behavior(); } //具体产品类 class ConcreteProduct1 extends AbstractProduct{ public function behavior(){ echo "doing what product1 should do!\n"; } } class ConcreteProduct2 extends AbstractProduct{ public function behavior(){ echo "I just doing what product2 should do!\n"; } } //抽象工厂类 abstract class AbstractCreator{ public abstract function createProduct($productname); } //具体工厂类 class ConcreteCreate extends AbstractCreator{ public function createProduct($productname){ try{ $product = new ReflectionClass($productname); $instance = $product->newInstanceArgs(); //$instance->behavior(); return $instance; }catch(LogicException $e){ die("Not gonna make it in here...\n"); }catch(ReflectionException $e){ die("Your class does not exist!\n"); } } } //场景类 class Client{ public function gene(){ $creator = new ConcreteCreate(); $product1 = $creator->createProduct("ConcreteProduct1"); $product1->behavior(); $product2 = $creator->createProduct("ConcreteProduct2"); $product2->behavior(); } } Client::gene();
2.简单工厂模式。又叫做静态工厂方法(Static Factory Method)模式。简单工厂模式是由一个工厂对象决定创建出哪一种产品类的实例。
简单工厂模式类结构图:
实现代码,只在工厂方法模式做小的修改即可。
<?php //抽象产品类 abstract class AbstractProduct{ //共有的方法 public function common(){ } public abstract function behavior(); } //具体产品类 class ConcreteProduct1 extends AbstractProduct{ public function behavior(){ echo "doing what product1 should do!\n"; } } class ConcreteProduct2 extends AbstractProduct{ public function behavior(){ echo "I just doing what product2 should do!\n"; } } //简单工厂类 class simpleCreator{ public function createProduct($productname){ try{ $product = new ReflectionClass($productname); $instance = $product->newInstanceArgs(); //$instance->behavior(); return $instance; }catch(LogicException $e){ die("Not gonna make it in here...\n"); }catch(ReflectionException $e){ die("Your class does not exist!\n"); } } } //场景类 class Client{ public function gene(){ $creator = new simpleCreator(); $product1 = $creator->createProduct("ConcreteProduct1"); $product1->behavior(); $product2 = $creator->createProduct("ConcreteProduct2"); $product2->behavior(); } } Client::gene();
3.抽象工厂模式。为创建一组相关或相互依赖的对象提供一个接口,而且无须指定它们的具体类。
php代码实现:
<?php //抽象产品类A abstract class AbstractProductA{ //共有的方法 public function common(){ } public abstract function behavior(); } //具体产品类A class ConcreteProductA1 extends AbstractProductA{ public function behavior(){ echo "doing what product1 should do!\n"; } } class ConcreteProductA2 extends AbstractProductA{ public function behavior(){ echo "I just doing what product2 should do!\n"; } } //抽象产品类B abstract class AbstractProductB{ //共有的方法 public function common(){ } public abstract function bdosomething(); } //具体产品类B class ConcreteProductB1 extends AbstractProductB{ public function bdosomething(){ echo "b....1!\n"; } } class ConcreteProductB2 extends AbstractProductB{ public function bdosomething(){ echo "b......2!\n"; } } //抽象工厂类 abstract class AbstractCreator{ public abstract function createProductA(); public abstract function createProductB(); } //具体工厂类 class ConcreteCreator1 extends AbstractCreator{ public function createProductA(){ return (new ConcreteProductA1()); } public function createProductB(){ return (new ConcreteProductB1()); } } class ConcreteCreator2 extends AbstractCreator{ public function createProductA(){ return (new ConcreteProductA2()); } public function createProductB(){ return (new ConcreteProductB2()); } } //场景类 class Client{ public function gene(){ $creator1 = new ConcreteCreator1(); $productA1 = $creator1->createProductA(); $productB1 = $creator1->createProductB(); $creator2 = new ConcreteCreator2(); $productA2 = $creator2->createProductA(); $productB2 = $creator2->createProductB(); $productB2->bdosomething(); } } Client::gene();
时间: 2024-10-27 13:12:53