What is a pattern? Simply put, a pattern is a solution to a problem in a context. Note the context should be a recurring situation. Patterns are a collection of wisdom to solve problems.
Review
Let's take a brief review of what we have learnt so far:
- Decorator: wraps an object to provide new behavior
- State: encapsulates state-based behaviors and uses delegation to switch between behaviors.
- Iterator: provides a way to traverse a collection of objects without exposing its implementation.
- Facade: simplifies the interface of a set of classes.
- Strategy: encapsulates interchangeable behaviors and uses delegation to decide which one to use.
- Proxy: wraps an object to control access to it.
- Factory method: allows a client to create families of objects without specifying their concrete classes.
- Adapter: wraps an object and provides a different interface to it.
- Observer: allows objects to be notifies when state changes.
- Template method: Subclasses decide how to implement steps in an algorithm.
- Composite: Clients treat collections of objects and individual objects uniformly.
- Singleton: ensures one and only one object is created.
- Abstract factory: allows a client to create families of objects without specifying their concrete classes.
- Command: encapsulates a request as an object.
There are more patterns for sure, but they are probably the most popular ones.
Category of Patterns
There are many ways to classify patterns. Understanding the categories help you memorize them and communicate them better.
One way is to partition patterns into creational, behavioral, structural pattern.
- Creational Pattern: involves object instantiation. Provides a way to decouple a client from the object it needs to instantiate.
- Behavioral Pattern: is concerned with how classes and objects interact and distribute responsibilities.
- Structural Pattern: lets you compose classes or objects into larger structures.
Another way is to classify them into class patterns and object patterns.
- Class Patterns describe how relationships between classes are defined via inheritance. Relationships in class patterns are established at compile time.
- Object Patterns describe relationships between objects and are primarily defined by composition. Relationships in object patterns are typically created at runtime and are more dynamic and flexible.
Remarks
- Do not use patterns just to use patterns. Always prefer simplicity (KISS: keep it simple). If a problem could be solved without using patterns, prefer easier solution. Only use patterns whenever you see benefits (e.g. You see a practical change might happen, which could be solved by using patterns).
- Refactoring is usually patterns time. e.g. Many conditional statements may hint for State pattern.
- Patterns are a common vocabulary that accelerate communication among developers.
Anti-patterns
Anti-patterns tell you a bad solution to a problem. Why it could be tempting. And what should be the right direction.
See anti-patterns.
Top comments (0)