Saturday, May 13, 2023

Top 23 Design Patterns Experienced Java Programmers Should Learn

Hello guys, if you want to learn Design Patterns and looking for a comprehensive tutorial which covers all important object oriented design pattern then you have come to the right place. Earlier, I have shared best Design pattern courses and books for experienced developers and in this article, I will introduce you with 23 common object oriented design patterns for writing better code. Design Patterns are tried and tested way of solving a problem within a given context. They are not invented, rather they are discovered, which is also obvious by use of word pattern. By using design pattern, you take knowledge of all wide communities and can safely use it to solve that problem. At times they might not give you perfect solution, require bit of tweaking, but they certainly provides one of the best solution, as long as you use them properly. While doing object oriented development, they are numerous task which appears quite often e.g. creating objects, structuring code, and implementing different behaviors based on different context.

Along the same line GOF recognized and divided design patterns in Creational, Structural and Behavioral patterns, in there classic book Design patterns. Though there are many more patterns which are used at different domain and across different projects, this catalog and book becomes a primary source to learn design pattern. To be frank, understanding design pattern is difficult job and applying them in your project is even more difficult.


Though I have found that, having a knowledge of different pattern in the back of your mind, helps to identify scenarios when pattern surfaces. If you have not spend much time with different design pattern than I suggest to get yourself a copy of Head First Design patterns by Kathy Siera, Freeman, Robson and Bates and the Design pattern explained by Shalloway and Trott. 

I personally prefer head first book because of interactive discussion they provide, there fire side chats, UML diagrams and cubical conversation helps tremendously to understand a design pattern, pros and cons and most importantly applicability.

Unfortunately that book doesn't cover all the design patterns mentioned by Gang of Four, so a second book is also necessary. Identifying patterns in different projects, frameworks and libraries is also a great way to learn design patterns in Java. For example, JDK itself uses several design patterns and serves a good reference for beginners.



23 Essential Object-Oriented Design Patterns for Java Programmers

And, here is the big diagram which shows the relationship between different object oriented design patterns:

Top 23 Object Oriented Design Patterns (GOF) Java Programmers Should Know


1. Creational Design Patterns in Java

Following is the list of Creational Design Pattern in Java.

1. Factory Design pattern in Java
Factory is a very useful and recognized pattern to create instance of objects in Java. It provides better control over object creation process then constructor. If you want to see an example of Factory pattern, you can see my earlier post about how to implement Factory pattern in Java

Factory Design pattern in Java



2. Abstract Factory Design pattern in Java
Abstract Factory pattern is extension of previous design pattern. It adds another layer of abstraction over Factory design pattern. In this case, Factory itself is abstract, which allows programs to choose different factory implementation, which produce different flavor of same product depending upon localization, and other requirements. You can see difference between factory and abstract factory pattern to learn more about it. 

Abstract Factory design pattern in Java



3. Builder Design Pattern
This pattern help you to create instance of complex object, which requires too many parameters. Once you exceed unwritten rule of more than five parameters for constructor, you will find yourself very hard to create instance. 

Since most of the parameters are also optional, it make no sense to just provide null or false for them. Builder sort this mess, it gives you clean and fluent interface to specify what you need and return product based upon those parameters. For code example, you can see my earlier Builder design pattern tutorial where I have talked more about this pattern. 


Builder Design Pattern in Java



4. Singleton Design Pattern in Java
Singleton is a useful pattern, which provides an easy way to access global objects in an application. Costly objects like Repository, Cache are almost always a Singleton in an application. Singleton pattern provides a consistent way to use them. 

If you are wondering how to implement Singleton pattern in Java then you can also checkout my 5 ways to implement Singleton Pattern in Java

By the way, with advent of test driven programming, Singleton has now become an anti pattern because of its inflexibility to provide test doubles during unit testing. Singleton are like static methods, which is very hard to mimic, a key requirement of test driven development.


Singleton Design Pattern in Java


5. Prototype Design Pattern in Java
Don't know much about Prototype pattern but whatever I know, I remember that it delegates the cloning process to the actual objects that are being cloned. It's also visible from following diagram where you can see Image is a common interface and has clone method but actually cloning is done by ImageOne and ImageTwo prefer not to clone anything. 


Prototype Design Pattern in Java UML Diagram


These were the Creational patterns from the original Gang of Four pattern list. They help you to solve problem related to creating objects.



2. Structural Design Patterns  in Java

Here is list of major Structural design patterns in Java.

6. Decorator Design Pattern in Java
Decorator pattern is used to add additional functionality on Object at runtime. It doesn't alter the interface, just adds new functionalities using Composition.

Decorator Design Pattern in Java




7. Adapter Design Pattern in Java
Adapter pattern is used to convert interfaces, so that two parties can work together, which otherwise can not because of incompatible interfaces. One example of Adapter pattern is

Adapter Design Pattern in Java




8. Composite Design Pattern in Java
Composite design pattern allows a client to treat collections of objects and individual objects uniformly. One way to identify Composite pattern is a tree like structure. 

In a tree, a node can either be a leaf node or another tree, similarly in Composite design pattern we have individual object and Composites. Though, operation on leaf and composite behave differently, but they are transparent to client. 

For example in an Organization tree, some employees are manager and has set of employees reporting to them. If we have a method called directs() which returns employees under a Manager, a normal employee may return empty list, while an Employee who is manager will return a list of Employees.

Composite Design Pattern in Java



