Famous Java Design Patterns

By ongraph
February 16, 2015 | 3008 Views

Latest Industry insights to keep you updated on the latest happenings.

Prerequisites :-

If you are here and looking for a ready made dish to test. Please be aware of a giant. What I meant in my last sentence is OOP’s. Before we head into this topic I assume you have a basic idea of OOP’s concept and Java.  

 

Design Pattern :-

Design pattern comes in existance while you start developing complext and real world solutions. Most of us do follow these patterns in general but the difference is that we don’t even know if we are doing so. Design patterns are solutions to general problems that software developers faced during software development.

 

Types of Design Pattern :-

A.Creational Patterns

        These design patterns provide a way to create objects while hiding the creation logic, rather than instantiating objects directly using new opreator. This gives program more flexibility in deciding which objects need to be created for a given use case.

B. Structural Patterns

        These design patterns concern class and object composition. Concept of inheritance is used to compose interfaces and define ways to compose objects to obtain new functionalities.

C. Behavioral Patterns

        These design patterns are specifically concerned with communication between objects.

 

Scope :-

We will discuss only six popular design patterns here :-

  1. Factory
  2. Singleton
  3. Adpter
  4. Decorator
  5. Facade
  6. Observer

Let’s now discuss these one by one.

1.  Factory Design Pattern :- 

This pattern comes under creational pattern as this pattern deals with best ways of creating objects. In Factory pattern, we create an object with hiding the original complexities or creational logic to client and share a common interface.

We’re going to create a Color interface and concrete classes implementing the Color interface. A factory class ColorFactory is defined as a next Move.

FactoryPatternSample, our sample class will use ColorFactory to get a Color object. It will pass information ( RED / YELLOW / GREEN ) to ColorFactory to get the type of object it needs.

Move 1:-

Create an interface named Color. Yes, we are creating an interface not the class since it has no default property.

Color.java

public interface Color {
void paint();
}

 

Move 2:-

Create concrete classes implementing the same interface. You could think it as giving a defination to color class with specific color.

Red.java

public class Red implements Color {
@Override
public void paint() {
System.out.println("Inside Red::paint() method.");
}
}

 

Yellow.java

public class Yellow implements Color {
@Override
public void paint() {
System.out.println("Inside Yellow::paint() method.");
}
}

 

Green.java

public class Green implements Color {
@Override
public void paint() {
System.out.println("Inside Green::paint() method.");
}
}

 

Move 3:-

Create a Factory to generate object of concrete class based on given information.

ColorFactory.java

public class ColorFactory {
//use getColor method to get object of type Color
public Color getColor(String ColorType){
if(ColorType == null){
return null;
}
if(ColorType.equalsIgnoreCase("Yellow")){
return new Yellow();
} else if(ColorType.equalsIgnoreCase("Red")){
return new Red();
} else if(ColorType.equalsIgnoreCase("Green")){
return new Green();
}
return null;
}
}

 

Move 4:-

Use the Factory to get object of concrete class by passing an information such as type.

FactoryPatternDemo.java

public class FactoryPatternDemo {
public static void main(String[] args) {
ColorFactory ColorFactory = new ColorFactory();
//get an object of Yellow and call its paint method.
Color Color1 = ColorFactory.getColor("Yellow");
//call paint method of Yellow
Color1.paint();
//get an object of Red and call its paint method.
Color Color2 = ColorFactory.getColor("Red");
//call paint method of Red
Color2.paint();
//get an object of Green and call its paint method.
Color Color3 = ColorFactory.getColor("Green");
//call paint method of Yellow
Color3.paint();
}
}

 

Move 5:-

Verify the output.

Inside Yellow::paint() method.
Inside Red::paint() method.
Inside Green::paint() method.

 

2. Singleton Design Pattern :- 

This pattern comes under creational design pattern. It provides a more handy and good way to create an object with it’s life cylce being extended to Application.

 

This class provides a way to access its only object which can be accessed directly without need to instantiate the object of the class. An example of this is creating a connection to database with a single and only object to avoid any conflicts and maintain it throughout the application.

Implementation:- 

We’re going to create a MySingleton class. MySingleton class have its constructor as private and have a static instance of itself.
MySingleton class provides a static method to get its static instance to outside world.SingletonPatternSample, our sample class will use MySinleton class to get a MySingletonObject.

 

Move 1:-

Create a Singleton Class.

MySingleton.java

public class MySingleton {
//create an object of  MySingleton
private static volatile MySingleton instance = new MySingleton();
//make the constructor private so that this class cannot be
//instantiated
private MySingleton(){}
//Get the only object available
public static MySingleton getInstance(){
return instance;
}
public void  displayIt(){
System.out.println("It is global class!");
}
}

Move 2:-

Get the only object from the singleton class.

SingletonPatternDemo.java

public class SingletonPatternDemo {
public static void main(String[] args) {
//illegal construct
//Compile Time Error: The constructor MySingleton() is not visible
//MySingleton object = new MySingleton();
//Get the only object available
MySingleton object = MySingleton.getInstance();
//show the message
object.displayIt();
}
}

 

