日记

看《深入PHP 面向对象,模式与时间》有几天了

虽然看了前1/4了但是感觉还是步子迈的有点大了

很多东西在头脑里还是只是有模糊印象并没有完全吸收

一方面自身接受能力有限猛的补充了那么多知识一下也接受不了

既然没有别人学的快 那就利用自己的时间 多复习 多练习 多记忆吧

明天也不打算继续往后面读了

先把前面这些内容弄得很熟悉之后再继续下去

要不然还和上周看的Android一样

虽然全部看完了但是并没有完全理解其中细节

照着书中代码敲出了不少app但是如果手里没有书可能独立也写不出来

一步一个脚印好了 好歹也在进步 不要放弃

啊哈哈哈哈

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
                   _ooOoo_
                  o8888888o
                  88" . "88
                  (| -_- |)
                  O\  =  /O
               ____/`---'\____
             .'  \\|     |//  `.
            /  \\|||  :  |||//  \
           /  _||||| -:- |||||-  \
           |   | \\\  -  /// |   |
           | \_|  ''\---/''  |   |
           \  .-\__  `-`  ___/-. /
         ___`. .'  /--.--\  `. . __
      ."" '<  `.___\_<|>_/___.'  >'"".
     | | :  `- \`.;`\ _ /`;.`/ - ` : | |
     \  \ `-.   \_ __\ /__ _/   .-` /  /
======`-.____`-.___\_____/___.-`____.-'======
                   `=---='
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
           佛祖保佑       永无BUG

组合&把不同的实现隐藏在父类所定义的共同接口下

组合使用对象比集成体系更灵活,因为组合可以以多种方式动态的处理任务。

把不同的实现隐藏在父类所定义的共同接口下.

然后客户端代码需要一个父类的对象,从而使客户端代码可以不用关心它实际得到的是哪个具体的实现.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
 
abstract class Lesson{
    private $duration;
    private $costStrategy;//支付策略
 
    function __construct($duration, CostStrategy $strategy)
    {
        $this->duration = $duration;
        $this->costStrategy = $strategy;
    }
 
    function cost(){
        return $this->costStrategy->cost($this);
    }
 
    function chargeType(){
        return $this->costStrategy->chargeType();
    }
 
    function getDuration(){
        return $this->duration;
    }
 
    //Lesson类的更多方法
}
 
class Lecture extends Lesson{
    //Lecture特定的实现
}
 
 class Seminar extends Lesson{
    //Seminar特定的实现
 }
 
 abstract class CostStrategy{
    abstract function cost(Lesson $lesson);
    abstract function chargeType();
 }
 
class TimedCostStrategy extends CostStrategy{
    function cost(Lesson $lesson){
        return ($lesson->getDuration() * 5);
    }
 
    function chargeType(){
        return "hourly rate";
    }
}
 
class FixedCostStrategy extends CostStrategy{
    function cost(Lesson $lesson){
        return 30;
    }
 
    function chargeType(){
        return "fixed rate";
    }
}
 
$lessons[] = new Seminar(4, new TimedCostStrategy());
$lessons[] = new Lecture(4, new FixedCostStrategy());
 
foreach($lessons as $lesson){
    print "lesson charge {$lesson->cost()}.";
    print "Charge type: {$lesson->chargeType()} <br />";
}
 
 
?>