📌
デザインパターンまとめ
GoF(Gang of Four)デザインパターンについて学んだことをメモします。順次追加更新します。
GoFデザインパターンとは
「Design Patterns: Elements of Reusable Object-Oriented Software」という著書で紹介された以下の3つのカテゴリーに分けて定義されたデザインパターンのこと。
- 🔴生成に関するパターン
• Abstract Factory
• Builder
• Factory Method
• Prototype
• Singleton - 🔵構造に関するパターン
• Adapter
• Bridge
• Composite
• Decorator
• Façade
• Flyweight
• Proxy - 🟢振舞いに関するパターン
• Chain of Responsibility
• Command
• Interpreter
• Iterator
• Mediator
• Memento
• Observer
• State
• Strategy
• Template Method
• Visitor
生成に関するパターン
🔴Factory Method
🔴Singleton
構造に関するパターン
🔵Composite
抽象クラス
component
具象クラス
composite
leaf
クライアントを分かりやすくすることが出来る
(出典: https://en.wikipedia.org/wiki/Composite_pattern)
木構造で表され、以下の3つの要素を持つ
- composite: 子オブジェクトを持ち、子に対するメソッドもある
- leaf:子オブジェクトはなく、メソッドのみ
- component: composite,leafのインターフェイスで全てcomposite,leafに共通の振る舞いを定義
clientはleafとcompositeどちらを扱っているのか気にする必要はなく、rootに存在するcompositeを操作するだけで良い。そのため、以下のメリットがある。
⭕️ 新たなcomponent(composite,leaf)の追加が容易
→clientの仕様を変える必要がない
❌ 設計を過度に一般化してしまう恐れがある
振舞いに関するパターン
🟢Observer
抽象クラス
Subject
Observer
具象クラス
ConcreteSubject
ConcreteObserver
Discussion