Move 3:-

Verify the output.

It is global class!

 

3. Adapter Design Pattern :-

Adapter pattern works as a bridge between two incompatible interfaces. This type of design pattern comes under structural pattern as this pattern combines the capability of two independent interfaces.
This pattern involves a single class which is responsible to join functionalities of independent or incompatible interfaces. As a real life example, we can think of a mobile charger as an adapter because mobile battery needs 3 volts to charge but the normal socket produces either 120V (US) or 240V (India). So the mobile charger works as an adapter between mobile charging socket and the wall socket.
We will try to implement multi-adapter using adapter design pattern in this tutorial.
So first of all we will have two classes – Volt (to measure volts) and Socket (producing constant volts of 120V).

 

Implementation:-

We’re going to create a MySingleton class. MySingleton class have its constructor as private and have a static instance of itself.

MySingleton class provides a static method to get its static instance to outside world.SingletonPatternSample, our sample class will use MySinleton class to get a MySingletonObject.

Move 1:-

Create a volt class bean.

Volt.java

public class Volt {
private int volts;
public Volt(int v){
this.volts=v;
}
public int getVolts() {
return volts;
}
public void setVolts(int volts) {
this.volts = volts;
}
}

 

Move 2:-

Create supper class Socket.

Socket.java

public class Socket {
public Volt getVolt(){
return new Volt(120);
}
}

 

Move 3:-

Create SocketAdapter interface that would work as a bridge B/W mobile socket and wall socket.

SocketAdapter.java

public interface SocketAdapter {
public Volt get120Volt();
public Volt get12Volt();
public Volt get3Volt();
}

 

Move 4:-

Create SocketAdapterImp that extends the behaviour of 120 volts(Socket) and supplies 3,12 and 120 volts by implementing SocketAdapter interface.

SocketObjectAdapterImpl.java 

//Using inheritance for adapter pattern
public class SocketAdapterImpl extends Socket implements SocketAdapter{
@Override
public Volt get120Volt() {
return getVolt();
}
@Override
public Volt get12Volt() {
Volt v= getVolt();
return convertVolt(v,10);
}
@Override
public Volt get3Volt() {
Volt v= getVolt();
return convertVolt(v,40);
}
private Volt convertVolt(Volt v, int i) {
new Volt(v.getVolts()/i);
}
}

 

Move 5:-

Create a test class to test our adapter implementation.

AdapterPatternTest.java 

public class AdapterPatternTest {
public static void main(String[] args) {
testClassAdapter();
}
private static void testClassAdapter() {
SocketAdapter sockAdapter = new SocketAdapterImpl();
Volt v3 = getVolt(sockAdapter,3);
Volt v12 = getVolt(sockAdapter,12);
Volt v120 = getVolt(sockAdapter,120);
System.out.println("v3 volts using Class Adapter="+v3.getVolts());
System.out.println("v12 volts using Class Adapter="+v12.getVolts());
System.out.println("v120 volts using Class Adapter="+v120.getVolts());
}
private static Volt getVolt(SocketAdapter sockAdapter, int i) {
switch (i){
case 3: return sockAdapter.get3Volt();
case 12: return sockAdapter.get12Volt();
case 120: return sockAdapter.get120Volt();
default: return sockAdapter.get120Volt();
}
}
}

 

Move 6:-

Verify the output.

Iv3 volts using Class Adapter=3
v12 volts using Class Adapter=12
v120 volts using Class Adapter=120

 

4. Decorator Design Pattern:-

Decorator pattern allows a user to add new functionality to an existing object without altering its structure. This type of design pattern comes under structural pattern as this pattern acts as a wrapper to existing class.
This pattern creates a decorator class which wraps the original class and provides additional functionality keeping class methods signature intact.
We are demonstrating the use of decorator pattern via following example in which we will decorate a Color with some mixer of colors without alter Color class.

Implementation:-

Move 1:-

Create an interface.

Color.java

public interface Color {
void paint();
}

 

Move 2:-

Create concrete classes implementing the same interface.

Red.java

public class Red implements Color {
@Override
public void paint() {
System.out.println("Color: Red");
}
}

 

Green.java

public class Green implements Color {
@Override
public void paint() {
System.out.println("Color: Green");
}
}

 

Move 3:-

Create abstract decorator class implementing the Color interface.

ColorDecorator.java

public abstract class ColorDecorator implements Color {
protected Color decoratedColor;
public ColorDecorator(Color decoratedColor){
this.decoratedColor = decoratedColor;
}
public void paint(){
decoratedColor.paint();
}
}

 

Move 4:-

Create concrete decorator class extending the ColorDecorator class.

BlueColorDecorator.java

public class BlueColorDecorator extends ColorDecorator {
public BlueColorDecorator(Color decoratedColor) {
super(decoratedColor);
}
@Override
public void paint() {
decoratedColor.paint();
paintBlue(decoratedColor);
}
private void paintBlue(Color decoratedColor){
System.out.println("paint one more Color: Blue");
}
}

 

Move 5:-

Use the RedColorDecorator to decorate Color objects.

DecoratorPatternDemo.java

