Design patterns play a crucial role in enhancing code quality, maintainability, and scalability. One of the most influential collections of design patterns is the Gang of Four (GoF) patterns, encapsulated in the seminal book “Design Patterns: Elements of Reusable Object-Oriented Software.”
Table of Contents
What are Design Patterns?
Design patterns are proven solutions to common problems encountered in software design. They provide a structured approach to design and promote code reusability, flexibility, and maintainability. The GoF design patterns are particularly notable for their effectiveness in object-oriented design.
Categories of GoF Design Patterns
The GoF design patterns are categorized into three main groups based on their purpose and functionality:
- Creational Design Patterns: Creational patterns deal with object creation mechanisms, providing flexibility in how objects are instantiated.
- Structural Design Patterns: Structural patterns focus on class and object composition, facilitating the creation of larger structures while keeping them flexible and efficient.
- Behavioural Design Patterns: Behavioral patterns are concerned with communication and interaction between objects, defining how they collaborate to accomplish tasks.
Creational Design Pattern
Mnemonic: Pastry Builder
- p: prototype
- as: abstract factory
- try: factory
- Builder: builder
- Singleton
Singleton
Singleton is a creational design pattern that ensures a class has only one instance and provides a global point of access to that instance.
Builder
The builder design pattern is a creational design pattern that separates the construction of a complex object from its representation, allowing the same construction process to create various representations.
Prototype
The prototype design pattern is a creational design pattern that allows the creation of new objects by copying an existing object, known as a prototype, rather than creating new instances using a constructor.
Factory
Abstract Factory
Structural Design Pattern
Mnemonic: Fresh Frozen ABCD pastry
- fresh: facade
- frozen: fly weight
- a: adapter
- b: bridge
- c: composite
- d: decorator
- pastry: proxy
Adapter
Adapter pattern is a structural design pattern that allows incompatible interfaces to work together by providing a wrapper that converts one interface into another.
Bridge
The Bridge Design Pattern is a structural pattern that separates an object’s abstraction from its implementation, allowing them to vary independently.
Composite
Decorator
The decorator design pattern is a structural design pattern that allows behaviour to be added to individual objects, either statically or dynamically without affecting the behaviour of other objects from the same class.
Facade
Facade pattern is a structural design pattern that provides a simplified interface to a complex subsystem, making it easier to use.
Fly Weight
The flyweight design pattern is a structural design pattern that uses sharing to support large numbers of fine-grained objects efficiently.
Proxy
The proxy design pattern is a structural design pattern that acts as a placeholder for another object to control access to it
Behavioural Design Pattern
Mnemonic: 2 MICS On TV
- m: Memento
- m: Mediator
- i: Iterator
- i: Interpreter
- c: Command
- c: Chain of responsibility
- s: Strategy
- s: State
- o: Observer
- t: Template
- v: Visitor
Memento
The memento design pattern is a behaviour design pattern that allows you to capture and restore the internal state of an object without violating encapsulation.
Mediator
The mediator design pattern is a behaviour design pattern that promotes loose coupling among multiple objects by centralising their communication through a mediator object.
Iterator
The iterator design pattern is a behavioural design pattern that provides a way to access elements of a collection(or aggregation) sequentially without exposing the underlying representation of the collection.
Interpreter
The interpreter design pattern is a behavioural design pattern that is used to define a grammatical representation of a language and provides an interpreter to deal with this grammar
Command
The Command design pattern is a behavioural design pattern that encapsulates a request as an object, thereby allowing for the parameterization of clients with different requests, queue or log requests and supporting undoable operations.
Chain of responsibility
Chain of responsibility is a behavioural design pattern that allows an object to pass a request along a chain of potential handlers until the request is handled or reaches the end of the chain.
Strategy
The strategy design pattern is a behavioural design pattern that enables you to define a family of interchangeable algorithms or strategies and encapsulate each one as an object
State
The state design pattern is a behavioural design pattern that allows an object to change its behaviour when its internal state changes
Observer
The observer design pattern is a behaviour design pattern that establishes a one-to-many dependency between objects. When the state of one object changes. all its dependent objects are notified and updated automatically.
Template
The template method pattern is a behavioural design pattern that defines the outline or structure of an algorithm in a base class but delegates some steps of the algorithms to its base class
Visitor
The Visitor design pattern is a behavioural design pattern that allows you to add new behaviours to a group of classes without modifying their code. It provides a way to separate algorithms or operations from the object on which they operate.
Benefits of Using GoF Design Patterns
Incorporating GoF design patterns into software development offers several advantages:
- Code Reusability: Patterns provide reusable solutions to common problems, reducing redundant code and promoting modular design.
- Scalability: Patterns facilitate the creation of scalable architectures that can accommodate changes and extensions efficiently.
- Maintainability: By promoting clean and organized code, patterns contribute to easier maintenance and debugging.
- Design Clarity: Patterns improve design clarity by following established best practices and design principles.
Conclusion
The Gang of Four (GoF) design patterns are a cornerstone of object-oriented design, offering time-tested solutions to recurring software design challenges. By leveraging these patterns, developers can create robust, flexible, and maintainable software systems that stand the test of time.