Loading

代理,装饰,适配,桥接,面孔的区别

代理,装饰,适配,桥接,面孔的区别

  • 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.
    • 一个或多个类的子系统组成的高级接口。假设你又一个复杂的概念,需要多个多想来表示他。对这组对象进行更改是令人困惑的,因为你不知道你需要调用哪一个方法。这个时候使用面孔模式,它为您可以对对象集合的所有复杂操作提供了一个高级方法。

原文

可以理解为除了桥接以外,其他模式都善于处理客户端代码到非我写的代码之间的关系。

posted @ 2022-12-05 17:07  尚墨  阅读(60)  评论(0编辑  收藏  举报