Monday, 2 December 2013

Abstract Factory Pattern

Design Pattern:

An approach or solution for specific problem is called design pattern, which is well proven solution for redundant problem. In simple word we can say that it is well described solution to common object oriented programming language.

It is very required to know at least some of the popular java design pattern to be a good java professional.
Commonly there are three types of design pattern in object oriented programming language.
  • Creational design pattern
  • Structural design pattern
  • Behavior design pattern
In this article, we will learn what and how to use abstract factory design pattern with demo example.
First understand the Abstract Factory Pattern from real world.
  • Abstract means hiding some information
  • Factory means which produce the items
  • Pattern means a design
In programming language, we can say that system wants to create multiple families of the product without exposing the implementation details.

Abstract Factory Pattern:
  • It is creation design pattern
  • It is one level abstraction higher than factory pattern.
  • It returns the factory of the classes.
We must need to know the factory design pattern before to start read Abstract factory design pattern.

Where we can use Abstract Factory Design pattern?

When application needs to create the multiple families of product and we want to provide a library of products with hiding the implementation details.

Let take a same example of factory pattern to understand the abstract factory pattern.

package com.abstractfactorypattern.example;
public abstract class Message {
public abstract String display();
}

There are three sub classes which is extends to common class names Message and override the display() method.

package com.abstractfactorypattern.example;
public class GoodMorning extends Message{
@Override
public String display() {
return "Good Morning";
}
}

package com.abstractfactorypattern.example;
public class GoodAfternoon extends Message{
@Override
public String display() {
return "Good Afternoon";
}
}


package com.abstractfactorypattern.example;
public class GoodEvening extends Message {
@Override
public String display() {
return "Good Evening";
}
}
Now we are going add one abstract layer for Message service application

package com.abstractfactorypattern.example;
public interface AbstractMessageFactory {
public Message createMessage();
}

Now we creates the ConcreteFactory classes for GoodMorning,GoodAfternoon and GoodEvening

package com.abstractfactorypattern.example;
public class GoodMorningAbstractFactory implements AbstractMessageFactory {
@Override
public Message createMessage() {
return new GoodMorning();
}
} 
package com.abstractfactorypattern.example;
public class GoodAfterNoonAbstractFactory implements AbstractMessageFactory {
@Override
public Message createMessage() {
return new GoodAfternoon();
}
}

package com.abstractfactorypattern.example;
public class GoodEveningAbstractFactory implements AbstractMessageFactory {
@Override
public Message createMessage() {
return new GoodEvening();
}
}

Create a class which will return the factories based on data provided

package com.abstractfactorypattern.example;
public class AbstractFactoryMessageProvider {
public AbstractMessageFactory getMessage(String str){
if(str.equals("morning")){
return new GoodMorningAbstractFactory();
} else if(str.equals("afternoon")){
return new GoodAfterNoonAbstractFactory();
} else {
return new GoodEveningAbstractFactory();
}
}
} 
Main Run Class:

package com.abstractfactorypattern.example;
public class ExecuteAbstractFactory {
public static void main(String[] args) {
AbstractFactoryMessageProvider messageFactory = new AbstractFactoryMessageProvider();
AbstractMessageFactory message = messageFactory.getMessage("morning");
System.out.println(message.createMessage().display());
}
}

output: Good Morning

Keypoints:
  • It is a creational design pattern
  • It provides an interface for creating families of dependent objects without specifying their concrete classes.
  • It is one level abstraction higher than factory pattern.
  • It returns the factory of the classes.
We are always ready to take your suggestion to improvement for blog articles quality. So please free feel to ask.
Thanks

No comments:

Post a Comment