What is a design pattern(DP)? What DPs do you know? Describe me the design pattern X, please. These are the questions you can hear in many technical interviews for object-oriented(OO) developer positions. I’ve heard many opinions for and against DP, but I think this is a must to know for a developer. It is not necessary to know all of them or even know implementation details by heart. It’s enough to know what the DP is about and what it actually does, and you can google the implementation details when you need it.

The Bible

The book “Design Patterns: Elements of Reusable Object-Oriented Software” by “Gang of Four” is like a bible for DP. This book is quite old, moreover, reading a bible is not very fun and easy. I want to recommend an amazing book called “Head First Design Patterns” by Eric Freeman, which is easy to read and understand. It doesn’t cover all the DPs from “Gang of Four”, but it is understandable. Time changes and we have a lot of tools, utilities, and JDK changes as well so not everything what was a problem more than 20 years ago is still relative now.

My repository on github includes the code from my DP studies. All the DP show a real world scenarios in simple examples. There are more details in the README file together with OO principles. Some of these pattens are taken from the mentioned book and some are my interpretations. Here is a list of DPs which can be found there:

  1. Strategy defines family of algorithms, encapsulates them and allows to switch the algorithm at a runtime.
  2. Observer defines one to many relationship between objects with one publisher to many observers/subscribers/consumers model.
  3. Decorator adds additional responsibilities dynamically. Or in another words, it allows to add new functionality to an existing object without altering its structure.
  4. Adapter makes two interfaces compatible. It converts interface to invoking another interface, which client expects.
  5. Chain of responsibility is a set of objects which gets the object, handles it and forwards to the next one.
  6. Command encapsulates request as an object, letting you parameterize other objects with different requests.
  7. Singleton ensures to have just a one instance of the class and provides global point of access to it.
  8. Facade provides an easier API for the client. It gives a higher level interface for the client and groups a logic behind.
  9. Template defines the an algorithm. The skeleton of algorithm is defined as ‘final’ and the behaviour which varies is implemented in subclasses.
  10. Iterator provides a way to access a collection without exposing it’s implementation. It is implemented by the JDK but unless you need a custom one…
  11. Abstract method defines an interface to create an object where a subclass of a factory decides which implementation to use.
  12. Abstract factory defines and interface to create groups/families of objects. Often it is an extension to abstract method pattern.
  13. Composite declares hierarchical data structures in a form of a tree.
  14. State controls the object when its’ internal state changes; it’s state machine controlled by a context.

Proxy provides a surrogate for an object to control access to it.
Remote proxy controls the access for another JVM,
Virtual proxy acts as a representative for an object that may be expensive to create,
Protection proxy uses handlers to control the access to the resources.

Looking at the code, some of DP might look very similar to each other. And actually they are, the difference is fundamental. So don’t focus on implementation, understand the goal first. Finally, the DP are just guidelines. Every project has its own specific so it is sometimes better to keep consistency of the project or use a tool rather than force adaptation of a DP.

Takeaways

  • Design pattern is a solution to a common problem
  • “Head First Design Patterns” by Eric Freeman is an amazing book for learning DP
  • You can use my DP code and follow me on
Share: