zoukankan      html  css  js  c++  java
  • 细学设计模式一(组合与继承)

     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()} 
    ";//30,fixed rate
    57 
    58 $seminar = new Seminar(3,Lesson::TIMED);
    59 print "{$seminar->cost()},{$seminar->chargeType()} 
    ";//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()} 
    ";
    }
    
    //lesson charge: 20 charge type: hourly rate 
    //lesson charge: 30 charge type: fixed rate 
  • 相关阅读:
    WEB服务-Nginx之10-动静分离
    第10课 文件指针及目录的创建与删除
    c++ 中常用类型转换
    编译c++文件时报错:在...中已定义,例如:已经在 .obj 中定义
    No converter found for return value of type: class java.util.ArrayList
    Unable to ping server at localhost:1099
    Failed building wheel for twisted
    第六天-缺陷和缺陷报告
    第五天-黑盒测试用例设计方法(二)
    第四天-测试用例和设计方法(一)
  • 原文地址:https://www.cnblogs.com/jami918/p/3714380.html
Copyright © 2011-2022 走看看