Luna Tech

Tutorials For Dummies.

Design Patterns Overview(设计模式)

2021-05-29


0. 前言

Reference

本文主要讲了 design pattern 是什么以及怎么学,从哪些 design pattern 开始入手。


1. What are design patterns

A general, reusable solution to a common occurring problem within a given context.

更简单的说,就是 best practice of solving a common problem.

学了设计模式,就可以避免走一些弯路,直接用经过实践验证的方案来解决问题,是不是很棒!

但是初学者常犯的错误是,不管干啥都要往用上 design pattern,就是过度执着于「用设计模式」,而不是「解决当下的问题」。

Origin

1994 Book: Design Patterns: Elements of Reusable Object‑Oriented Software

也被称为 Gang of four(GoF) Design Patterns,因为这本书由四个作者合著。

这本书里面有 23 个 pattern。

The book established a language for describing object‑oriented software design patterns. It, too, organized these patterns by their type. The types included creational, structural, and behavioral.

人们也在不断识别新的 pattern。


2. Why we need to learn them

  1. Avoid to reinvent the wheels
  2. Communicate more effectively with other developers

大家都知道 pattern 是什么的时候,code review 就轻松多了。你直接可以说,这里需要改成 xx pattern,对方就能理解你的想法。


3. How to learn

T shape 学习法,先了解一个宽泛的大概框架,然后 go deep。

  1. 先了解一些 pattern,别人提到某个术语的的时候知道这是个 pattern;
  2. 然后看一些具体的 pattern 课程,深入了解实现方式,通过练习掌握几个 pattern;
  3. 你需要在工作中使用这些 pattern,并且能给别人解释清楚这个 pattern 到底是什么;
  4. 你不一定会用到或者掌握所有的 pattern,这没关系,但你至少要有几个熟悉的,让这些常用的 pattern 成为你的肌肉记忆,想都不用想就可以用。

4. What to learn

Pattern 的基本信息:

Pattern 的结构一般用 UML 的 class diagram、interaction diagram 或者 sequence diagram 来表示,我们从 diagram 里面可以看出 pattern 的参与对象、职责、以及对象之间的交互方式。

Note: 我们需要有能力看懂 UML diagram。

Pattern 也有 trade-off,所以我们需要知道用 pattern 可能造成的副作用。

接着,我们再学 pattern 的 implementation(用一个具体的程序语言),如何用 pattern 解决既定的问题。

我们可以通过学习 pattern 的常见使用场景来识别 real-world implementation。

不同的 Pattern 也经常会搭配使用,所以我们在学习 pattern 的时候也会看到相关 pattern。这些 pattern 的 diagram 可能长得一样,但是具体的使用场景和目的有所不同。

If you’re just aiming for a breadth of knowledge, the bare minimum amount that you should understand about a pattern in order to get to that awakening stage of learning, you should know the pattern’s names, both its primary name, as well as any aliases it might go by, you should know what its goal or intent is, and you should know at least some of the scenarios, situation, or context in which that pattern is appropriate. This is usually covered in the motivation and applicability section of the definition. This tells you what is the problem that it’s seeking to solve, and this will help you because then when you see that kind of problem, you’ll remember that there was a pattern that could be applied there.

Note: design pattern 要解决的问题往往是不符合 SOLID principles 的一些代码,所以我们可以先分析代码的问题(比如不符合哪条 SOLID),然后再去用相关的 pattern 解决。


5. How to apply

最好先自己做一些 coding 练习,不要直接用在生产环境,我们可以用 TDD,先写好 test cases,然后去 implement,看测试是否通过,对于一个问题,我们可以选择不同的 pattern 来尝试解决,然后对比不同的方案。

在生产环境中你可以用 pattern 来做 code refactoring,但一定要确保有足够的 test case coverage。

Code Kada

我们可以用 code kada(卡塔) 来做练习。

Code Kata is an attempt to bring this element of practice to software development. A kata is an exercise in karate where you repeat a form many, many times, making little improvements in each.

Example Kata

DesignPatternsInCSharp


6. Start with some good patterns

  1. Strategy
    • dependency injection
  2. Data access (Repository)
    • decouple system
    • can be used with other patterns (cached repository), such as decorator and strategy pattern, get huge performance wins
  3. Adapter
    • common pattern
  4. Factory
    • tightly related to the idea of an IoC container (a factory on steroids), IoC stands for inversion of control.
    • helps understand dependency injection and IoC container better
  5. Proxy and Decorator
    • common and powerful, easy to use with other patterns
    • these 2 patterns are closly related (structurally they are almost identical) but have different intents, the way we use them and the reason we use them differs
  6. Singleton
    • not covered in the Gang of Four Design Pattern book
    • some people think it’s an anti-pattern
    • we need to understand how to apply the singleton in the proper way because it’s easy to get wrong
    • we also need to know how to solve problems that singleton solves in other ways that might be more appropriate.

7. Info: Design Patterns

1. Behavioral

Behavioral design patterns are concerned with algorithms and the assignment of responsibilities between objects.

2. Creational

Creational patterns provide various object creation mechanisms, which increase flexibility and reuse of existing code.

3. Structural

Structural patterns explain how to assemble objects and classes into larger structures while keeping these structures flexible and efficient.