zoukankan      html  css  js  c++  java
  • 设计模式-适配器模式

    在我们平常用的手机充电器,就是一个适配器,它把我们日常使用的交流电通过手机充电器(适配器)转化成手机使用的直流电。

    适配器模式Adapter Pattern: (结构型模式)
    将一个接口转换为客户希望的另外一个接口,适配器模式使接口不兼容的那些类可以一起工作
    适配器别名又叫包装器 Wrapper。

    模式中的角色:
    1 目标接口(Target):客户所期待的接口。目标可以是具体的或抽象的类,也可以是接口。

    2 需要适配的类(Adaptee):需要适配的类或适配者类。

    3 适配器(Adapter):通过包装一个需要适配的对象,把原接口转换成目标接口。

    示例:
    1: 使用继承模式

    <?php
    /*
    使用继承的适配器模式
    
    <?php
    class Adapter extends YourClass implements OtherInterface
    ?>
    */
    
    //已经实现了鲸鱼类
    class Whale
    {
        public function __construct() {
            $this->name = "Whale";
        }
    
        public function eatFish() {
            echo "whale eat fish. 
    ";
        }
    }
    //已经实现了鲤鱼类
    class Carp
    {
        public function __construct() {
            $this->name = "Carp";
        }
    
        public function eatMoss() {
            echo "Carp eat moss. 
    ";
        }
    }
    
    
    //现在要建一个动物馆
    interface Animal
    {
        function eatFish();
        function eatMoss();
    }
    
    //但是我们不能修改Whale 和 Carp类,于是用Adapter模式
    class WhaleAdapter extends Whale implements Animal
    {
        public function __construct() {
            $this->name = "Whale";
        }
        public function eatMoss() {
            echo "Whale do not eat moss. 
    ";
        }
    }
    
    class CarpAdapter extends Carp implements Animal
    {
        public function __construct() {
            $this->name = "Carp";
        }
    
        public function eatFish() {
            echo "Carp do not eat moss. 
    ";
        }
    }
    
    $whaleAdpter = new WhaleAdapter();
    $whaleAdpter->eatFish();
    $whaleAdpter->eatMoss();
    
    $carpAdapter = new CarpAdapter();
    $carpAdapter->eatFish();
    $carpAdapter->eatMoss();

    2:使用组件的适配器模式

    <?php
    /*
    使用组件的适配器模式
    
    */
    
    //已经实现了鲸鱼类
    class Whale
    {
        public function __construct() {
            $this->name = "Whale";
        }
    
        public function eatFish() {
            echo "whale eat fish. 
    ";
        }
    }
    //已经实现了鲤鱼类
    class Carp
    {
        public function __construct() {
            $this->name = "Carp";
        }
    
        public function eatMoss() {
            echo "Carp eat moss. 
    ";
        }
    }
    
    //现在要建一个动物馆
    interface Animal 
    {
      function eatFish();
      function eatMoss();
    }
    
    class WhaleAdapter implements Animal
    {
        public function __construct() {
            $this->name = "Whale";
            $this->whale = new Whale();
        }
    
        public function eatFish() {
            $this->whale->eatFish();
        }
    
        public function eatMoss() {
            echo "Whale do not eat moss. 
    ";
        }
    }
    
    class CarpAdapter implements Animal
    {
        public function __construct() {
            $this->name = "Carp";
            $this->carp = new Carp();
        }
    
        public function eatFish() {
            echo "Carp do not eat moss. 
    ";
        }
    
        public function eatMoss() {
            $this->carp->eatMoss();
        }
    }
    
    $whaleAdpter = new WhaleAdapter();
    $whaleAdpter->eatFish();
    $whaleAdpter->eatMoss();
    
    $carpAdapter = new CarpAdapter();
    $carpAdapter->eatFish();
    $carpAdapter->eatMoss();

    来自:http://csprojectedu.com/2016/03/18/PHPDesignPatterns-10/

  • 相关阅读:
    理解SVG坐标系统和变换: transform属性
    在svg文间画图过程中放大缩小图片后,坐标偏移问题
    理解SVG的缩放 偏移的计算公式
    svg 实践之屏幕坐标与svg元素坐标转换
    Winform 程序打包及安装
    使用bootstrap table小记(表格组件)
    MVC实现多级联动
    微信公众号开发之网页中及时获取当前用户Openid及注意事项
    微信公众号开发之网页授权获取用户基本信息
    微信公众号开发之自动消息回复和自定义菜单
  • 原文地址:https://www.cnblogs.com/jiujuan/p/10296257.html
Copyright © 2011-2022 走看看