我正在查看代理模式,对我来说,它看起来非常像装饰器、适配器和桥接模式。我是不是误解了什么?有什么不同?为什么我要使用代理模式而不是其他模式?在过去的实际项目中,您是如何使用它们的?


当前回答

所有这四种模式都涉及到用外部对象/类包装内部对象/类,因此它们在结构上非常相似。我将通过目的来概述不同之处:

代理将访问从外部封装到内部。 装饰器用外部修改或扩展内部的行为。 适配器转换接口从内部到外部。 桥将行为的不变部分(外部)与变量或平台依赖部分(内部)分开。

通过内外物体之间的界面变化:

在代理接口中是相同的。 在Decorator接口中是相同的。 在适配器接口形式上不同,但实现相同的目的。 桥接接口在概念上是不同的。

其他回答

代理、装饰器、适配器和桥接器都是“包装”类的变体。但它们的用途不同。

Proxy could be used when you want to lazy-instantiate an object, or hide the fact that you're calling a remote service, or control access to the object. Decorator is also called "Smart Proxy." This is used when you want to add functionality to an object, but not by extending that object's type. This allows you to do so at runtime. Adapter is used when you have an abstract interface, and you want to map that interface to another object which has similar functional role, but a different interface. Bridge is very similar to Adapter, but we call it Bridge when you define both the abstract interface and the underlying implementation. I.e. you're not adapting to some legacy or third-party code, you're the designer of all the code but you need to be able to swap out different implementations. Facade is a higher-level (read: simpler) interface to a subsystem of one or more classes. Suppose you have a complex concept that requires multiple objects to represent. Making changes to that set of objects is confusing, because you don't always know which object has the method you need to call. That's the time to write a Facade that provides high-level methods for all the complex operations you can do to the collection of objects. Example: a Domain Model for a school section, with methods like countStudents(), reportAttendance(), assignSubstituteTeacher(), and so on.

Design pattern is not mathematics, it is combination of art and software engineering. There is nothing like for this requirment you have to use proxy, bridge etc. Design patterns are created to solve the problems. If you anticipate a design problem, then use it. Based on experience, you will come to know for specific problem, which pattern to use. If you are good in solid design principles, you would have implemented design pattern without knowing it is pattern. Common example is statergy and factory patterns

因此,更多地集中在坚实的设计原则,干净的编码原则和ttd

所有这四种模式都涉及到用外部对象/类包装内部对象/类,因此它们在结构上非常相似。我将通过目的来概述不同之处:

代理将访问从外部封装到内部。 装饰器用外部修改或扩展内部的行为。 适配器转换接口从内部到外部。 桥将行为的不变部分(外部)与变量或平台依赖部分(内部)分开。

通过内外物体之间的界面变化:

在代理接口中是相同的。 在Decorator接口中是相同的。 在适配器接口形式上不同,但实现相同的目的。 桥接接口在概念上是不同的。

所有来自专家的好答案都已经解释了每种模式代表什么。

我将装饰关键点。

装饰:

在运行时向对象添加行为。继承是实现此功能的关键,这是此模式的优点和缺点。 它修改了界面的行为。

例如(带链接):java。io包类与InputStream和OutputStream接口相关

FileOutputStream fos1 = new FileOutputStream("data1.txt");  
ObjectOutputStream out1 = new ObjectOutputStream(fos1);

代理:

使用它进行延迟初始化,通过缓存对象和控制对客户端/调用者的访问来提高性能。它可以提供替代行为或调用真实对象。在此过程中,它可能会创建新的Object。 与Decorator(允许对象链接)不同,Proxy不允许对象链接。

例如:java。Rmi包类。

适配器:

它允许两个不相关的接口通过不同的对象一起工作,可能扮演相同的角色。 对原有界面进行修改。

例如:java.io.InputStreamReader (InputStream返回Reader)

桥:

它允许抽象和实现独立变化。 它使用复合而不是继承。

例如,java.util中的集合类。由ArrayList实现的List。

关键笔记:

Adapter provides a different interface to its subject. Proxy provides the same interface. Decorator provides an enhanced interface. Adapter changes an object's interface, Decorator enhances an object's responsibilities. Decorator and Proxy have different purposes but similar structures Adapter makes things work after they're designed; Bridge makes them work before they are. Bridge is designed up-front to let the abstraction and the implementation vary independently. Adapter is retrofitted to make unrelated classes work together Decorator is designed to let you add responsibilities to objects without subclassing.

看看关于各种设计模式示例的优秀SE问题/文章

什么时候使用装饰器模式?

什么时候使用桥接模式?它与适配器模式有何不同?

代理模式和装饰模式的区别

在许多GoF模式中有大量的重叠。它们都建立在多态性的力量之上,有时只是意图不同。(战略vs.国家)

在阅读了头部优先设计模式之后,我对模式的理解增加了100倍。

我强烈推荐!