php设计模式课程---7、装饰器模式如何使用
php设计模式课程---7、装饰器模式如何使用
一、总结
一句话总结:
装饰器的核心是获取了文章类整个类,而不是获取了文章内容,有了这个文章类,我想给你加多少装饰就给你加多少装饰(将文章这个类封装进去),在这个类的外部,在装饰器中。
4 class Art {
5 protected $content;
6 public function __construct($content) {
7 $this->content = $content;
8 }
9
10 public function decorator() {
11 return $this->content;
12 }
13 }
14
15 class Artdec extends Art {//装饰基类
16 protected $art = null;
17
18 public function __construct($art) {
19 $this->art = $art;
20 }
21
22 public function decorator() {
23 }
24 }
1、装饰器的装饰放在哪?
装饰器相当于可以实现多继承在装饰和本体之间放东西(装饰类里面的方法属性),而不是在本体里面放东西:比如说在seo装饰类里面加个属性
15 class Artdec extends Art {//装饰基类
16 protected $art = null;
17
18 public function __construct($art) {
19 $this->art = $art;
20 }
21
22 public function decorator() {
23 }
24 }
25
26 class SeoArt extends Artdec {//seo装饰类
27 public function decorator() {
28 return $this->art->decorator() . ' SEO keywords';
29 }
30 }
2、不同装饰类之间的层级关系如何?
装饰基类其实是在文章类上面套了一个盒子,把文章类包起来了。
不同的装饰类之间平级,不同的装饰类之间可以互相嵌套。就像给文章类这个东西加上一层层的装饰盒子一样。
15 class Artdec extends Art {//装饰基类
16 protected $art = null;
17
18 public function __construct($art) {
19 $this->art = $art;
20 }
21
22 public function decorator() {
23 }
24 }
25
26 class SeoArt extends Artdec {//seo装饰类
27 public function decorator() {
28 return $this->art->decorator() . ' SEO keywords';
29 }
30 }
31
32 class AdArt extends Artdec {//广告装饰类
33 public function decorator() {
34 return $this->art->decorator() . ' 广告内容';
35 }
36 }
3、装饰模式如何使用?
用一个装饰类去包装其它或包装或没包装的类
38 $art = new Art('这是一篇普通文件');
39 $art = new SeoArt($art);
40 $art = new AdArt($art);
41
42 echo $art->decorator() , "<br>";
43
44
45 $zz = new Art('这是一篇政治文件');
46 $zz = new SeoArt($zz);
47 echo $zz->decorator();
二、装饰器模式如何使用
1、代码
场景:一篇文章既加seo又加广告

装饰器模式代码
1 <?php 2 // 装饰器模式 3 4 class Art { 5 protected $content; 6 public function __construct($content) { 7 $this->content = $content; 8 } 9 10 public function decorator() { 11 return $this->content; 12 } 13 } 14 15 class Artdec extends Art {//装饰基类 16 protected $art = null; 17 18 public function __construct($art) { 19 $this->art = $art; 20 } 21 22 public function decorator() { 23 } 24 } 25 26 class SeoArt extends Artdec {//seo装饰类 27 public function decorator() { 28 return $this->art->decorator() . ' SEO keywords'; 29 } 30 } 31 32 class AdArt extends Artdec {//广告装饰类 33 public function decorator() { 34 return $this->art->decorator() . ' 广告内容'; 35 } 36 } 37 38 $art = new Art('这是一篇普通文件'); 39 $art = new SeoArt($art); 40 $art = new AdArt($art); 41 42 echo $art->decorator() , "<br>"; 43 44 45 $zz = new Art('这是一篇政治文件'); 46 $zz = new SeoArt($zz); 47 echo $zz->decorator(); 48 ?>
非装饰器模式代码
1 <?php 2 // 装饰器模式 3 4 class Art { 5 protected $content; 6 public function __construct($content) { 7 $this->content = $content; 8 } 9 10 public function decorator() { 11 return $this->content; 12 } 13 } 14 15 class SeoArt extends Art { 16 public function decorator() { 17 return parent::decorator() . ' SEO Keywords'; 18 } 19 } 20 21 class AdArt extends SeoArt { 22 public function decorator() { 23 return parent::decorator() . ' 广告文本'; 24 } 25 } 26 27 /* 28 $art = new SeoArt('世界大力世比赛'); 29 echo $art->decorator(); 30 */ 31 32 $ad = new AdArt('世界大力世比赛'); 33 echo $ad->decorator(); 34 35 36 ?>


浙公网安备 33010602011771号