Java Design Pattern #1

Reading Time: 3 minutes

In this blog, we will understand what design pattern is, its importance using example, and the types of design patterns we have.

What is design pattern?

Design pattern is a reusable solution to a commonly occurring problem within a given context in software design.

Every pattern has four essential elements.
1) Pattern name: It is an identifier or what the pattern is referred to as. It is a handle that can be used to describe a design problem, its solutions, and consequence in a word or two.
2) Problem: It describes when to apply the pattern and explains the problem and its context.
3) Solution: It describes the elements, their structure, relationship, and collaboration.
4) Consequences: They are the results, pros, and cons of applying the pattern for a particular situation.

Do we really need Design pattern?

Consider a scenario where a person Sean working in XYZ company makes duck simulation game that shows a large variety of ducks swimming and making quacking sounds.
Discussing technical details, the system uses OO techniques and created one Duck superclass from which other ducks inherits.
Now the company decides to add some innovations and they need the duck to fly.

So, Sean approached the problem in the following manner:
1) Sean added fly method in Duck superclass. But the problem occurs as the fly() method is inappropriate for some other Ducks subclass as they cannot fly like the rubber duck.

2) Then Sean comes out with other solution, He thought of taking out the fly() method from Duck Super Class make a Flyable interface with fly() method. So, this way the ducks that are supposed to fly will implement Flyable interface and have a fly method.
Initially, it looks promising but, Java interface do not have an implementation code. And whenever we have to modify any behaviour, then we have to track and change it in all the different subclasses where that behavior is defined which probably can introduce one bug. All I want to say is that its a nightmare to track everything!

So, here comes Design pattern as a rescue. We can solve the above problem by implementing the Strategy pattern which we will discuss in future blogs.

So, design pattern is basically a template that how we can resolve any problem and considered as an approach towards the best practice that programmers can use while designing the system to avoid inflexibility in code that can bring them in trouble later.

Types of Design Pattern

We have three categories of design patterns.
1) Creational: This design pattern is all about object creation. The main motive of this pattern is to decouple the client from the actual initialization process. It further divides into various types – Singleton, Factory, Abstract, and Builder.

2) Structural: This design pattern is all about organizing the class and object. To create an abstraction, it simply provides different ways of using object composition and inheritance. It further divides into different types – Adaptor, Bridge, Decorator, Facade, and Proxy.

3) Behavior: This pattern is concerned with communication between objects and provided with the loose coupling and flexibility to extend. It is of the following types – Iterator, Interpreter, Strategy, Template method, Chain of responsibility, Mediator, Template method, Observer.

How useful the design patterns are?

With the above use case mentioned, we are quite clear that how useful these design patterns are for any application. Implementing the correct design pattern as per the use case provides us with a highly cohesive module with minimum coupling. It simply isolates the variability that exists in system and makes it flexible, understandable, and maintainable.

 Conclusion

I hope you are clear about the design patterns, why we need it, its type and its advantage. In the upcoming blog, we will look into each design pattern with an illustration.
Please feel free to provide your suggestions. 🙂 Please comment if you have some queries and want to add some points.

References:
A book: Head First Design Patterns
https://www.baeldung.com/design-pattern-series

blog-footer

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading