A software design pattern is a general, reusable solution to a commonly occuring problem within given context in software design. It is not a finished design, it is a description or template for how to solve a problem that can be used in many different situations.
Why Design Patterns?
- Improve maintability
- Decrease dependencies
Creating objects in a manner suitable for the situation.
- Factory Method
- Singleton
Ease the design by identifying a simple way to realize relationships between entities.
- Decorator
- Adapter
- Facade
Common communication patterns between objects.
- Strategy
- Observer
- Command
- Template Method
I used Head First Design Patterns by Eric Freeman and Elisabeth Robson in Java book as reference and implemented them in Swift.
- Defines a family of algorithms
- Encapsulates each one
- Makes them interchangable objects that can be set or switched at runtime
- Has three parts: an object using a strategy, a strategy protocol, and a family of strategy objects.
- Subject, updates Observers using a common interface
- Defines one-to-many relationship between set of objects
- When the state of one object changes, all of its dependents are notified
- Observers are loosely coupled in that the Subject knows nothing about them
- Attaches additional responsibilities to an object dynamically
- Defines an interface for creating an object, but lets the subclasses decide
- It should just return a concrete product
- Ensures a class has only one instance and provides a global point of access to it.
- Encapsulates each request as an object
- Removes dependency between receiver and invoker
- Converts the interface of a class into another interface the clients expect.
- Provides a unified interface to a set of interfaces in a subsystem
- Defines a higher level interface that makes the subsystem easier to use
- Defines the skeleton of an algorithm in a method, deferring some steps to subclasses.