Design Patterns : Drill

What is a design pattern.
————————————–
“Design patterns are recurring solutions to design problems you see over
Alpert, et. al., 1998).
“Design patterns constitute a set of rules describing how to accomplish
certain tasks in the realm of software development.” (Pree, 1994)

How many catogeries are there in Java patterns.
————————————————–
Creational patterns are ones that create objects for you, rather than
having you instantiate objects directly. This gives your program more
flexibility in deciding which objects need to be created for a given case.
Structural patterns help you compose groups of objects into larger
structures, such as complex user interfaces or accounting data.
Behavioral patterns help you define the communication between objects
in your system and how the flow is controlled in a complex program.

Design Patterns is a catalog of 23 generally useful patterns for writing objectoriented software.

Give me an example of Creational Patterns.
——————————————————
All of the creational patterns deal with the best way to create instances of objects. This is important because your program should not depend on how objects are created and arranged. In Java, of course, the simplest way to create an instance of an object is by using the new operator.

Sharat = new Sharat(); //instance of Fred class

However, this really amounts to hard coding how you create the object within your program. In many cases, the exact nature of the object that is created could vary with the needs of the program from time to time and abstracting the creation process into a special “creator” class can make your program more flexible and general.

The Factory Pattern is used to choose and return an instance of a class from a number of similar classes based on data you provide to the factory.
The Abstract Factory Pattern is used to return one of several groups of classes. In some cases it actually returns a Factory for that group of classes.
The Builder Pattern assembles a number of objects to make a new object, based on the data with which it is presented. Frequently, the choice of which way the objects are assembled is achieved using a Factory.
The Prototype Pattern copies or clones an existing class rather than creating a new instance when creating new instances is more expensive.
The Singleton Pattern is a pattern that insures there is one and only one instance of an object, and that it is possible to obtain global access to that one instance.

What is Singleton pattern. Where did you use it.
——————————————————
Definition: Singleton pattern is used whenever we require a class to have only one instance throughout the application. Sometimes it’s appropriate to have exactly one instance of a class: If we need some data to be stored , used and manipulated the same instance by the whole application, we will use singleton pattern. 

  • Ensure that only one instance of a class is created
  • Provide a global point of access to the object
  • Allow multiple instances in the future without affecting a singleton class’s clients

Examples:

  • window managers, print spoolers, and filesystems are prototypical examples.
  • Search window in Microsoft word, even if multiple documents are open, only one search window will be opened.  
  • One important usage of singleton pattern is when you need to instantiate a subclass and check which class to be instantiated during run time. For example there are two subclasses one for production and one for development and we need to instantiate one during run time depending on the environment.

Syntax:

public class ClassicSingleton {
private static ClassicSingleton instance = null;
protected ClassicSingleton() {
// Exists only to defeat instantiation.
}
public static ClassicSingleton getInstance() {
if(instance == null) {
instance = new ClassicSingleton();
}
return instance;
}
The ClassicSingleton class maintains a static reference to the lone singleton instance and returns that reference from the static getInstance() method.

public class SingletonInstantiator {
  public SingletonInstantiator() {
   ClassicSingleton instance = ClassicSingleton.getInstance();
ClassicSingleton anotherInstance =
new ClassicSingleton();
       …
  }
}
Explanation: In the first line we have created an instance of this class as null. We have declared a private constructor for the class to make sure that no object of class can be created outside the class.

getInstance() method is the entry point for the class which will return the only instance present for the class. This method is marked as static as it will be called directly from other classes. In this method we check if the instance of singletonExample is already present, if yes, return that instance else create the instance and return it. This makes sure that only one instance of the class is present at any point.

In the user class, that is calling our singleton class, we just need to call our singletonExample.getinstance() method which will return us the instance of singletonExample.

Types:

  • we create an instance of singleton class only when it is called for the first time is called lazy instantiation.
  • Another variation can be creating the instance before it is called, this kind of instantiation is called eager instantiation.

Problems:

  • A side effect of using singleton class: as the constructor of the singleton class is private, you cannot create a subclass of a singleton class, but in most of the cases that is desirable.
  • Additionally you can declare the class as final to explicitly show that the class can’t be subclassed. But in case if it is required to create a subclass of singleton class one can define the constructor of singleton class as protective instead of private.
  • Another point to note is that the singleton classes are not thread-safe.

Making your singleton class thread-safe is easy, you just have to make your getinstance method synchronized.

Public synchronized static singletonExample getInstance()
{
//if instance is null create else return old instance
}

What is Factory pattern. Explain it.
——————————————————

  • if we have a super class and n sub-classes, and based on data provided, we have to return the object of one of the sub-classes, we use a factory pattern.
  • provides a simple decision making class which returns one of several possible subclasses of an abstract base class depending on data it is provided.
  • Usually all of the classes it returns have a common parent class and common methods,but each of them performs a task differently and is optimized for differentkinds of data.

You should consider using a Factory pattern when
· A class can’t anticipate which kind of class of objects it must create.
· A class uses its subclasses to specify which objects it creates.
· You want to localize the knowledge of which class gets created.

Needs to isolate concrete classes from their super classes.

  • A system needs independent of how its products are created, composed, and represented.
  • Advertisements

    2 Responses

    1. This is very useful

    2. This Topic is very useful for us.Thanks.

    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

    %d bloggers like this: