我正在查看代理模式,对我来说,它看起来非常像装饰器、适配器和桥接模式。我是不是误解了什么?有什么不同?为什么我要使用代理模式而不是其他模式?在过去的实际项目中,您是如何使用它们的?
当前回答
我想为Bill Karwing的回答补充一些例子(顺便说一句,这很好)。 我还补充了一些执行上的关键差异,我觉得这是我所缺失的
引用部分来自[https://stackoverflow.com/a/350471/1984346] (Bill Karwing)的回答
代理、装饰器、适配器和桥接器都是“包装”类的变体。 但它们的用途不同。 当您希望延迟实例化对象时,可以使用代理 隐藏您正在调用远程服务或控制访问的事实 对物体。
ProxyClass和被代理的ObjectClass应该实现相同的接口,因此它们是可交换的
示例- proxy昂贵对象
class ProxyHumanGenome implements GenomeInterface {
private $humanGenome = NULL;
// humanGenome class is not instantiated at construct time
function __construct() {
}
function getGenomeCount() {
if (NULL == $this->humanGenome) {
$this->instantiateGenomeClass();
}
return $this->humanGenome->getGenomeCount();
}
}
class HumanGenome implement GenomeInterface { ... }
Decorator也称为“智能代理”。这是在你需要的时候使用的 向一个对象添加功能,但不是通过扩展该对象的功能 类型。这允许您在运行时这样做。
DecoratorClass应该(可以)实现ObjectClass的扩展接口。所以ObjectClass可以被DecoratorClass替换,反之则不行。
示例-添加附加功能
class DecoratorHumanGenome implements CheckGenomeInterface {
// ... same code as previous example
// added functionality
public function isComplete() {
$this->humanGenome->getCount >= 21000
}
}
interface CheckGenomeInterface extends GenomeInterface {
public function isComplete();
}
class HumanGenome implement GenomeInterface { ... }
适配器用于当您有一个抽象接口,并且您想要 将该接口映射到具有类似功能的另一个对象 角色,但是不同的接口。
实现差异代理,装饰器,适配器
Adapter为其主题提供了不同的接口。Proxy提供相同的接口。Decorator提供了增强的接口。
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.
这个答案中的大部分信息来自https://sourcemaking.com/design_patterns,我推荐它作为设计模式的优秀资源。
其他回答
所有来自专家的好答案都已经解释了每种模式代表什么。
我将装饰关键点。
装饰:
在运行时向对象添加行为。继承是实现此功能的关键,这是此模式的优点和缺点。 它修改了界面的行为。
例如(带链接):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问题/文章
什么时候使用装饰器模式?
什么时候使用桥接模式?它与适配器模式有何不同?
代理模式和装饰模式的区别
所有这四种模式都涉及到用外部对象/类包装内部对象/类,因此它们在结构上非常相似。我将通过目的来概述不同之处:
代理将访问从外部封装到内部。 装饰器用外部修改或扩展内部的行为。 适配器转换接口从内部到外部。 桥将行为的不变部分(外部)与变量或平台依赖部分(内部)分开。
通过内外物体之间的界面变化:
在代理接口中是相同的。 在Decorator接口中是相同的。 在适配器接口形式上不同,但实现相同的目的。 桥接接口在概念上是不同的。
这是引用自 头部优先的设计模式
定义属于书。例子属于我。
装饰器——不改变界面,但增加了责任。假设你有一个汽车界面, 当您为不同型号的汽车(s, sv, sl)实现此功能时,您可能需要为某些型号添加更多的职责。比如有天窗,安全气囊等。
适配器—将一个接口转换为另一个接口。你有一个汽车界面,你想让它像吉普车一样工作。所以你把车开过来,改装成吉普车。因为这不是真正的吉普车。但它就像一辆吉普车。
Facade -使界面更简单。假设你有汽车、飞机、轮船接口。实际上,你所需要的只是一个将人从一个位置发送到另一个位置的类。你想让门面决定用什么车。然后将所有这些接口引用收集到一个保护伞下,并让它决定/委托以保持简单。
首先:“facade不仅简化了接口,还将客户端与子系统解耦 的组件。 外观和适配器可以包装多个类,但是外观的目的是简化 适配器的作用是将接口转换为不同的东西。”
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
它们非常相似,而且它们之间的线条是灰色的。我建议您阅读c2 wiki中的代理模式和装饰器模式条目。
那里的条目和讨论相当广泛,也链接到其他相关文章。顺便说一下,c2 wiki在了解不同模式之间的细微差别时非常有用。
总结c2项,我认为装饰器添加/更改行为,但代理与访问控制(延迟实例化,远程访问,安全等)有更多关系。但正如我所说,它们之间的界线是灰色的,我看到对代理的引用很容易被视为装饰器,反之亦然。