Design Pattern - Adapter

本文详细介绍了适配器模式的概念及其实现方式,通过对比Bridge模式突出其在系统整合阶段的作用,并提供了一个C++实现的例子。

评价:这是一个战斗级别的Pattern,多用于系统整合阶段的软件设计。它形式上和Bridge类似的地方,但是很显然Adapter目标接口转换,说白了是为了应用,基于existing的功能完成其他功能需求。

而Bridge目标于大规模的接口和实现的隔离,它使软件系统更加解藕(decouple),这是软件设计的终极目标,所以我说它是战役级别的Pattern


适用场景

  1. 使用已经存在的类,或者第三方的类,但是需要集成到现在的统一接口设计。

  2. 用于整合一个与现有设计不兼容的类,使用此模式来整合。

// Adapter.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
using namespace std;

class Adaptee
{
public:
	void SpecialMethod() const {
		cout << "this is constant component without source code" << endl;
	}
};

class ITarget
{
public:
	virtual void Method() = 0;
};

class TargetDerive : public ITarget
{
public:
	void Method() {
		cout << "this is a normal derive implementation." << endl;
	}
};

class TargetAdapter : public ITarget
{
	// the adaptee object
	Adaptee _adaptee;
public:
	void Method(){
		_adaptee.SpecialMethod();
	}

};


int main(int argc, void* argv[])
{
	TargetDerive normalDerive;
	normalDerive.Method();

	//Adapter client for consumer
	TargetAdapter adapter;
	adapter.Method();

	return 0;
}


转载于:https://my.oschina.net/zhujinbao/blog/223450

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值