8000 complete chapter 17 · webphper/design-patterns-by-php@6cf3432 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6cf3432

Browse files
committed
complete chapter 17
1 parent 0e507c1 commit 6cf3432

File tree

3 files changed

+233
-1
lines changed

3 files changed

+233
-1
lines changed

chapter17.md

Lines changed: 112 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,120 @@
11
### 第十七章 在NBA我需要翻译 --- 适配器模式
22

3+
```php
4+
<?php
5+
6+
//篮球翻译适配器
7+
abstract class Player
8+
{
9+
protected $name;
10+
11+
function __construct($name)
12+
{
13+
$this->name = $name;
14+
}
15+
16+
abstract public function Attack();
17+
abstract public function Defense();
18+
}
19+
20+
//前锋
21+
class Forwards extends Player
22+
{
23+
public function Attack()
24+
{
25+
echo "前锋:".$this->name." 进攻\n";
26+
}
27+
public function Defense()
28+
{
29+
echo "前锋:".$this->name." 防守\n";
30+
}
31+
}
32+
33+
//中锋
34+
class Center extends Player
35+
{
36+
function __construct()
37+
{
38+
parent::__construct();
39+
}
40+
41+
public function Attack()
42+
{
43+
echo "中锋:".$this->name." 进攻\n";
44+
}
45+
public function Defense()
46+
{
47+
echo "中锋:".$this->name." 防守\n";
48+
}
49+
}
50+
51+
//外籍中锋
52+
class ForeignCenter
53+
{
54+
private $name;
55+
public function setName($name)
56+
{
57+
$this->name = $name;
58+
}
59+
60+
public function getName()
61+
{
62+
return $this->name;
63+
}
64+
65+
public function 进攻()
66+
{
67+
echo "外籍中锋:".$this->name." 进攻\n";
68+
}
69+
70+
public function 防守()
71+
{
72+
echo "外籍中锋:".$this->name." 防守\n";
73+
}
74+
}
75+
76+
//翻译者
77+
class Translator extends Player
78+
{
79+
private $foreignCenter;
80+
81+
function __construct($name)
82+
{
83+
$this->foreignCenter = new ForeignCenter();
84+
$this->foreignCenter->setName($name);
85+
}
86+
87+
public function Attack()
88+
{
89+
$this->foreignCenter->进攻();
90+
}
91+
public function Defense()
92+
{
93+
$this->foreignCenter->防守();
94+
}
95+
96+
}
97+
98+
// 客户端代码
99+
$forwards = new Forwards("巴蒂尔");
100+
$forwards->Attack();
101+
$forwards->Defense();
102+
103+
$translator = new Translator("姚明");
104+
$translator->Attack();
105+
$translator->Defense();
106+
```
107+
3108
总结:
4109

110+
> ***适配器模式***,将一个类的接口转化成客户希望的另外一个接口。适配器模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作。
111+
112+
> 系统的数据和行为都正确,但接口不符时,我们应该考虑用适配器,目的是使控制范围之外的一个原有对象与某个接口匹配。适配器模式主要应用于希望复用一些现存的类。但是接口又与复用环境要求不一致的情况。
113+
114+
> 两个类所做的事情相同或相似,但是具有不同的接口时要使用它。
5115
116+
> 在双方都不太容易修改的时候再使用适配器模式适配。
6117
7118
上一章:[第十六章 无尽加班何时休 --- 状态模式](https://github.com/flyingalex/design-patterns-by-php/blob/master/chapter16.md)
8119

9-
下一章:[第十八章](https://github.com/flyingalex/design-patterns-by-php/blob/master/chapter18.md)
120+
下一章:[第十八章 如果再回到从前 --- 备忘录模式](https://github.com/flyingalex/design-patterns-by-php/blob/master/chapter18.md)

chapter18.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
### 第十八章 如果再回到从前 --- 备忘录模式
2+
3+
总结:
4+
5+
6+
7+
上一章:[第十七章 在NBA我需要翻译 --- 适配器模式](https://github.com/flyingalex/design-patterns-by-php/blob/master/chapter17.md)
8+
9+
下一章:[第十九章 ](https://github.com/flyingalex/design-patterns-by-php/blob/master/chapter19.md)

codes/Adapter.php

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
<?php
2+
3+
//篮球翻译适配器
4+
abstract class Player
5+
{
6+
protected $name;
7+
8+
function __construct($name)
9+
{
10+
$this->name = $name;
11+
}
12+
13+
abstract public function Attack();
14+
abstract public function Defense();
15+
}
16+
17+
//前锋
18+
class Forwards extends Player
19+
{
20+
public function Attack()
21+
{
22+
echo "前锋:".$this->name." 进攻\n";
23+
}
24+
public function Defense()
25+
{
26+
echo "前锋:".$this->name." 防守\n";
27+
}
28+
}
29+
30+
//中锋
31+
class Center extends Player
32+
{
33+
function __construct()
34+
{
35+
parent::__construct();
36+
}
37+
38+
public function Attack()
39+
{
40+
echo "中锋:".$this->name." 进攻\n";
41+
}
42+
public function Defense()
43+
{
44+
echo "中锋:".$this->name." 防守\n";
45+
}
46+
}
47+
48+
//外籍中锋
49+
class ForeignCenter
50+
{
51+
private $name;
52+
public function setName($name)
53+
{
54+
$this->name = $name;
55+
}
56+
57+
public function getName()
58+
{
59+
return $this->name;
60+
}
61+
62+
public function 进攻()
63+
{
64+
echo "外籍中锋:".$this->name." 进攻\n";
65+
}
66+
67+
public function 防守()
68+
{
69+
echo "外籍中锋:".$this->name." 防守\n";
70+
}
71+
}
72+
73+
//翻译者
74+
class Translator extends Player
75+
{
76+
private $foreignCenter;
77+
78+
function __construct($name)
79+
{
80+
$this->foreignCenter = new ForeignCenter();
81+
$this->foreignCenter->setName($name);
82+
}
83+
84+
public function Attack()
85+
{
86+
$this->foreignCenter->进攻();
87+
}
88+
public function Defense()
89+
{
90+
$this->foreignCenter->防守();
91+
}
92+
93+
}
94+
95+
96+
97+
// 客户端代码
98+
$forwards = new Forwards("巴蒂尔");
99+
$forwards->Attack();
100+
$forwards->Defense();
101+
102+
$translator = new Translator("姚明");
103+
$translator->Attack();
104+
$translator->Defense();
105+
106+
107+
108+
109+
110+
111+
112+

0 commit comments

Comments
 (0)
0