Design Patterns | Fundamentals
Hello Folks! ππΌ
I’ve decided to embark on a new article series, and I’m going to tackle Design Patterns. Combining the notes I’ve taken from the articles I’ve read and watched while learning Design Patterns, I’ve decided to create a perpetual resource on my blog.
I’m planning to divide this article series into four parts, where I’ll delve into each category and pattern in detail, accompanied by examples. Throughout the examples, I’ll be using Java as the programming language.
The article series will progress in the following order, as I anticipate:
- Design Pattern Fundamentals
- Creational Design Patterns
- Structural Design Patterns
- Behavioral Design Patterns
We’ll start with this article to provide a nice introduction and gain a general overview and understanding of Design Pattern concepts. In the subsequent articles, we’ll meticulously delve into each remaining part separately.
Without further ado, let’s dive into our first topic! π€πΌ
Β
Design Patterns, in the realm of software development, hold a significant place as the cool-sounding name for a set of standardized software designs that developers employ to tackle recurring problems, create more flexible, sustainable, and reusable code in their projects.
These designs, comprising numerous different patterns, are categorized into three distinct groups based on their usage and purposes, as outlined below.
Let’s now roughly delve into the realm of Creational Design Patterns, which will be one of the pillars of our article series, and try to grasp what they are all about.π
Creational Design Patterns
Creational Design Patterns encompass a category of design patterns that revolve around the process of creating new objects. In this category, the patterns focus on how an object can be created in a flexible and reusable manner.
βIn software engineering, creational design patterns are design patterns that deal with object creation mechanisms, trying to create objects in a manner suitable to the situation. The basic form of object creation could result in design problems or added complexity to the design. Creational design patterns solve this problem by somehow controlling this object creation.β (1)
We’ve covered it quite extensively in the previous lines, but if we were to highlight them in bullet points, these design patterns address the following concerns:
- How objects are created.
- Managing the processes involved in creating large and complex objects.
- Defining the requirements during the creation of objects.
- Determining which objects to use in the object’s creation.
Within this category, we’ll explore 6 different patterns. Now, let’s take a quick peek at them.π§
- Singleton Pattern: It is used to design a class with only one instance.
- Factory Method Pattern: It allows subclasses to use the interface of the parent class to create objects.
- Abstract Factory Pattern: It enables the creation of objects in a group that are related to each other.
- Builder Pattern: It allows the step-by-step construction of complex objects.
- Prototype Pattern: It is used to create copies of objects.
As mentioned at the beginning of the article, we briefly covered each pattern. Now, let’s take a look at our next category, Structural Design Patterns. π±βπ
Structural Design Patterns
The patterns within this category determine how the different components of software will:
- Come together,
- Get organized
- Interact with each other.
These design patterns are in line with the principles of object-oriented programming (OOP) and help optimize the interaction between the various components of the software.
Structural design patterns explain how to assemble objects and classes into larger structures, while keeping these structures flexible and efficient. (2)
Structural Design Patterns allow for easier understanding and more convenient maintenance of the code and the resulting structure. These design patterns, especially during the development of large and complex software, provide developers with opportunities to better organize and reuse their code.
Within the Structural Pattern category, there are 7 different design patterns.
These include:
- Adapter Pattern: It is a structural design pattern that enables two classes with different interfaces to work together harmoniously.
- Bridge Pattern: It is a design pattern that separates the abstraction and implementation, allowing them to vary independently.
- Composite Pattern: It is a design pattern that creates objects in a hierarchical structure, enabling operations to be performed on the entire structure instead of individually handling each object.
- Decorator Pattern: It is a structural design pattern that dynamically adds new features to an existing object.
- Facade Pattern: It is a design pattern that simplifies a complex system by providing an interface for clients to interact with, making it easier for clients to use the system.
- Flyweight Pattern: It is a design pattern that prevents the repeated creation of similar objects, thereby improving system performance.
- Proxy Pattern: It is a design pattern that acts as a substitute for an object and controls access to that object.
Up until now, we have gained some understanding about two different categories and the 12 design patterns within them. At least now we’re not completely clueless! π
Now, let’s take a look at our final guest of this article, Behavioral Design Patterns.
Behavioral Design Patterns
Our pals in this design pattern category actually have similar goals. They assist in organizing interactions and responsibilities among objects.
Let’s dive into the various Behavioral Design Patterns and explore their roles.
Behavioral design patterns are concerned with algorithms and the assignment of responsibilities between objects. (3)
These patterns help make object behaviors more flexible and cohesive. They are used to facilitate easier maintenance and development of software.
Here are some important Behavioral Design Patterns:
- Observer Pattern: Establishes a relationship between objects, enabling automatic updates to dependent objects when one object changes.
- Command Pattern: Encapsulates a command-containing class, allowing separation of objects responsible for executing and parameterizing the command. This enables different ways of invoking and undoing the command.
- Iterator Pattern: Provides an interface for accessing elements of a collection sequentially, ensuring the access method remains consistent even if the collection’s structure changes.
- Template Method Pattern: Defines the steps of a process and allows subclasses to implement these steps as desired.
- Strategy Pattern: Encapsulates a class containing an algorithm, enabling different objects to apply the algorithm. This allows the algorithm to be applied in different ways and changed at runtime.
- Chain of Responsibility Pattern: Creates a chain of objects where each object is responsible for handling a specific request. The request is passed along the chain until it is handled.
- State Pattern: Used to manage an object’s state and change its behavior based on the current state.
- Interpreter Pattern: Provides an object structure for interpreting a specific part of a language and interpreting the data accordingly.
- Mediator Pattern: Instead of direct communication between objects, it uses a mediator object to facilitate communication and maintain independence among objects.
- Visitor Pattern: Provides an interface for accessing elements in an object structure and allows different objects to perform different operations on these elements.
These patterns contribute to creating more flexible and maintainable software systems by defining clear interactions and responsibilities among objects.
Let’s summarize it. We have three different categories of patterns at our disposal. These are:
- Creational
- Structural
- Behavioral
And each of these categories focuses on a different mechanism.
- Creational Patterns deal with the creation of objects.
- Structural Patterns address how objects will come together and be organized once they are created.
- Behavioral Patterns allow us to make adjustments regarding interactions and responsibilities among objects.
Thank you for taking the time to read through it. If you notice any missing or incorrect points, please feel free to let me know, and I’ll be happy to make the necessary corrections. I hope that the notes I have gathered here will be of some use to you and contribute to your work, even a little bit.
Happy Coding ππΌ
References
- Design Patterns. https://sourcemaking.com/design_patterns (1),(2)
- TasarΔ±m Desenleri, https://yusufyilmazfr.gitbook.io/tasarim-desenleri-turkce-kaynak/tasarim-desenleri/tasarim-deseni-nedir