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

    #pragma once
    #include "stdafx.h"
    
    #include<iostream>
    using namespace std;
    
    
    /*
    设计模式-适配器模式 Adapter
    
    将一个类的接口转换成客户希望的;另外一个接口。Adapter模式使得原本由于接口
    不兼容而不能一起工作的那些类可以一起工作。
    1.使用一个已经存在的类,但如果它的接口,也就是它的方法和你的要求不相同时,就应该考虑使用适配器模式。
    2.客户代码可以统一调用同一接口就行了,这样应该可以更简单、更直接。更紧凑。
    3.我们通常是在软件开发后期或者是开发过程中使用别人的模块才使用这个模式。
    4.在双方都不太容易修改的时候再使用适配器模式适配,而不是一有不同时就使用它。
    下面是实现代码,很简单。
    */
    
    class CTarget {//期待的
    public:
    	virtual void Request() = 0;
    };
    
    class CAdaptee {//已有的,需要适配的
    public:
    	void SpecificRequest() {
    		cout << "特殊请求" << endl;
    	}
    };
    
    class CAdapter : public CTarget {//实际上拿来用的
    private:
    	CAdaptee * m_pAdaptee = NULL;
    public:
    	CAdapter() { m_pAdaptee = new CAdaptee(); }
    	~CAdapter() { delete m_pAdaptee; }
    	void Request() {
    		m_pAdaptee->SpecificRequest();
    	}
    };
    
    
    
    int main() {
    
    	CTarget *pTarget = new CAdapter();
    	pTarget->Request();
    
    	getchar();
    	return 0;
    }
  • 相关阅读:
    670. Maximum Swap
    653. Two Sum IV
    639. Decode Ways II
    636. Exclusive Time of Functions
    621. Task Scheduler
    572. Subtree of Another Tree
    554. Brick Wall
    543. Diameter of Binary Tree
    535. Encode and Decode TinyURL
    博客园自定义背景图片
  • 原文地址:https://www.cnblogs.com/csnd/p/12061914.html
Copyright © 2011-2022 走看看