View
311
Download
0
Category
Preview:
DESCRIPTION
An introduction to the abstract factory design pattern.
Citation preview
Abstract FactoryManohar Thummala
Bharat KhatriGovind Chintapalli
Mohana Sai Cherukuri
Pizza Store
Pizza orderPizza(String type) {Pizza pizza;if (type.equals(“cheese”)) {
pizza = new CheesePizza();} else if (type.equals(“greek”) {
pizza = new GreekPizza();} else if (type.equals(“pepperoni”) {
pizza = new PepperoniPizza();}
pizza.prepare();pizza.bake();pizza.cut();pizza.box();return pizza;
}
Pizza Store
Problem ?
The problem starts with objects' creation (new in Java).
Pizza orderPizza(String type) {Pizza pizza;if (type.equals(“cheese”)) {
pizza = new CheesePizza();} else if (type.equals(“greek”) {
pizza = new GreekPizza();} else if (type.equals(“pepperoni”) {
pizza = new PepperoniPizza();
}pizza.prepare();pizza.bake();pizza.cut();pizza.box();return pizza;
}
New Pizza Store
Pizza orderPizza(String type) {Pizza pizza;if (type.equals(“cheese”)) {
pizza = new CheesePizza();} else if (type.equals(“greek”) {
pizza = new GreekPizza();} else if (type.equals(“pepperoni”) {
pizza = new PepperoniPizza();
}pizza.prepare();pizza.bake();pizza.cut();pizza.box();return pizza;
}
Pizza Store
Solution
Encapsulate the change into a class which creates objects.Product - Objects (that need to be instantiated)
Factory - The class that encapsulates the creation of products.
Client - The class that performs actions on the products.
Pizza orderPizza(String type) {Pizza pizza;
pizza = new SimplePizzafactory .createPizza(type);
pizza.prepare();pizza.bake();pizza.cut();pizza.box();return pizza;}
public class SimplePizzaFactory {public Pizza createPizza(String type) {Pizza pizza = null;if (type.equals(“cheese”)) {
pizza = new CheesePizza();} else if(type.equals(“pepperoni”)) {
pizza = new PepperoniPizza();} else if (type.equals(“clam”)) {
pizza = new ClamPizza();} else if (type.equals(“veggie”)) {
pizza = new VeggiePizza();}return pizza;}}
We got a Factory !
Another Problem ?
Our factory is a concrete class.
New family of products will require new Factory.
DC Pizza Factory
public class DCPizzaFactory{Pizza createPizza(String item) {if (item.equals(“cheese”)) {return new DCCheesePizza();} else if (item.equals(“veggie”)) {return new DCVeggiePizza();} else if (item.equals(“clam”)) {return new DCClamPizza();} else if (item.equals(“pepperoni”)) {return new DCPepperoniPizza();} else return null;}}
NY Pizza Factory
public class NYPizzaFactory{Pizza createPizza(String item) {if (item.equals(“cheese”)) {return new NYCheesePizza();} else if (item.equals(“veggie”)) {return new NYVeggiePizza();} else if (item.equals(“clam”)) {return new NYClamPizza();} else if (item.equals(“pepperoni”)) {return new NYPepperoniPizza();} else return null;}}
Another Problem ?
Our factory is a concrete class.
New family of products will require a new Factory.
New concrete Factory implementation would lead to change in the client.
Abstract Factory
Abstract Factory - Intent
Provide an interface for creating families of related or dependent objects without specifying their concrete classes.
Let's get formal !
●AbstractFactory - declares an interface for operations that create abstract products.
●ConcreteFactory - implements operations to create concrete products.
●AbstractProduct - declares an interface for a type of product objects.
●Product - defines a product to be created by the corresponding ConcreteFactory; it implements the AbstractProduct interface.
●Client - uses the interfaces declared by the AbstractFactory and AbstractProduct classes.
Demonstration
Questions ?
Recommended