天天看点

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