首页 > 代码库 > 细学设计模式一(组合与继承)

细学设计模式一(组合与继承)

 1 <?php
 2 abstract class Lesson{
 3     protected $duration;
 4     const FIXED = 1;
 5     const TIMED = 2;
 6     private $costtype;
 7     
 8     function __construct($duration,$costtype){
 9         $this->duration = $duration;
10         $this->costtype = $costtype;
11     }
12     
13     function cost(){
14         switch ($this->costtype){
15             case self::TIMED :
16                 return (5*$this->duration);
17                 break;
18             case self::FIXED:
19                 return 30;
20                 break;
21             default :
22                 $this->costtype = self::FIXED;
23                 return 30;
24         }
25     }
26     
27     function chargeType()
28     {
29         switch ($this->costtype)
30         {
31             case self::TIMED:
32                 return "hourly rate";
33                 break;
34             case self::FIXED:
35                 return "fixed rate";
36                 break;
37             default :
38                 $this->costtype = self::FIXED;
39                 return "fixed rate";
40         }
41     }
42     
43     //Lesson的更多方法
44 }
45 
46 class Lecture extends Lesson{
47     //Lecture特定的实现
48 }
49 
50 class Seminar extends Lesson{
51     //Seminar特定的实现
52 }
53 
54 //下面如何使用这些类
55 $lecture = new Lecture(5,Lesson::FIXED);
56 print "{$lecture->cost()},{$lecture->chargeType()} \n";//30,fixed rate
57 
58 $seminar = new Seminar(3,Lesson::TIMED);
59 print "{$seminar->cost()},{$seminar->chargeType()} \n";//15,hourly rate

 

使用组合:使用策略模式(strategy)来解决条件语句

<?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 lecture(4,new TimedCostStrategy());
$lessons[] = new Seminar(4,new FixedCostStrategy());

foreach ($lessons as $lesson)
{
    print "lesson charge: {$lesson->cost()} ";
    print "charge type: {$lesson->chargeType()} \n";
}

//lesson charge: 20 charge type: hourly rate 
//lesson charge: 30 charge type: fixed rate