Observer Pattern

This pattern will notify change to various classes.

Leave a Comment

Singleton Pattern

Need to create only one object and make sure no other object will create again the object of same class but it can share the same object  created once.

A Singleton Pattern ensures a class has only one object or instance and provides a global point of access to it.

A singleton is a class that can be instantiated only one time in a JVM per class loader. Repeated calls always return the same instance. Ensures that a class has only one instance, and provide a global point of access. It can be an issue if singleton class gets loaded by multiple class loaders.

/*
*
*  lazy creation of the object
*  ensure only one object
*
*  Chance of creating multiple
*  objects in multi threads environment.
*
*/

public class Singleton
{

	private static Singleton singleton;

	private Singleton(){
		System.out.println( "Create an object " +
		"which is having the private Constructor" );
	}

	public static Singleton getInstance(){
		if(singleton == null){
			singleton = new Singleton();
		}
		return singleton;
	}

}
/*
*
*  lazy creation of the object
*  ensure only one object in conditions
*  of multiple threading environment
*
*  Every time you call a method
*  it is locks up to give you the object
*  synchronized block is expensive
*
*/
class SingletonSync
{

	private static SingletonSync singleton;

	private SingletonSync(){
		System.out.println( "Create an object " +
				"which is having the private" );
	}

	public static synchronized SingletonSync getInstance(){
		if(singleton == null){
			singleton = new SingletonSync();
		}
		return singleton;
	}

}
/**
 *  This method of creation of the object is defining and initializing the object
 *
 *  ensure only one object in conditions of multiple threading environment without
 *  using the synchronized block
 *
 *  you will find an object weather it is used or not
 *  So it will occupy memory etc.
 *
 */
class SingletonEager {

	private static SingletonEager
		single = new SingletonEager();

	private SingletonEager(){
		System.out.println( "Create an object " +
				"which is having the private" );
	}

	public static SingletonEager getInstance(){
		return single;
	}

}
/**
 * I think this is the most preferable
 *  way of implementing the singleton pattern
 *
 *  lazy creation of the object
 *  ensure only one object in conditions
 *  of multiple threading environment
 *  only one time synchronization block is used.
 *
 *  synchronized block is expensive
 *
 */

class SingletonDoubleCheck {

	// The volatile keyword ensures that multiple threads
	// handle the unique instance variable correctly
	// when it is being initialized to the singleton instance
	private volatile static SingletonDoubleCheck singleton;

	private SingletonDoubleCheck(){
		System.out.println( "Create an object" +
				" with double checked syncronized" );
	}

    // we only synchronize one time when it cont found the object
	public static SingletonDoubleCheck getInstance(){
		if(singleton == null){
			synchronized (SingletonDoubleCheck.class) {
				singleton = new SingletonDoubleCheck();
			}
		}
		return singleton;
	}
}
/**
*  This method of creation of the object is defining and initializing the object
*
*  ensure only one object in conditions of multiple threading environment without
*  using the synchronized block
*
*  you will find an object weather it is used or not
*  So it will occupy memory etc.
*
*/
class SingletonEager {
private static SingletonEager
single = new SingletonEager();
private SingletonEager(){
System.out.println( “Create an object ” +
“which is having the private” );
}
public static SingletonEager getInstance(){
return single;
}
}

Leave a Comment

Builder Pattern

The builder pattern encapsulate or separate the construction of a complex object from its representation so that the same construction process can create different representations.

The Builder Pattern

The Builder Pattern

Leave a Comment

Abstract Factory Pattern

Objects creates the objects of releated classes without specifying the concrete classes.

Abstract Factory Pattern

Abstract Factory Pattern

Leave a Comment

Abstract Factory Pattern

Provides an interface for creating families of  related or dependent objects without specifying their concrete classes

Leave a Comment

Factory Pattern

Object is basically instace of  there prototype called class. There are lot of ways to create the object in various technologies. But ultimately we need to create an object.

What is the strategy of creating objects. weather we use “new” key word to create an object or we are using the method to get the created object.

Factory pattern defines a way and handle creation the objects. It is basically talks about the obtects which are helpful to create the object without knowing the class of the object.Factory pattern encapsulate object creation.

How do we implement Factory pattern?

Accroding to GOF

Define an interface for creating an object but let subclasses decide which class to instanciate. Factory method lets a class difer instantiation to subclass.

Leave a Comment

Creational Patterns

In object oriented technologies we need to create the objects. But how, what is the process? what is the best way to create an object? Do we create object using or explicitly calling the constructor or we need to use any kind of  patterns used to create the objects. So the object creation patterns help us to explain the different kind of object creations methodologies and there intent and pros and cons.

So we have popular patterns such as Singleton, Factory, Abstract Factory, Builder, Prototype etc. we will see these and discuss in details in next posts

Leave a Comment

Interface and Abstract class pattern

Client classes are independent of the classes that implement the behavior of the class and make sure that there is consistent behavior of the classes which implement the interface.

The abstract class implement the interface and the concrete classes extends the behaviour of the abstract class.

Don’t  chose only between interface and abstract class. Let abstract class implement the interface and the concrete classes extend the abstract class.

Interfaces can be used to hide the specific class that implement the behavior from the client classes

Organize the classes that provide related behavior common the the abstract super class helps to ensure consistency of implementation

Using both interface and abstract class pattern helps the object desing to get the benefit of both the interface and abstract classes.

interface-abstract-class-pattern

Leave a Comment

Design as a thought

Design is an ART of designer. Various thoughts are combined to originate a design. When those thoughts of designer are accepted by the community, then it becomes a pattern which is tested and followed and implemented.

We are focused most on the technological design and design pattern applied for architecting the software application. Irrespective of technology we discuss here most importantly the intent, advantages and disadvantages of the design patterns.  Please join hands with us to contribute to this cause to make this blog helpful for those who really wants to understand the design pattern and apply in there daily life.

Leave a Comment