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 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:
- Strategy defines family of algorithms, encapsulates them and allows to switch the algorithm at a runtime.
- Observer defines one to many relationship between objects with one publisher to many observers/subscribers/consumers model.
- Decorator adds additional responsibilities dynamically. Or in another words, it allows to add new functionality to an existing object without altering its structure.
- Adapter makes two interfaces compatible. It converts interface to invoking another interface, which client expects.
- Chain of responsibility is a set of objects which gets the object, handles it and forwards to the next one.
- Command encapsulates request as an object, letting you parameterize other objects with different requests.
- Singleton ensures to have just a one instance of the class and provides global point of access to it.
- Facade provides an easier API for the client. It gives a higher level interface for the client and groups a logic behind.
- Template defines the an algorithm. The skeleton of algorithm is defined as ‘final’ and the behaviour which varies is implemented in subclasses.
- 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…
- Abstract method defines an interface to create an object where a subclass of a factory decides which implementation to use.
- Abstract factory defines and interface to create groups/families of objects. Often it is an extension to abstract method pattern.
- Composite declares hierarchical data structures in a form of a tree.
- 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.
- 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