Design patterns are the ultimate toolkit for programmers. Furthermore, we use them to create maintainable software. This post goes through the design patterns and provides sample Java codes.
The Need For Design Patterns
Design patterns are known solutions to common programming problems. Although programming is an art, it should not always be the case. It is in the best interest of our stakeholders to craft maintainable codes to help them control costs. For example, programmers spend less time fixing bugs when the codes are maintainable. Enabling new features may require less time too. All these changes could happen at the same time while sustaining quality.
Three Types Of Design Patterns
Before we go through all the design patterns, it is best to organize our thought at the outset. First, there are three types or classes of design patterns. These are creational, behavioral, and structural.
Creational Design Patterns – Toolkit For Creating Objects
Creational patterns deal with creating objects. They create objects in a way that reduces complexity to the whole code design. So, why do we need them? We need them because the basic way of creating objects is terrible. It could make our codes complex and not flexible to change. For example, the following codes could negatively impact the overall code design.
1 | StudentRepository repo = new StudentRepositoryImpl(); |
For instance, the following design patterns fall under this type.
- Abstract Factory Pattern
- Builder Pattern – (Rust, Java, Go, Groovy)
- Singleton Pattern – (Rust, Java, Go)
- Prototype Pattern – (Rust, Java, Go)
- Factory Method Pattern
Structural Design Patterns – Toolkit For Organizing Codes
Structural design patterns deal with the organization of our codes. Moreover, the basis of the organization is often the relationship between entities or classes. In particular, these are the following structural design patterns:
- Adapter Pattern
- Bridge Pattern
- Composite Pattern – (Rust, Java, Go)
- Decorator Pattern – (Rust, Java, Go)
- Facade Pattern – (Rust, Java, Go)
- Flyweight Pattern – (Rust, Java, Go)
- Proxy Pattern – (Rust, Java, Go)
- Composition
Behavioral Design Patterns – Toolkit To Make Codes Communicate With Each Other
When we use behavioral design patterns, we design codes that allow for flexible communication among objects. For example, these are the following behavioral design patterns.
- Chain of Responsibility – (Rust, Java, Go)
- Command Pattern – (Rust, Java, Go)
- Interpreter Pattern
- Iterator Pattern – (Rust, Java, Go)
- Visitor Pattern – (Rust, Java, Go)
- Mediator Pattern
- Memento Pattern
- Observer Pattern
- State Pattern – (Rust, Java, Go)
- Strategy Pattern – (Rust, Java, Go)
- Template Method Pattern
Learning these design patterns (or toolkits) is never too late for any programmers who want to improve their skills. In fact, many experienced programmers are not aware of these or even the design pattern concept.
So, keep learning! It is fun!