1. 程式人生 > 其它 >代理,裝飾,適配,橋接,面孔的區別

代理,裝飾,適配,橋接,面孔的區別

代理,裝飾,適配,橋接,面孔的區別

  • 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.
    • 一個或多個類的子系統組成的高階介面。假設你又一個複雜的概念,需要多個多想來表示他。對這組物件進行更改是令人困惑的,因為你不知道你需要呼叫哪一個方法。這個時候使用面孔模式,它為您可以對物件集合的所有複雜操作提供了一個高階方法。

可以理解為除了橋接以外,其他模式都善於處理客戶端程式碼到非我寫的程式碼之間的關係。