Skip to content

23 GoF Design Patterns

一. Creational Patterns

1. Factory Method Pattern

2. Abstract Factory Pattern

3. Singleton Pattern

4. Prototype Pattern

5. Builder Pattern:

6. Object Pool Pattern

二. Structural Patterns

1. Adapter Pattern

2. Bridge Pattern

3. Composite Pattern

4. Decorator Pattern

5. Facade Pattern

6. Flyweight Pattern

7. Proxy Pattern

三. Behavioral Patterns

1. Chain of Responsibility Pattern

2. Command Pattern

3. Iterator Pattern

4. Mediator Pattern

5. Memento Pattern

6. Observer Pattern

7. State Pattern

8. Visitor Pattern

9. Strategy Pattern

The strategy pattern allows you to define a family of algorithms, encapsulate each one of them, and make them interchangeable. This pattern lets the algorithms vary independently from clients that use it.

When to use the Strategy Design Pattern?

  1. Multiple Algorithms: You have multiple algorithms that can be used interchangeably based on different context, such as sorting algorithms.
  2. Encapsulating Algorithms: You want to seperate the implementation details of algorithms from the context uses them, allowing for easier maintenance, testing, and modification of alogrithms without affecting the client code.
  3. Runtime Selection: You need to dynamically select between different algorithms at runtime based on user preferences, configuration settings, or system state.
  4. Reducing Conditional Statements: When you have multiple conditional statements that choose bewteen different behaviours, using Strategy pattern helps in eliminating the need for conditional statements and making the code more modular and maintainable.

Strategy Patterns in iOS SDK

  1. requestCachePolicy on URLSession.
  2. Animation curves like .linear, .easeIn and .spring.
  3. AVCaptureDevice for microphone and cameras.

10. Template Method Pattern