zoukankan      html  css  js  c++  java
  • 小话设计模式一:适配器模式

    适配器模式定义:

      将一个类的接口适配成用户所期待的。一个适配允许通常因为接口不兼容而不能在一起工作的类工作在一起,做法是将类自己的接口包裹在一个已存在的类中。

    适配器模式分类:

      1、类适配器模式

        这种适配器模式下,适配器继承自已实现的类(一般多重继承)。

      2、对象适配器模式

        在这种适配器模式中,适配器容纳一个它包裹的类的实例。在这种情况下,适配器调用被包裹对象的物理实体。

    适配器模式解析:

      引例:我有一台笔记本电脑,有一个鼠标,笔记本有4个USB接口,没有PS2接口;鼠标却是PS2接口的。我没有办法把鼠标插入笔记本电脑中,我又不能换一台有PS2接口的笔记本电脑,也不能去买一个有USB接口的鼠标。也就是笔记本和鼠标是不能更换的,我怎么样才能让他们相互工作起来呢?

      笔记本电脑如图:

                                        

      鼠标如图:

                                                 

      我们要怎么样才能让笔记本和鼠标工作起来呢?答案就是我们需要这样一个转接器如下图):

                                                          

      这样笔记本和鼠标就能工作起来了,而这个转接器就类似适配器。

      下面我们来看看这两种适配器模式。

      1、类适配器模式

        当客户在接口中定义了他期望的行为时,我们就可以应用适配器模式,提供一个实现该接口的类,并且扩展已有的类,通过创建子类来实现适配。

        下面是类适配器的UML图:

                                        

        下面是简单代码实现:  

    //相当于笔记本
    class USBPort
    {
    public:
        virtual void usbworking();
        virtual ~USBPort(){}
    };
    
    void USBPort::usbworking()
    {
        cout<<"usbworking"<<endl;
    }
    
    //相当于鼠标
    class PS2Port
    {
    public:
        virtual void ps2working();
    };
    
    void PS2Port::ps2working()
    {
        cout<<"ps2working"<<endl;
    }
    
    //相当于转接器
    class Adapter : public USBPort, private PS2Port
    {
    public:
        void usbworking();
    };
    
    void Adapter::usbworking()
    {
        this->ps2working();
    }
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        USBPort *usbport = new Adapter;
        usbport->usbworking();
        return 0;
    }

      2、对象适配器模式

        对象适配器”通过组合除了满足“用户期待接口”还降低了代码间的不良耦合。在工作中推荐使用“对象适配器”。

        下面是对象适配器的UML图:

                                        

        下面是简单代码实现:

    //相当于笔记本
    class USBPort
    {
    public:
        virtual void usbworking();
        virtual ~USBPort(){}
    };
    
    void USBPort::usbworking()
    {
        cout<<"usbworking"<<endl;
    }
    
    //相当于鼠标
    class PS2Port
    {
    public:
        virtual void ps2working();
    };
    
    void PS2Port::ps2working()
    {
        cout<<"ps2working"<<endl;
    }
    
    //相当于转接器
    class Adapter : public USBPort
    {
    public:
        Adapter(PS2Port *ps2port): ps2port(ps2port) {};
        void usbworking();
    private:
        PS2Port *ps2port;
    };
    
    void Adapter::usbworking()
    {
        ps2port->ps2working();
    }
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        PS2Port *ps2port = new PS2Port;
        USBPort *usbport = new Adapter(ps2port);
        usbport->usbworking();
        return 0;
    }
  • 相关阅读:
    网站页面布局的实现
    谈谈我对MVC的View层实现的理解
    浅谈.htaccess文件--避免滥用.htaccess文件
    magento新增商品属性以及将属性加入Flat table
    MySQL JOIN | 联结
    Linux常用命令
    Laravel Model查询结果的3种存储格式内存占用对比
    Laravel配置Route调用artisan
    研究微信红包分配算法之Golang版
    解决IDEA提示Untrusted Server's certificate 证书不可用( Server's certificate is not trusted )
  • 原文地址:https://www.cnblogs.com/venow/p/2737885.html
Copyright © 2011-2022 走看看