zoukankan      html  css  js  c++  java
  • Adapter 模式

    在实际软件系统设计和开发中,会经常遇到这种问题:我们为了完成某项工作购买了一个第三方的库来加快开发。 这就带来了一个问题: 我们在应用程序中已经设计好了接口,与这个第三方提供的接口不一致,为了使得这些接口不兼容的类可以在一起工作,Adapter 模式提供了将一个类(第三方库)的接口转化为客户(购买使用者)希望的接口。

     1 //////////////Adapter.h//////////////////
     2 #pragma once 
     3 class Target
     4 {
     5 public:
     6     virtual ~Target();
     7     virtual void Request();
     8     Target();
     9 protected:
    10 private:
    11 };
    12 
    13 class Adaptee
    14 {
    15 public:
    16     Adaptee();
    17     ~Adaptee();
    18     void SpecificRequest();
    19 };
    20 
    21 class Adapter : public Target ,private Adaptee
    22 {
    23 public:
    24     Adapter();
    25     ~Adapter();
    26     void Request();
    27 protected:
    28 private:
    29 };
    //////////////Adapter.cpp////////////////////
    #include "Adapter.h"
    #include <iostream>
    using namespace std ;
    
    Target::Target()
    {
    
    }
    Target::~Target()
    {
    
    }
    void Target::Request()
    {
        cout<<"Target::Request"<<endl;
    }
    
    Adaptee::Adaptee()
    {
    
    }
    Adaptee::~Adaptee()
    {
    
    }
    void Adaptee::SpecificRequest()
    {
        cout<<"Adaptee::SpecificRequest"<<endl;
    }
    
    
    
    Adapter::Adapter()
    {
    
    }
    Adapter::~Adapter()
    {
    
    }
    void Adapter::Request()
    {
        this->SpecificRequest();
    }
     1 //////////////main.cpp//////////////////////////////
     2 #include "Adapter.h"
     3 #include <iostream>
     4 int main()
     5 {
     6     Target* t = new Adapter();
     7     t->Request();
     8     getchar();
     9     return 0 ;
    10 }
  • 相关阅读:
    PHP学习笔记(二)
    PHP开发笔记(一)
    PHP NOTE
    python windows进制文件可以直接下载使用
    深入浅出Hyper-V网络虚拟化技术
    深入浅出Hyper-V网络虚拟化(序)
    Windows Server 笔记(七):Windows Server 2012 R2 NIC Teaming(NIC组)
    Hyper-v Server 2012 R2增强会话模式
    Hyper-V动态迁移中?小心性能损失
    CLOUDSTACK FOR HYPER-V
  • 原文地址:https://www.cnblogs.com/csxcode/p/3707955.html
Copyright © 2011-2022 走看看