天天看點

Design PatternsTools應用場景Dive Deep

文章目錄

  • Tools
    • UML
  • 應用場景
    • Factory Pattern
      • Prototype Pattern
      • Builder Pattern
    • Behavior Patterns
      • Iterator Pattern
      • Interpretor Pattern
      • 責任鍊模式
      • Observer Pattern
      • Command Patterns
      • Strategy Pattern / Policy Pattern
      • Visitor Pattern
    • Structural Pattern
      • Decorator Pattern
  • Dive Deep
    • Factory Pattern

Tools

UML

  • Use a umllab trial in eclipse
  • UML Lab
  • 關系圖總結
  • https://www.1point3acres.com/bbs/thread-557107-1-1.html
  • https://github.com/puncsky/system-design-and-architecture#%E7%B3%BB%E7%BB%9F%E8%AE%BE%E8%AE%A1%E4%B8%8E%E6%9E%84%E6%9E%B6—%E4%B8%AD%E6%96%87%E7%89%88

應用場景

tutorialpoint

23種設計模式

Factory Pattern

  • Belongs to creation pattern
  • In Factory pattern, we create object without exposing the creation logic to the client and refer to newly created object using a common interface.

ShapeFactory:

if type = a, create logic a

if type = b, create logic b

Prototype Pattern

文章

有點沒弄清楚

Builder Pattern

Behavior Patterns

Iterator Pattern

next()

hasNext()

Interpretor Pattern

責任鍊模式

靈活的if else語句

中文解釋

Observer Pattern

就是改動通知其他很多部件

中文解釋

Command Patterns

  • 基礎:

    https://python-3-patterns-idioms-test.readthedocs.io/en/latest/FunctionObjects.html

    就是把command封裝成object,然後你可以在執行的時候随意組合?

  • 應用場景:

    http://www.voidcn.com/article/p-umiulemc-rp.html

Strategy Pattern / Policy Pattern

In computer programming, the strategy pattern (also known as the policy pattern) is a behavioral software design pattern that enables selecting an algorithm at runtime. Instead of implementing a single algorithm directly, code receives run-time instructions as to which in a family of algorithms to use.

對于算法的封裝,例如排序,可以有多種排序方法,生成list,可以是ArrayList和linkedlist

Example:

https://blog.csdn.net/zhengzhb/article/details/7609670

Visitor Pattern

Resources:

講的非常清楚

Visitor Pattern in Python

Good example

Senario:

一個對象結構包含多個類型的對象,希望對這些對象實施一些依賴其具體類型的操作。在通路者中針對每一種具體的類型都提供了一個通路操作,不同類型的對象可以有不同的通路操作。

Type: Behavior Pattern

Structural Pattern

Decorator Pattern

https://python-3-patterns-idioms-test.readthedocs.io/en/latest/Decorator.html

做咖啡加奶加糖加奶油啥的,做披薩加什麼topping都是很好的例子

可以自由變更recipe

Dive Deep

Factory Pattern

When you discover that you need to add new types to a system, the most sensible first step is to use polymorphism to create a common interface to those new types. This separates the rest of the code in your system from the knowledge of the specific types that you are adding. New types may be added without disturbing existing code … or so it seems. At first it would appear that the only place you need to change the code in such a design is the place where you inherit a new type, but this is not quite true. You must still create an object of your new type, and at the point of creation you must specify the exact constructor to use. Thus, if the code that creates objects is distributed throughout your application, you have the same problem when adding new types-you must still chase down all the points of your code where type matters. It happens to be the creation of the type that matters in this case rather than the use of the type (which is taken care of by polymorphism), but the effect is the same: adding a new type can cause problems.

The solution is to force the creation of objects to occur through a common factory rather than to allow the creational code to be spread throughout your system. If all the code in your program must go through this factory whenever it needs to create one of your objects, then all you must do when you add a new object is to modify the factory.

Since every object-oriented program creates objects, and since it’s very likely you will extend your program by adding new types, I suspect that factories may be the most universally useful kinds of design patterns.

大類下面的小類有相同的function,根據小類,function有所不同

https://python-3-patterns-idioms-test.readthedocs.io/en/latest/Factory.html