抽象工厂模式 - 设计模式 - PHP版
2021-05-18 16:27
标签:article detail nim ati new sdn ice blog ack 抽象工厂模式 - 设计模式 - PHP版 标签:article detail nim ati new sdn ice blog ack 原文地址:http://www.cnblogs.com/benben7466/p/7736507.html 1 php
2 /*
3 * 抽象工厂模式
4 *
5 * 参考:http://blog.csdn.net/zhaoxuejie/article/details/7087689
6 *
7 */
8 //抽象工厂
9 interface AnimalFactory {
10 public function createCat();
11 public function createDog();
12 }
13 //具体工厂
14 class BlackAnimalFactory implements AnimalFactory {
15 function createCat() {
16 return new BlackCat();
17 }
18 function createDog() {
19 return new BlackDog();
20 }
21 }
22 class WhiteAnimalFactory implements AnimalFactory {
23 function createCat() {
24 return new WhiteCat();
25 }
26 function createDog() {
27 return new WhiteDog();
28 }
29 }
30 //抽象产品
31 interface Cat {
32 function Voice();
33 }
34 interface Dog {
35 function Voice();
36 }
37 //具体产品
38 class BlackCat implements Cat {
39 function Voice() {
40 echo ‘黑猫喵喵……‘;
41 }
42 }
43 class WhiteCat implements Cat {
44 function Voice() {
45 echo ‘白猫喵喵……‘;
46 }
47 }
48 class BlackDog implements Dog {
49 function Voice() {
50 echo ‘黑狗汪汪……‘;
51 }
52 }
53 class WhiteDog implements Dog {
54 function Voice() {
55 echo ‘白狗汪汪……‘;
56 }
57 }
58 //客户端
59 class Client {
60 public static function main() {
61 self::run(new BlackAnimalFactory());
62 self::run(new WhiteAnimalFactory());
63 }
64 public static function run(AnimalFactory $AnimalFactory) {
65 $cat = $AnimalFactory->createCat();
66 $cat->Voice();
67 $dog = $AnimalFactory->createDog();
68 $dog->Voice();
69 }
70 }
71 Client::main();
文章标题:抽象工厂模式 - 设计模式 - PHP版
文章链接:http://soscw.com/index.php/essay/87258.html