Design patterns 桥接模式和适配器模式的区别
桥接器模式和适配器模式之间有什么区别? 适配器模式更多的是让现有代码与更新的系统或接口一起工作Design patterns 桥接模式和适配器模式的区别,design-patterns,adapter,bridge,Design Patterns,Adapter,Bridge,桥接器模式和适配器模式之间有什么区别? 适配器模式更多的是让现有代码与更新的系统或接口一起工作 如果您有一组公司标准的Web服务API,您想提供给另一个应用程序现有的可扩展性接口,您可以考虑编写一组适配器来实现这一点。请注意,这里有一个灰色区域,这更多的是关于如何从技术上定义模式,因为其他模式(如立面)是类似的 桥接模式将允许您可能有算法或系统的替代实现 虽然不是一个经典的桥接模式示例,但是想象一下,如果您有一些数据存储的实现:一个在空间上是有效的,另一个在原始性能上是有效的。。。你有一个商业
如果您有一组公司标准的Web服务API,您想提供给另一个应用程序现有的可扩展性接口,您可以考虑编写一组适配器来实现这一点。请注意,这里有一个灰色区域,这更多的是关于如何从技术上定义模式,因为其他模式(如立面)是类似的
桥接模式将允许您可能有算法或系统的替代实现 虽然不是一个经典的桥接模式示例,但是想象一下,如果您有一些数据存储的实现:一个在空间上是有效的,另一个在原始性能上是有效的。。。你有一个商业案例可以在你的应用程序或框架中提供对于您的问题,“我可以在哪里使用哪种模式”,答案是,无论它对您的项目有何意义!也许考虑提供一个澄清编辑来指导讨论你认为你需要使用一个或另一个。 “适配器使事物在设计完成后工作;桥接器使它们工作 在他们到达之前工作。[GoF,p219] 实际上,当您拥有现有代码时,适配器模式非常有用,无论是第三方代码还是内部代码,但不受您的控制,或者无法更改以完全满足您需要的接口。例如,我们有一个超级武器阵列,可以控制一系列世界末日装置
public class SuperWeaponsArray {
/*...*/
public void destroyWorld() {
for (Weapon w : armedWeapons) {
w.fire();
}
}
}
太好了。除非我们意识到,我们的武库中有一个核装置,远远早于武器接口的转换。但我们真的希望它在这里起作用。。。那我们该怎么办。。。插进去
NukeWeaponsAdaptor-基于我们的Nuke类,但导出武器接口。亲爱的,现在我们肯定可以毁灭世界了。这看起来有点混乱,但它使事情顺利进行
桥接模式是您预先实现的—如果您知道您有两个正交的层次结构,它提供了一种将接口和实现解耦的方法,这样您就不会得到数量惊人的类。假设你有: 文件对象的MemoryMappedFile和DirectReadFile类型。假设您希望能够从各种源读取文件(可能是Linux与Windows实现等)。Bridge帮助您避免陷入以下困境: MemoryMappedWindowsFile MemoryMappedLinux文件 DirectReadWindowsFile
DirectReadLinuxFile这篇文章已经发布了很长一段时间了。然而,重要的是要理解,facade在某种程度上类似于适配器,但它不是完全相同的东西。适配器将现有类“适配”到通常不兼容的客户机类。假设您有一个旧的工作流系统,您的应用程序将其用作客户端。您的公司可能会用一个新的“不兼容”工作流系统(在接口方面)来替换工作流系统。在大多数情况下,您可以使用适配器模式并编写实际调用新工作流引擎接口的代码。桥梁通常以不同的方式使用。如果您的系统需要与不同的文件系统(即本地磁盘、NFS等)协同工作,那么您可以使用桥接模式并创建一个抽象层来与您的所有文件系统协同工作。这基本上是桥接模式的一个简单用例。Facade和适配器确实共享一些属性,但facades通常用于简化现有接口/类。在EJB的早期,没有对EJB的本地调用。开发人员总是获得存根,缩小它的范围,并称之为“伪远程”。这通常会导致性能问题(特别是在真正通过线路调用时)。有经验的开发人员会使用facade模式向客户机提供一个非常粗粒度的接口。然后,该外观将依次对不同的更细粒度的方法执行多个调用。总之,这大大减少了所需的方法调用数量并提高了性能。适配器:
public class AdapterDemo{
public static void main(String args[]){
SquareArea s = new SquareArea(4);
System.out.println("Square area :"+s.getArea());
}
}
class RectangleArea {
public int getArea(int length, int width){
return length * width;
}
}
class SquareArea extends RectangleArea {
int length;
public SquareArea(int length){
this.length = length;
}
public int getArea(){
return getArea(length,length);
}
}
桥接器:
Gear gear = new ManualGear();
Vehicle vehicle = new Car(gear);
vehicle.addGear();
gear = new AutoGear();
vehicle = new Car(gear);
vehicle.addGear();
相关职位:
主要差异:来自文章
Adapter | Bridge
-----------|---------------
Target | Abstraction
-----------|---------------
| RefinedAbstraction
|
| This element is Bridge specific. If there is a group of
| implementations that share the same logic, the logic can be placed here.
| For example, all cars split into two large groups: manual and auto.
| So, there will be two RefinedAbstraction classes.
-----------|---------------
Adapter | Implementor
-----------|---------------
Adaptee | ConcreteImplementor
//---------------------------------------External Vendor/Provider--------------------------------
//Adaptee | RussianTankInterface is adaptee | adaptee lives in is own lala land and do not care about any other class or interface
RussianTankInterface smerch9K58 = new RussianTank("The Russian Artillery bought by India in October 2015");
smerch9K58.aboutMyself();
smerch9K58.stuff();
smerch9K58.rotate();
smerch9K58.launch();
//---------------------------------2016 : India manufactures Bharat52 ------------------------------
//Client_1 :: IndianTank
EnemyAttacker bharat52Attacker = new IndianTank("Tank built in India delivered to Army in Jul 2016");
// behaves normally -------------------------(1)
bharat52Attacker.aboutMe();
bharat52Attacker.load();
bharat52Attacker.revolve();
bharat52Attacker.fireArtillery();
//---------------------------------2019 : India mnufactures Pinaka, and thought about fusion with Russian technology - so adaption required ------------------------------
//Client_2 :: IndianTank
EnemyAttacker pinakaAttacker = new IndianTank("Tank built in India in 1998 got upgraded_v1 in 9 Sep 2019");
#####----Bilateral-Coalition happens----##
##### India : I want a fusion artillery technology with
##### 1) Indian materials and brain-power but
##### 2) Russian machine-parts-movement technology
##### Russia : Give me your Interface - at max we can help by providing an Adapter
//---------------------------------------External Vendor/Provider-----------------------------------
//Adapter :: RussianTechnologyAdapter | Russia gets EnemyAttacker interface only from India & creates RussianTechnologyAdapter
RussianTechnologyAdapter russianTechnologyAdapter = new RussianTechnologyAdapter(smerch9K58);
//Target | EnemyAttacker was initially ClientInterface but later becomes the Target as story evolves | <- client owns this Interface
EnemyAttacker dhanushAttacker = russianTechnologyAdapter;
#####----Russia keeps her Word----##
##### Russia to India : Here you go! Take Dhanush, a wrapper over our encapsulated adapter, and plug-in anything conforming to your EnemyAttacker.
##### India : Thanks a lot!
//--------------------------------- 2020 : India returns back happily with dhanushAttacker---------------------------------------
//Client_2 - adapted behavior -------------------------(2)
dhanushAttacker.setNavigationCapability(pinakaAttacker.getCuttingEdgeNavigableTargets());
dhanushAttacker.aboutMe(); //calls RussianInstance -> aboutMyself()
dhanushAttacker.load(); //calls RussianInstance -> stuff()
dhanushAttacker.revolve(); //calls RussianInstance -> rotate()
dhanushAttacker.fireArtillery(); //calls RussianInstance -> launch()
- Invoker /Use(uses Adapter later point)/Client
- ClientInterface (a.k.a Target )
- ClientInterface ( becomes Target after sharing)
- Adapter (later shared directly or as a wrapper )
- Adaptee