In the next few posts, I will write a series of posts to discuss design patterns in Java. I will give an introduction to design patterns. What design patterns are? How to use them? I will describe design patterns in Java.
What are design patterns?
Firstly, design patterns are programming and design strategies. These are independent of programming languages. Design patterns are mostly used to build a solution for common object-oriented programming problems. Secondly, one of the major benefits of design patterns is that most code is reusable and easily maintainable. However, a design pattern is a repeatable solution to a commonly occurring problem in software design.
Design patterns speed up the process of development. Nevertheless, the design patterns differ in their complexity. Hence, using them takes some practice. Overusing design patterns can complicate the design and your system. Especially, design patterns should simplify the design and not complicate it.
Example of design pattern in the real world?
Therefore, to understand what exactly design patterns are, let’s consider a real-life example. Suppose we have an animal class. The subclasses for the animal class will be Elephant, Dog, Cat. I show these classes below.
Likewise, an abstract factory is a design pattern, that can be used in this example.
abstract class AbstractAnimalFactory { public Elephant makeElephant() { return new Elephant(); } public Dog makeDog(){ return new Dog(); } } abstract class Animal { } class Elephant extends Animal { } class Dog extends Animal { }
Types of design patterns
Consequently, based on their purpose, design patterns are divided into three types of patterns creational, structural, and behavioral. Moreover, each of these design patterns has sub-types.
Creational Design Pattern
- Singleton Design Pattern
- Factory Pattern
- Absolute factory Pattern
- Builder Pattern
- Prototype Pattern
Structural Design Pattern
- Adapter Pattern
- Composite Pattern
- Proxy Pattern
- Flyweight Pattern
- Facade Pattern
- Bridge Pattern
- Decorator Pattern
Behavioral Design Pattern
- Template Method Pattern
- Mediator Pattern
- Chain of responsibility Pattern
- Observer Pattern
- Strategy Pattern
- Command Pattern
- State Pattern
- Visitor Pattern
- Interpreter Pattern
- Iterator Pattern
- Memento Pattern
So, we will discuss each design pattern with a production-ready example.
Advantages of design patterns
- Reusable in multiple projects
- Capture the complete experience of software engineering
- Provide clarity to system architecture
Conclusion
In conclusion, we discussed an introduction to design patterns. Besides, there is some criticism about design patterns that I have not talked about. Furthermore, I will build actual design patterns to show how design patterns work. If you enjoyed this post, subscribe to my blog here.
References