Design patterns is a very crucial topic in field of software engineering. Everybody wants to learn them in more and more depth. There is lot of literature available like GOF book and head first design patterns and other references. However some times it becomes difficult to map design patterns we use in our normal daily routine coding. We can find huge number of examples in books about the conceptual explanation and examples of design patterns but I would like to write down a short note on pattern definition and their standard usage in JDK.
This is just a page to revise in case you are planning for interview next day , provided you have already read patterns in details. I would not go with explanations and standard examples of coffee/tv remote or any fictional objects.
Pattern – “A pattern is a proven solution to a problem in a context.”
or in other words
“Each pattern is a three-part rule, which expresses a relation between a certain context, a problem, and a solution.”
Singleton – Ensure that only one instance of a class is created and Provide a global access point to the object.
Factory – Creates objects without exposing the instantiation logic to the client and Refers to the newly created object through a common interface.
- java.lang.Object#toString() (overrideable in all subclasses)
- java.lang.Integer#valueOf(String) (also on Boolean, Byte, Character, Short, Long, Float and Double)
Abstract Factory – Offers the interface for creating a family of related objects, without explicitly specifying their classes. We can simply say that it is higher level of abstraction of factory pattern.
- java.lang.management.ManagementFactory (all getXXX() methods)
Observer – Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.
- All implementations of java.util.EventListener (practically all over Swing thus)
Builder – Defines an instance for creating an object but letting subclasses decide which class to instantiate and Allows a finer control over the construction process. We can say the simplest builder will be similar to factory. Basically Builder allows you to give options for creating objects.
- java.lang.StringBuilder#append() (unsynchronized)
- java.lang.StringBuffer#append() (synchronized)
- java.nio.ByteBuffer#put() (also on CharBuffer, ShortBuffer, IntBuffer, LongBuffer, FloatBuffer and DoubleBuffer)
- All implementations of java.lang.Appendable
Prototype – Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype.
- java.lang.Object#clone() (the class has to implement java.lang.Cloneable)
Chain of Responsibiliy – It avoids attaching the sender of a request to its receiver, giving this way other objects the possibility of handling the request too.
– The objects become parts of a chain and the request is sent from one object to another across the chain until one of the objects will handle it.
In this I have covered seven patterns. Keep pinging the blog for next parts in this series. 🙂
This is not any reference or detailed documentation. You can always refer to more resources for detailed explanation of patterns.