zoukankan      html  css  js  c++  java
  • php实现设计模式之 适配器模式

    <?php
    /*
     * 适配器模式:将一个类的接口转换成客户希望的另外一个接口。Adapter模式使得原来由于接口不兼容而不能一起工作的那些类可以一起工作(结构型模式)
     * 
     * 一个源接口,不符合客户的需求
     * 一个目标接口,客户需要的接口
     * 适配器类,实现客户的接口,包装了源接口
     */
    
    //源接口
    interface China{
    	public function flat(); 
    }
    
    class Chinese implements China{
    	public function flat(){
    		echo '我用扁形孔充电';
    	}
    }
    
    //$xiaoming = new Chinese();
    //$xiaoming->flat();
    
    //以上是已经存在的对象小明,他在中国用扁形孔来充电
    //现在他到了欧洲,欧洲充电是圆形孔
    
    
    //目标接口
    interface Europe{
    	public function round();
    }
    
    //适配器,包含源接口,实现(继承)目标接口
    class European implements Europe{
    	public $xiaoming;
    	public function __construct($chinese){
    		$this->xiaoming = $chinese;
    	}
    	
    	public function round(){
    		echo '在欧洲,利用电源适配器,';
    		$this->xiaoming->flat();
    	}
    }
    
    class Client{
    	public static function main(){
    		$chinese = new Chinese();
    		$european = new European($chinese);
    		$european->round();
    		
    	}
    }
    Client::main();
    ?>
    

      UML类图:

  • 相关阅读:
    监听用户的访问的链接
    软件开发的流程
    PHP性能优化四(业务逻辑中使用场景)
    php性能优化三(PHP语言本身)
    php性能优化二(PHP配置php.ini)
    poj 1676
    计蒜客 蒜头君下棋
    计蒜客 划分整数
    计蒜客 蒜头君的数轴
    计蒜客 藏宝图
  • 原文地址:https://www.cnblogs.com/taijun/p/4103235.html
Copyright © 2011-2022 走看看