Home  >  Article  >  php教程  >  php设计模式 — 抽象工厂模式

php设计模式 — 抽象工厂模式

WBOY
WBOYOriginal
2016-06-21 08:47:37918browse

在什么情况下应当使用抽象工厂模式:

1、一个系统不应当依赖于产品类实例如何被创建、组合和表达的细节,这对于所有的形态的工厂模式都是重要的。

2、这个系统的产品有多余一个的产品族,而系统只消费其中某一个族的产品。

3、同属于同一个产品族的产品是在一起使用的,这一约束必须在系统的设计中体现出来。

4、系统提供一个产品类的库,所有的产品以同样的接口出现,从而使客户端不依赖于实现。

 

 

案例1:

还是以农场为例。

我们的农场分了多个产品线,一个是专门卖北方的蔬菜水果。一个专门卖南方的蔬菜水果。大家可以试着不写一下,这里就不给出示例了,uml类图大家可以试着画一下。

 

 

案例2:

以电脑为例

电脑分为PC电脑,mac电脑。pc电脑有他组装需要的cpu、主板、键盘 mac电脑同样需要这些组件,但是型号不一样,下面是此示例的uml类图。

 

 

 

 

 

代码示例:

 

此示例是依据女娲造人而写的。

 

复制代码

  1

  2 

  3 //此实例是根据女娲造人写的

  4 

  5 //首先定义一个全局的工厂接口

  6 //由阴绳 阳绳来继承 都要实现举绳子这个方法

  7 interface nvwaRope{

  8     public function liftPeopleRope($name);

  9     public function liftAnimalRope($name);

 10 }

 11 

 12 //定义阴绳 来继承 nvwaRope

 13 class yinRope implements nvwaRope{

 14     public function liftPeopleRope($name){

 15         return new girl($name);

 16     }

 17 

 18     public function liftAnimalRope($name){

 19         return new muAnimal($name);

 20     }

 21 }

 22 

 23 //定义阳绳 

 24 class yangRope implements nvwaRope{

 25     public function liftPeopleRope($name){

 26         return new boy($name);

 27     }

 28 

 29     public function liftAnimalRope($name){

 30         return new gongAnimal($name);

 31     }

 32 }

 33 

 34 

 35 //定义一个人的抽象接口

 36 interface people{

 37     public function eat();

 38     public function getName();

 39 }

 40 

 41 

 42 abstract class abstractPeople implements people{

 43     private $name;

 44 

 45     public function __construct($name){

 46         $this->name = $name;

 47     }

 48 

 49     public function eat(){

 50         echo $this->name . " eat";

 51     }

 52 

 53     public function getName(){

 54         echo $this->name;

 55         return $this->name;

 56     }

 57 

 58     //检查是否有胸

 59     public function checkChest(){

 60         return $this->haveChest;

 61     }

 62 }

 63 

 64 class boy extends abstractPeople{

 65     public $haveChest = false; //男孩没有胸

 66 }

 67 

 68 class girl extends abstractPeople{

 69     public $haveChest = true; //女孩有胸

 70 }

 71 

 72 

 73 //定义一个动物的抽象接口

 74 interface animal{

 75     public function eat();

 76     public function sleep();

 77 }

 78 

 79 abstract class abstractAnimal implements animal{

 80     private $name;

 81 

 82     public function __construct($name){

 83         $this->name = $name;

 84     }

 85 

 86     public function eat(){

 87         echo $this->name . " eating";

 88     }

 89 

 90     public function sleep(){

 91         echo $this->name . " sleeping";

 92     }

 93 

 94     public function getName(){

 95         echo $this->name;

 96         return $this->name;

 97     }

 98 }

 99 

100 //定义一个母动物的类

101 class muAnimal extends abstractAnimal{

102     public $xingbie = "mu";

103 }

104 

105 

106 //定义一个公动物的类

107 class gongAnimal extends abstractAnimal{

108     public $xingbie = "gong";

109 }

110 

111 

112 

113 //使用方式

114 

115 //首先获取工厂的实例 也就是阴绳或者阳绳的实例

116 $yinRope = new yinRope();

117 

118 $people = $yinRope->liftPeopleRope("zhangsan");

119 echo $people->eat();

120 echo $people->getName();

121 var_dump($people->checkChest());

122 

123 $animel = $yinRope->liftAnimalRope("马");

124 echo $animel->eat();



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn