Skip to content

Learning design patterns with golang, node.js, python...

Notifications You must be signed in to change notification settings

nongod/design-pattern-learning

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

design-pattern-learning

Learning design patterns with golang, node.js, python...

Menu

Languages

  • Go
  • Node.js
  • Python
  • TypeScript (need to learn...)

Start up

develop code base on docker, so you need to install Docker first.

Go

  1. change workdir to go
  2. run command ./dev

Node JS

  1. change workdir to nodejs
  2. build docker image docker build -t design-pattern:node .
  3. run command ./dev

Python

  1. change workdir to python
  2. build docker image docker build -t design-pattern:python .
  3. run command ./dev

TypeScript

  1. change workdir to typescript
  2. build docker image docker build -t design-pattern:typescript .
  3. run command ./dev

Design Patterns

Creational patterns

Abstract factory

Abstract factory pattern 是 Factory method 的延伸應用,既然創建 class 需要用到 factory pattern,那麼取得 factory 是不是也可以抽象出一個 factory 來創建呢? 所以 Abstract factory 亦被稱為 工廠的工廠,這時又會遇見 simple factory 提到 OO 的 Open-Closed Principle,這時部分 Language 可以使用 reflect 這個技巧,如果存在這個 factory 就將他給創建出來。

Abstract factory User case

使用 abstract factory 建立食物烹煮工廠,讓廚師可以製作出飲料、烤物、燉煮類食物。

┌──────┐     ┌───────────────────────────────────────────┐
│ Main │┄┄┄┄⇢│ AbstractFactory                           │
└──────┘     ├───────────────────────────────────────────┤
 ┆           ├───────────────────────────────────────────┤┄┐ Dependency
 ┆           │+ getFactory(factoryName: String): Factory │ ┆
 ┆           └───────────────────────────────────────────┘ ┆
 ┆                                                         ┆
 ┆    ┌────────────────────────┐                           ┆  ┌──────────────────────────────────────┐
 ┆    │ Factory                │     Inheritance           ├┄⇢│ BBQFactory                           │
 ├┄┄┄⇢├────────────────────────┤⇽────────────────────┬─────┼──┼──────────────────────────────────────┼┄┄┬┄┄┄┄┄┄┄┄┄┄┐
 ┆    ├────────────────────────┤                     │     ┆  │+ getFood(foodName: String): Food     |  ┆          ┆
 ┆    └────────────────────────┘                     │     ┆  └──────────────────────────────────────┘  ┆          ┆
 ┆                                                   │     ┆                                            ┆          ┆
 ┆                                                   │     ┆  ┌──────────────────────────────────────┐  ┆          ┆
 ┆                                                   │     ├┄⇢│ StewFactory                          │  ┆          ┆
 ┆                                                   ├─────┼──┼──────────────────────────────────────┼┄┄┼┄┄┄┄┄┄┄┄┄┐┆
 ┆                                                   │     ┆  │+ getFood(foodName: String): Food     |  ┆         ┆┆
 ┆                                                   │     ┆  └──────────────────────────────────────┘  ┆         ┆┆
 ┆                                                   │     ┆                                            ┆         ┆┆ Dependency
 ┆                                                   │     ┆  ┌──────────────────────────────────────┐  ┆         ┆┆
 ┆                                                   │     └┄⇢│ DrinkFactory                         │  ┆         ┆┆
 ┆                                                   └────────┼──────────────────────────────────────┤┄┄┼┄┄┄┄┄┄┄┄┐┆┆
 ┆                                                            │+ getFood(foodName: String): Food     |  ┆        ┆┆┆
 ┆                                                            └──────────────────────────────────────┘  ┆        ┆┆┆
 ┆                       ┌┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┘        ┆┆┆
 ┆                       ⇣                 Dependency                                                            ┆┆┆
 ┆    ┌──────────────────────────────────────┐                                                                   ┆┆┆
 ┆    │ Food                                 │                                                                   ┆┆┆
 ┆    ├──────────────────────────────────────┤                        ┌──────────────────────────────────────┐   ┆┆┆
 ┆    │- name                                │       Inheritance      │ BBQ                                  │   ┆┆┆
 └┄┄┄⇢├──────────────────────────────────────┤⇽──────────────┬────────┼──────────────────────────────────────┤⇽┄┄┼┼┘
      │+ constructor(foodName: String): void │               │        │+ constructor(foodName: String): void │   ┆┆
      │+ getName(): String                   │               │        └──────────────────────────────────────┘   ┆┆
      └──────────────────────────────────────┘               │                                                   ┆┆
                                                             │        ┌──────────────────────────────────────┐   ┆┆
                                                             │        │ Stew                                 │   ┆┆
                                                             ├────────┼──────────────────────────────────────┤⇽┄┄┼┘
                                                             │        │+ constructor(foodName: String): void │   ┆
                                                             │        └──────────────────────────────────────┘   ┆
                                                             │                                                   ┆
                                                             │        ┌──────────────────────────────────────┐   ┆
                                                             │        │ Drink                                │   ┆
                                                             └────────┼──────────────────────────────────────┤⇽┄┄┘
                                                                      │+ constructor(foodName: String): void │
                                                                      └──────────────────────────────────────┘

Builder

Dependency injection

Factory method

Factory pattern 將實體化 class 這個動作封裝成另一個 factory class,專門 new 這些 class,可以使用傳入參數決定要創建哪個實例,在 simple factory 使用 condition 決定要創建哪個實例,但若要添加或刪減創建 class 的種類,需要更動 condition,違反了 OO 的 Open-Closed Principle,為了解決 simple factory 的耦合問題,factory method 將每個要創建的實例都設立一個 factory class,創建實例時只需使用相對應得 factory 來執行創建。

Factory method User case

使用 factory method 建立食物烹煮工廠,讓廚師可以製作出飲料、烤物、燉煮類食物。

┌──────┐
│ Main │┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┐ Dependency
└──────┘                                                   ┆
 ┆                                                         ┆
 ┆    ┌────────────────────────┐                           ┆  ┌──────────────────────────────────────┐
 ┆    │ Factory                │     Inheritance           ├┄⇢│ BBQFactory                           │
 ├┄┄┄⇢├────────────────────────┤⇽────────────────────┬─────┼──┼──────────────────────────────────────┼┄┄┬┄┄┄┄┄┄┄┄┄┄┐
 ┆    ├────────────────────────┤                     │     ┆  │+ getFood(foodName: String): Food     |  ┆          ┆
 ┆    └────────────────────────┘                     │     ┆  └──────────────────────────────────────┘  ┆          ┆
 ┆                                                   │     ┆                                            ┆          ┆
 ┆                                                   │     ┆  ┌──────────────────────────────────────┐  ┆          ┆
 ┆                                                   │     ├┄⇢│ StewFactory                          │  ┆          ┆
 ┆                                                   ├─────┼──┼──────────────────────────────────────┼┄┄┼┄┄┄┄┄┄┄┄┄┐┆
 ┆                                                   │     ┆  │+ getFood(foodName: String): Food     |  ┆         ┆┆
 ┆                                                   │     ┆  └──────────────────────────────────────┘  ┆         ┆┆
 ┆                                                   │     ┆                                            ┆         ┆┆ Dependency
 ┆                                                   │     ┆  ┌──────────────────────────────────────┐  ┆         ┆┆
 ┆                                                   │     └┄⇢│ DrinkFactory                         │  ┆         ┆┆
 ┆                                                   └────────┼──────────────────────────────────────┤┄┄┼┄┄┄┄┄┄┄┄┐┆┆
 ┆                                                            │+ getFood(foodName: String): Food     |  ┆        ┆┆┆
 ┆                                                            └──────────────────────────────────────┘  ┆        ┆┆┆
 ┆                       ┌┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┘        ┆┆┆
 ┆                       ⇣                 Dependency                                                            ┆┆┆
 ┆    ┌──────────────────────────────────────┐                                                                   ┆┆┆
 ┆    │ Food                                 │                                                                   ┆┆┆
 ┆    ├──────────────────────────────────────┤                        ┌──────────────────────────────────────┐   ┆┆┆
 ┆    │- name                                │       Inheritance      │ BBQ                                  │   ┆┆┆
 └┄┄┄⇢├──────────────────────────────────────┤⇽──────────────┬────────┼──────────────────────────────────────┤⇽┄┄┼┼┘
      │+ constructor(foodName: String): void │               │        │+ constructor(foodName: String): void │   ┆┆
      │+ getName(): String                   │               │        └──────────────────────────────────────┘   ┆┆
      └──────────────────────────────────────┘               │                                                   ┆┆
                                                             │        ┌──────────────────────────────────────┐   ┆┆
                                                             │        │ Stew                                 │   ┆┆
                                                             ├────────┼──────────────────────────────────────┤⇽┄┄┼┘
                                                             │        │+ constructor(foodName: String): void │   ┆
                                                             │        └──────────────────────────────────────┘   ┆
                                                             │                                                   ┆
                                                             │        ┌──────────────────────────────────────┐   ┆
                                                             │        │ Drink                                │   ┆
                                                             └────────┼──────────────────────────────────────┤⇽┄┄┘
                                                                      │+ constructor(foodName: String): void │
                                                                      └──────────────────────────────────────┘

Lazy initialization

Multiton

Object pool

Prototype

Resource acquisition is initialization (RAII)

Singleton

Structural patterns

Adapter, Wrapper, or Translator

Bridge

Composite

Decorator

Extension object

Facade

Flyweight

Front controller

Marker

Module

Proxy

Twin

Behavioral patterns

Blackboard

Chain of responsibility

Command

Interpreter

Iterator

Mediator

Memento

Null object

Observer or Publish/subscribe

Servant

Specification

State

Strategy

Template method

Visitor

Concurrency patterns

Active Object

Balking

Binding properties

Compute kernel

Double-checked locking

Event-based asynchronous

Guarded suspension

Join

Lock

Messaging design pattern (MDP)

Monitor object

Reactor

Read-write lock

Scheduler

Thread pool

Thread-specific storage

Use draw symbol

┼ ┴ ┬ ┤ ├ ─ │ ┌ ┐ └ ┘

About

Learning design patterns with golang, node.js, python...

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published