A dive into design patterns used in jdk – I

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.
Examples –

  • java.lang.Runtime#getRuntime()
  • java.awt.Desktop#getDesktop()

Factory – Creates objects without exposing the instantiation logic to the client and Refers to the newly created object through a common interface.
Examples –

  • java.lang.Object#toString() (overrideable in all subclasses)
  • java.lang.Class#newInstance()
  • java.lang.Integer#valueOf(String) (also on Boolean, Byte, Character, Short, Long, Float and Double)
  • java.lang.Class#forName()
  • java.lang.reflect.Array#newInstance()
  • java.lang.reflect.Constructor#newInstance()

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.
Examples –

  • java.util.Calendar#getInstance()
  • java.util.Arrays#asList()
  • java.util.ResourceBundle#getBundle()
  • java.net.URL#openConnection()
  • java.sql.DriverManager#getConnection()
  • java.sql.Connection#createStatement()
  • java.sql.Statement#executeQuery()
  • java.text.NumberFormat#getInstance()
  • java.lang.management.ManagementFactory (all getXXX() methods)
  • java.nio.charset.Charset#forName()
  • javax.xml.parsers.DocumentBuilderFactory#newInstance()
  • javax.xml.transform.TransformerFactory#newInstance()
  • javax.xml.xpath.XPathFactory#newInstance()

Observer – Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.
Examples –

  • java.util.Observer/java.util.Observable
  • All implementations of java.util.EventListener (practically all over Swing thus)
  • javax.servlet.http.HttpSessionBindingListener
  • javax.servlet.http.HttpSessionAttributeListener
  • javax.faces.event.PhaseListener

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.
Examples –

  • java.lang.StringBuilder#append() (unsynchronized)
  • java.lang.StringBuffer#append() (synchronized)
  • java.nio.ByteBuffer#put() (also on CharBuffer, ShortBuffer, IntBuffer, LongBuffer, FloatBuffer and DoubleBuffer)
  • javax.swing.GroupLayout.Group#addComponent()
  • 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.
Examples –

  • 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.

  • java.util.logging.Logger#log()
  • javax.servlet.Filter#doFilter()

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.

Advertisements
This entry was posted in Design patterns and tagged , . Bookmark the permalink.

2 Responses to A dive into design patterns used in jdk – I

  1. Pingback: A dive into design patterns used in jdk – II | Feasting Technical Cognition

  2. Hissam says:

    thanks, this is what I was looking for while learning designPatterns in depth

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s