public class DecoratorPatternDemo {
public static void main(String[] args) {
Color Green = new Green();
Color blueGreen = new RedColorDecorator(new Green());
Color blueRed = new RedColorDecorator(new Red());
System.out.println("Simple Green");
Green.paint();
System.out.println("\nGreen with Blue mixer");
redGreen.paint();
System.out.println("\nRed with blue mixer");
redBlue.paint();
}
}

 

Move 6:-

Verify the output.

Simple Green
Color: Green

Green with Blue mixer

Color: Green
paint one more Color: Blue

Red with blue mixer

Color: Red
paint one more Color: Blue

 

5. Facade Design Pattern :-

Facade pattern hides the complexities of the system and provides an interface to the client using which the client can access the system. This type of design pattern comes under structural pattern as this pattern adds an interface to existing system to hide its complexities.
This pattern involves a single class which provides simplified methods required by client and delegates calls to methods of existing system classes.

 

Implementation:-

Move 1:-

Create an interface.

Color.java

public interface Color {
void paint();
}

 

Move 2:-

Create concrete classes implementing the same interface.

Red.java

public class Red implements Color {
@Override
public void paint() {
System.out.println("Red::paint()");
}
}

 

Yellow.java

public class Yellow implements Color {
@Override
public void paint() {
System.out.println("Yellow::paint()");
}
}

 

Green.java

public class Green implements Color {
@Override
public void paint() {
System.out.println("Green::paint()");
}
}

 

Move 3:-

Create a facade class.

ColorMaker.java

public class ColorMaker {
private Color Red;
private Color Yellow;
private Color Green;
public ColorMaker() {
Red = new Red();
Yellow = new Yellow();
Green = new Green();
}
public void paintRed(){
Red.paint();
}
public void paintYellow(){
Yellow.paint();
}
public void paintGreen(){
Green.paint();
}
}

 

Move 4:-

Use the facade to paint various types of Colors.

FacadePatternSample.java

public class FacadePatternSample {
public static void main(String[] args) {
ColorMaker ColorMaker = new ColorMaker();
ColorMaker.paintRed();
ColorMaker.paintYellow();
ColorMaker.paintGreen();
}
}

 

Move 5:-

Verify the output.

Red::paint()
Yellow::paint()
Green::paint()

 

6. Observer Design Pattern :- 

Observer pattern is used when there is one-to-many relationship between objects such as if one object is modified, its depenedent objects are to be notified automatically. Observer pattern falls under behavioral pattern category.

Implementation:- 

Observer pattern uses three actor classes. Subject, Observer and Client. Subject is an object having methods to attach and detach observers to a client object. We have created an abstract class Observer and a concrete class Subject that is extending class Observer.
ObserverPatternSample, our sample class, will use Subject and concrete class object to show observer pattern in action.

 

Move 1:- 

Create Subject class.

Subject.java

public class Subject {
private List<Observer> observers = new ArrayList<Observer>();
private int state;
public int getState() {
return state;
}
public void setState(int state) {
this.state = state;
notifyAllObservers();
}
public void attach(Observer observer){
observers.add(observer);
}
public void notifyAllObservers(){
for (Observer observer : observers) {
observer.update();
}
}
}

 

Move 2:-

Create Observer class.

Observer.java

public abstract class Observer {
protected Subject subject;
public abstract void update();
}

 

Move 3:-

Create concrete observer classes

BinaryObserver.java

public class BinaryObserver extends Observer{
Subject subject;
public BinaryObserver(Subject subject){
this.subject = subject;
this.subject.attach(this);
}
@Override
public void update() {
System.out.println( "Binary String: " + Integer.toBinaryString( subject.getState() ) );
}
}

 

OctalObserver.java

public class OctalObserver extends Observer{
Subject subject;
public OctalObserver(Subject subject){
this.subject = subject;
this.subject.attach(this);
}
@Override
public void update() {
System.out.println( "Octal String: " + Integer.toOctalString( subject.getState() ) );
}
}

 

HexaObserver.java

public class HexaObserver extends Observer{
Subject subject;
public HexaObserver(Subject subject){
this.subject = subject;
this.subject.attach(this);
}
@Override
public void update() {
System.out.println( "Hex String: " + Integer.toHexString( subject.getState() ).toUpperCase() );
}
}

 

Move 4:-

Use Subject and concrete observer objects.

ObserverPatternDemo.java

public class ObserverPatternDemo {
public static void main(String[] args) {
Subject subject = new Subject();
new HexaObserver(subject);
new OctalObserver(subject);
new BinaryObserver(subject);
System.out.println("First state change: 15");
subject.setState(15);
System.out.println("Second state change: 10");
subject.setState(10);
}
}

 

Move 5:-

Verify the output.

First state change: 15
Hex String: F
Octal String: 17
Binary String: 1111
Second state change: 10
Hex String: A
Octal String: 12
Binary String: 1010

That’s all about ‘Famous Java Design Patterns’. If you have any question please write in comment.

Monthly industry insights to keep you updated on latest happenings

Follow us on Twitter
Follow us on Facebook
Follow us on Linkedin