9. Facade Design Pattern in Java
Sole job of Facade pattern is to make an interface simpler for use. It is used to simplify interface of one or more classes, so that they are easier to use.

Facade Design Pattern in Java




10. Proxy Design Pattern in Java
Proxy design pattern allows you to control access of an object due to various reason e.g. Security, Performance, Caching etc. Proxy implements same interface as real object, so it can act as surrogate, until real object is available and than it can forward request to real object for processing.

Proxy Design Pattern in Java




11. Flyweight Design Pattern
The Flyweight pattern is also extensively used in JDK e.g. Integer.valueOf() or String.valueOf() methods are example of Flyweight pattern in Java.





12. Bridge Design Pattern
The bridge design pattern is used to solve X problem in Java application. By using this pattern you can reduce complexity of your solution.





3. Behavioural Design Patterns in Java

This list contains some of the very useful behavioral design patterns for Java programmers.


13. Strategy Design Pattern in Java (example)
Strategy Pattern encapsulate interchangeable behaviors, known as Strategy or algorithm and uses delegation to decide, which behavior to use at run time. This pattern is based upon Open Closed design principle which promote how to write extensible code without touching already tried and tested code. 





14. State Design Pattern in Java (example)
State Pattern allows an object to change its behavior, when some state changes. It's one of the popular design pattern and you can use State Pattern to solve many interesting problem. For example, you can use State design pattern to implement state machine as well. Earlier, I have also showed you how to implement Vending Machine using State Pattern in Java, which is another great example of how and when to use State pattern in Java.  






15. Command Design Pattern in Java (example)
Command design pattern allows you to decouple the requester of an action from the object that actually performs the action. This is achieved by introducing a command object, an interface with a predefined method. Command object encapsulate actual object which performs the action. 

Requester only knows about Command object and doesn't know specifics of actual object, which reduce coupling between requester and action object.

 All requester needs to know is call  method on command object, which then delegates the request to specific action object. For example,  Command pattern is a behavioral pattern as defined by seminal book Design Patterns by GOF.






16. Template Design Pattern in Java (Example)
Template pattern is used to define an algorithm, but deferring certain steps to subclasses. By encapsulating Algorithm in super class, this pattern don't allow modification of algorithm, but provides flexibility by letting sub class to implement certain steps. In short, Template Pattern allows subclass to decide how to implement steps in an algorithm.

Template Design Pattern in Java (Example)



17. Observer Design Pattern in Java (example)
Observer pattern is used to notify a set of objects, when state of a another object changes. Object, whose state change is notified is called Subject or Observable, because it's observed by a set of Objects, known as Observer. If you are wondering how to implement Observer pattern in Java then you can see my Java Observer Design pattern tutorial, there I have shared complete code example from real world, just follow along. 


Observer Design Pattern in Java (example)




18. Iterator design Patter in Java (example)
Iterator pattern provides a way to traverse a collection or aggregation of object without exposing internal details of Collection.

Iterator design Patter in Java (example)




19. Visitor Design Pattern in Java
Visitor is another useful object oriented design pattern which is based upon concept of double dispatch. It removes ugly chain of if-else code require to customize behavior of different objects in same class hierarchy. It also follows open close design principle of SOLID principle. It stands for "O" in SOLID.

Visitor Design Pattern in Java example




20. Chain of Responsibility Pattern


Chain of Responsibility Pattern UML Diagram



21. Interpreter Design Pattern






22. Mediator Design Pattern






23. Memento Design Pattern







That's all on this list of essential object oriented design patterns  an experienced and senior Java developers should know. This patterns are not restricted to Java programming language and can be used with any other object oriented programming language like Python, C++,  or TypeScript. While this is a long article, you can bookmark it and refer it again and again to get familiar with these patterns. I also plan to keep updating this article and adding more useful info in future. 


Other Java Design Patterns tutorials you may like
  • How to design a Vending Machine in Java? (questions)
  • 5 Free Courses to learn Object Oriented Programming (courses)
  • How to implement a Decorator design pattern in Java? (tutorial)
  • When to use Command Design Pattern in Java (example)
  • How to use Factory method design pattern in Java? (tutorial)
  • 7 Best Courses to learn Design Pattern in Java (courses)
  • How to implement the Strategy Design Pattern in Java? (example)
  • Difference between Factory and Abstract Factory Pattern? (example)
  • How to create thread-safe Singleton in Java (example)
  • 7 Best Books to learn the Design Pattern in Java? (books)
  • Difference between Factory and Dependency Injection Pattern? (answer)
  • Difference between State and Strategy Design Pattern in Java? (answer)
  • 18 Java Design Pattern Interview Questions with Answers (list)
  • 20 System Design Interview Questions (list)
  • 5 Free Courses to learn Data Structure and Algorithms (courses)
  • 10 OOP Design Principle Every Java developer should learn (solid principle)
  • 7 Books to learn System Design for Beginners (System design books)
  • How to use Composite Design Pattern in Java (composite example)

Thanks a lot for reading this article so far. If you like these essential design pattern in Java and find this article worth your time then please share it with your friends and colleagues. If you have any questions or feedback then please drop a note.

P. S. - If you are an experienced Java developer and want to learn Design patterns and looking for a best design pattern resources like books and online courses then you can also checkout this list of best design pattern courses for experience developers to start with. It contains online courses to learn design pattern and how to use them in real world coding. 

No comments:

Post a Comment