1. 程式人生 > >java幾個設計模式之間的區別

java幾個設計模式之間的區別

Q1: What is the difference between State and Strategy patterns? {#Q1}

狀態模式和策略模式之間有什麼區別

While the implementation is similar they solve different problems. The State pattern deals with what state an object is in - it encapsulates state-dependent behavior. The Strategy pattern deals with how an object performs a certain task - it encapsulates an algorithm.

雖然這兩種設計模式實現非常類似,但是他們解決了不同的問題,狀態模式處理物件所處的狀態,封裝了依賴於狀態的行為, 策略模式處理物件如何執行特定的任務,它封裝的是演算法

Q2: What is the difference between Strategy and Template Method patterns? {#Q2}

策略模式和模板方法模式的區別

In Template Method the algorithm is chosen at compile time via inheritance. With Strategy pattern the algorithm is chosen at runtime via composition.

在模板方法中,演算法是在編譯時通過繼承選擇的。使用策略模式,演算法在執行時通過組合選擇。

Q3: What is the difference between Proxy and Decorator patterns? {#Q3}

代理模式和裝飾模式有什麼區別

The difference is the intent of the patterns. While Proxy controls access to the object Decorator is used to add responsibilities to the object.

不同之處在於模式的意圖。 代理模式控制物件訪問許可權,裝飾模式用於向物件新增職責。

Q4: What is the difference between Chain of Responsibility and Intercepting Filter patterns? {#Q4}

責任鏈和攔截過濾器模式之間有什麼區別?

While the implementations look similar there are differences. The Chain of Responsibility forms a chain of request processors and the processors are then executed one by one until the correct processor is found. In Intercepting Filter the chain is constructed from filters and the whole chain is always executed.

雖然實現看起來相似但存在差異。 責任鍊形成了一系列請求處理器,然後是處理器逐個執行,直到找到正確的處理器。 在攔截 過濾鏈由過濾器構成,整個鏈條始終如一執行。