Design patterns are divided into three fundamental groups:
- Behavioral,
- Creational, and
- Structural
- Behavioral Patterns: Behavioral patterns describe interactions between objects and focus on how objects communicate with each other. They can reduce complex flow charts to mere interconnections between objects of various classes. Behavioral patterns are also used to make the algorithm that a class uses simply another parameter that is adjustable at runtime. Behavioral patterns are concerned with algorithms and the assignment of responsibilities between objects. Behavioral patterns describe not just patterns of objects or classes but also the patterns of communication between them. These patterns characterize complex control flow that is difficult to follow at run-time. They shift your focus away from the flow of control to let you concentrate just on the way objects are interconnected. Behavioral class patterns use inheritance to distribute behavior between classes. The Template Method is the simpler and more common of the two. A template method is an abstract definition of an algorithm. It defines the algorithm step by step. Each step invokes either an abstract operation or a primitive operation. A subclass fleshes out the algorithm by defining the abstract operations. The other behavioral class pattern is Interpreter pattern, which represents a grammar as a class hierarchy and implements an interpreter as an operation on instances of these classes.
- Creational Patterns: Creational patterns are used to create objects for a suitable class that serves as a solution for a problem. Generally when instances of several different classes are available. They are particularly useful when you are taking advantage of polymorphism and need to choose between different classes at runtime rather than compile time. Creational patterns support the creation of objects in a system. Creational patterns allow objects to be created in a system without having to identify a specific class type in the code, so you do not have to write large, complex code to instantiate an object. It does this by having the subclass of the class create the objects. However, this can limit the type or number of objects that can be created within a system.
- Structural Patterns: Structural patterns form larger structures from individual parts, generally of different classes. Structural patterns vary a great deal depending on what sort of structure is being created for what purpose. Structural patterns are concerned with how classes and objects are composed to form larger structures. Structural class patterns use inheritance to compose interfaces or implementations. As a simple example, consider how multiple inheritance mixes two or more classes into one. The result is a class that combines the properties of its parent classes. This pattern is particularly useful for making independently developed class libraries work together.
Polymorphism and Design Patterns
Polymorphism [1]
allows instances of different classes to be used as an instance of a common
superclass[2]. For example, an
EncryptionStream
object might only have a reference to an abstract
Cipher
object. The type of encryption the
EncryptionStream
performed could be adjusted by changing from a
DESCipher
to an
RSACipher
. In cryptology, RSA is an algorithm for public-key cryptography. It was the first algorithm known to be suitable for signing as well as encryption, and one of the first great advances in public key cryptography. RSA is widely used in electronic commerce protocols, and is believed to be secure given sufficiently long keys and the use of up-to-date implementations.
Real World Example of Polymorphism:
- Example 1: A Teacher interacts with student. A Teacher interacts with his or her seniors.
Here teacher is an object but the attitude is different based on the situation.
- Example 2: Person interacts with genetic offspring in house at the same time that person interacts with an empoloyee in the office.
- Example 3: Your mobile phone, has one name of many forms:
- As phone
- As camera
- As mp3 player
- As radio
Another example is the class form of the
Adapter Pattern.
In general, an adapter makes one interface (the adaptee's) conform to another, thereby providing a uniform abstraction of different interfaces. A class adapter accomplishes this by inheriting privately from an adaptee class. The adapter then expresses its interface in terms of the adaptee's.
The link below will reinforce your knowledge of the three types of design patterns.
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.
The
Mediator pattern avoids this by introducing a mediator object between peers. The mediator provides the indirection needed for loose coupling.
Creational Patterns:
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
Structural Patterns:
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)
- Bridge
- Composite
- Decorator
- Façade
- Flyweight
- Proxy
[1]
Polymorphism: In design patterns, polymorphism allows objects of different classes to be treated as objects of a common type. This enables you to write code that can work with a variety of objects without knowing their exact class, promoting flexibility and maintainability.
[2]
superclass: A superclass is a class that has been extended by another class. It allows the extending class to inherit its state and behaviors.