结构型模式之适配器模式(Adapter Pattern)与外观模式(Facade Pattern)

  

适配器模式定义:将一个类的接口,转换成客户期望的另一个接口。适配器让原本接口不兼容的类可以合作无间。

   适配器模式的工作是将一个接口转换成另一个。如果让一个适配器包装多个被适配者就是Façade 模式。

对象适配器类图:

 


类适配器:


对象适配器利用组合的方式将请求传送给被适配者。类适配器采用多重继承来实现。Adapter继承了Target和Adaptee

 

 

 

适配器与装饰模式看起来类似,都是通过委托来包装对象的行为或责任,但是它们的意图差异颇大。

 

适配器模式:将一个接口转成另一接口。

外观模式:让接口更简单,它将一个或数个类的复杂的一切都隐藏在背后。

装饰模者式:不改变接口,但加入职责。

 

外观模式定义:提供了一个统一的接口,用来访问子系统中的一群接口。外观定义了一个高层接口,让子系统更容易使用。

外观模式提供简化的接口的同时,依然将系统的功能暴露出来,以供需要的人使用。外观模式与适配器的不同点在于他们的意图,不能从他们包装多少个类来判别,它们都可以包装许多类,但是外观模式的意图是简化接口,而适配器的意图是将接口转换成不同的接口符合客户的期望。


适配器模式的C++实现:

Adapter.h头文件

#ifndef _ADAPTER_H_
#define _ADAPTER_H_
#include<iostream>
using namespace std;
class Target
{
public:
	   Target();
	virtual ~Target();
	virtual void Request();

};
class Adaptee
{
public:
	Adaptee();
	~Adaptee();
	void SpecificRequest();

};

//类适配器,通过继承来实现
class Adapter:public Target,private Adaptee
{
public:
	Adapter();
	~Adapter();
	void Request();
};

//对象适配器,通过组合实现
class OAdapter:public Target
{
public:
OAdapter(Adaptee * ade);
~OAdapter();
void Request();

protected:

private:
Adaptee * _ade;
};

#endif

Adapter.CPP

#include "Adapter.h"
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();
}

//对象适配器实现
OAdapter::OAdapter(Adaptee *ade)
{
	this->_ade=ade;

}
OAdapter::~OAdapter()
{

}
void OAdapter::Request()
{
	_ade->SpecificRequest();
}

主函数main.CPP

#include "Adapter.h"
#include <iostream>
using namespace std;
int main(int argc,char* argv[])
{
Adaptee* ade = new Adaptee;
//对象适配器
Target* adt = new OAdapter(ade);
adt->Request();

//类适配器
Target* cadt=new Adapter;
cadt->Request();

return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
适配器模式(Adapter Pattern)是一种结构型设计模式,它允许你将现有的类(即不兼容的类)转换为其他接口所期望的接口。适配器模式旨在使原本由于接口不兼容而不能一起工作的类可以一起工作[^1]。适配器模式通常用于系统的接口设计中,对于现有的系统,由于接口不兼容而不能满足现有的需求,但是系统的功能又不能改变,此时可以考虑采用适配器模式外观模式(Facade Pattern)是一种结构型设计模式,它提供了一个简化接口,使得客户端能够更方便地访问复杂系统。外观模式可以将系统的复杂性与客户端的代码分开,降低客户端的复杂度,并提高系统的可复用性[^2]。外观模式通常用在大型系统的开发中,可以将系统的各个模块进行组织,提供简化的接口给客户端使用。 下面是适配器模式外观模式的代码范例: 适配器模式: ```python class Adaptee: def specific_request(self): return 42 class Adapter: def __init__(self, adaptee): self.adaptee = adaptee def request(self): return self.adaptee.specific_request() if __name__ == "__main__": adaptee = Adaptee() adapter = Adapter(adaptee) assert adapter.request() == 42 ``` 外观模式: ```python class SubSystem1: def method1(self): return "SubSystem1 method1" def method2(self): return "SubSystem1 method2" class SubSystem2: def method1(self): return "SubSystem2 method1" def method2(self): return "SubSystem2 method2" class Facade: def __init__(self): self.subsystem1 = SubSystem1() self.subsystem2 = SubSystem2() def operation(self): results = [] results.append("Facade initializes subsystems:") results.append(self.subsystem1.method1()) results.append(self.subsystem2.method1()) results.append("Facade orders subsystems to perform the action:") results.append(self.subsystem1.method2()) results.append(self.subsystem2.method2()) return "\n".join(results) if __name__ == "__main__": facade = Facade() print(facade.operation()) ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值