Three Categories of Design Patterns
The "Gang of Four" book on design patterns is a classic. They talk about three main types of design patterns:
1) Creational, 2) Structural, 3) and Behavioral.
So, Creational patterns are kind of like the blueprints for building objects. Imagine you're crafting something complex, and you want to simplify the process, make it more flexible, or even hide some of the details of the creation process. That's where Creational patterns come in. They help manage this whole "creation" saga.
Then there are Structural patterns. These are all about how objects and classes are composed or put together. Think of it like putting together a puzzle. Each piece has to fit in just the right way to create a bigger picture. Structural patterns help ensure that even if the pieces change, the overall structure remains stable and efficient.
And lastly, we've got Behavioral patterns. These focus on communication between objects. It's like they're concerned with how objects play nice and work together in a system. Behavioral patterns help manage the complexities of these interactions, making sure that the flow of communication and the responsibilities are well-organized.
Each of these patterns has its own set of specific patterns that deal with particular problems. It's pretty fascinating how they categorize and solve different software design challenges.
Behavioral Patterns: Characteristics and Overview in the Context of the Gang of Four Patterns
In the realm of software design, the Gang of Four (GoF) patterns stand as a seminal work, offering 23 design patterns categorized into three groups: Creational, Structural, and Behavioral. Among these, Behavioral Patterns focus on the communication between objects, ensuring efficient and flexible interactions.
- Fundamental Characteristics of Behavioral Patterns:
- Object Interactions: Behavioral Patterns primarily address the responsibilities of objects and how they communicate. They ensure that objects collaborate effectively, distributing responsibilities among them.
- Decoupling: One of the primary goals of Behavioral Patterns is to decouple classes, ensuring that interacting objects remain loosely coupled. This decoupling fosters flexibility in the system, allowing for easier modification and extension.
- Responsibility Chains: These patterns often define how requests are passed between a chain of objects, ensuring that the request is handled appropriately without explicitly specifying the receiver.
- Centralized Control: Some Behavioral Patterns centralize control in a single object, which manages the logic and coordination for the entire system.
- Flexibility in Object Interactions: Behavioral Patterns provide solutions that allow for dynamic changes in the interactions between objects, ensuring adaptability in the face of changing requirements.
- Notable Behavioral Patterns To provide a clearer understanding, here are some of the key Behavioral Patterns as defined by the Gang of Four:
- Observer: Defines a dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.
- Mediator: Encapsulates how a set of objects interact, promoting loose coupling by keeping objects from referring to each other explicitly.
- Command: Turns a request into a stand-alone object that contains information about the request, allowing for parameterization of clients with different requests, queuing of requests, and logging of operations.
- Strategy: Defines a family of algorithms, encapsulates each one, and makes them interchangeable, allowing the algorithm to vary independently from clients that use it.
- State: Allows an object to alter its behavior when its internal state changes, making it appear as though the object's class has changed.
- Template Method: Defines the skeleton of an algorithm in a method in an algorithm class but delays some steps to subclasses, allowing subclasses to redefine certain steps of an algorithm without changing the algorithm's structure.
- Iterator: Provides a way to access the elements of an aggregate object sequentially without exposing its underlying representation.
- Chain of Responsibility: Decouples the sender from the receiver by allowing more than one object to handle a request. The request is passed along a chain until an object handles it or it reaches the end of the chain.
- Visitor: Represents an operation to be performed on elements of an object structure, allowing for new operations to be added without modifying the classes of the elements on which they operate.
- Memento: Captures and externalizes an object's internal state without violating encapsulation, allowing the object to be restored to this state later.
Behavioral Patterns, as delineated by the Gang of Four, offer a structured approach to handle interactions and communications between objects. By focusing on the distribution of responsibilities and ensuring that objects remain loosely coupled, these patterns provide solutions that enhance flexibility, adaptability, and maintainability in software design.
Behavioral patterns describe interactions between objects. They focus on how objects communicate with each other.
Behavioral object patterns use object composition rather than inheritance. Some describe how a group of peer objects cooperate to perform a task that no single object can carry out by itself. An important issue here is how peer objects know about each other. Peers could maintain explicit references to each other, but that would increase their coupling. In the extreme, every object would know about every other.
pattern avoids this by introducing a mediator object between peers. The mediator provides the indirection needed for loose coupling.
Make objects of the right class for a problem, generally when instances of several different classes are available.
There are two recurring themes in creational patterns. First, they all encapsulate knowledge about which concrete classes the system uses.
Second, they hide how instances of these classes are created and put together.
All the system at large knows about the objects is their interfaces as defined by abstract classes. Consequently, the creational patterns give you a lot of flexibility in what gets created, who creates it, how it gets created, and when.
They let you configure a system with "product" objects that vary widely in structure and functionality. Configuration can be static (that is, specified at compile-time) or dynamic (at run-time).
You know those design patterns that are all about the "birth" of an object? That's what we are talking about with creational design patterns. Their whole gig is to keep the how-to of making an object under wraps, so what you get is this neat, ready-to-use instance without all the behind-the-scenes mess. Think of it like a magician pulling a rabbit out of a hat, where you get the magic without seeing the prep work. Now, there are five big players in this world: Abstract Factory, Builder, Factory Method, Prototype, and Singleton.
Ad Design Patterns Explained
Form larger structures from individual parts, generally of different classes. Rather than composing interfaces or implementations, structural object patterns describe ways to compose objects to realize new functionality. The added flexibility of object composition comes from the ability to change the composition at run-time, which is impossible with static class composition.
Structural design patterns examine how objects and classes are composed to form larger structures. In class structural design, new structures are created through multiple inheritance. One class inherits from more than a single parent class to create a new structure. More commonly, object structures combine different objects to form new structures. The following seven patterns have been designated as structural.
- Adapter (class and object)