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.
First understand the Abstract Factory Pattern from real world.
Abstract Factory 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.
There are three sub classes which is extends to common class names Message and override the display() method.
Now we are going add one abstract layer for Message service application
Now we creates the ConcreteFactory classes for GoodMorning,GoodAfternoon and GoodEvening
Create a class which will return the factories based on data provided
Keypoints:
Thanks
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
First understand the Abstract Factory Pattern from real world.
- Abstract means hiding some information
- Factory means which produce the items
- Pattern means a design
Abstract Factory Pattern:
- It is creation design pattern
- It is one level abstraction higher than factory pattern.
- It returns the factory of the classes.
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";
}
}
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();
}
}
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.
Thanks
No comments:
Post a Comment