FACTORY
PATTERN
presented to
Mohammad Samiullah
presented by
Anindya Sundar Paul, 07
Zahin Jawad, 31
2
SCENARIO
Scenario
3
Pizza Shop!
Pizza orderPizza() {
Pizza pizza = new Pizza();
// bake, box and other stuffs
return pizza;
}
Scenario
4
But we have so many
different types of
pizza to offer!
5
PROBLEM!
Problem
6
Pizza
CheesePizza GreekPizza PepperoniPizza
Solution
7
Pizza orderPizza(String type) {
Pizza pizza;
if(type.equals(“cheese”)) pizza = new CheesePizza();
if(type.equals(“greek”)) pizza = new GreekPizza();
if(type.equals(“pepperoni”)) pizza = new PepperoniPizza();
// bake, box and other stuffs
return pizza;
}
8
MORE PIZZAS!
Problem
9
New problem.
• Add more pizzas to the menu.
• Remove the unpopular ones.
• Change the system accordingly.
Solution
10
Pizza orderPizza(String type) {
Pizza pizza;
if(type.equals(“cheese”)) pizza = new CheesePizza();
if(type.equals(“greek”)) pizza = new GreekPizza();
if(type.equals(“pepperoni”)) pizza = new PepperoniPizza();
if(type.equals(“clam”)) pizza = new ClamPizza();
if(type.equals(“veggie”)) pizza = new VeggiePizza();
// bake, box and other stuffs
return pizza;
}
11
Nice solution?
Realize where
this is going?
Everlasting Problem
12
Everlasting problem!
The business will keep on changing
FOREVER.
Everlasting Problem
13
New pizza, change code.
Remove pizza, change code.
New pizza, change code.
Remove pizza, change code.
It just
doesn’t
seem right!
Ultimate solution
14
So what’s the ultimate
solution?
Design Pattern
15
USE DESIGN PATTERN
Design Patterns
16
What is design pattern?
a general reusable solution to
a commonly occurring
problem
Design Patterns
17
Why use it?
• already solved
• reliable solution
• time saving
Design Patterns
18
How does it relate to our problem?
Factory pattern solves the
problem we were having!
Factory Pattern
19
The problem was about
the object creation.
Let’s take that part out and
encapsulate it!
Factory Pattern
20
Introducing the
FACTORY
a new class for the
creation of objects
Factory Pattern
21
public class PizzaFactory {
public Pizza createPizza(String type) {
Pizza pizza = null;
if(type.equals(“cheese”)) pizza = new CheesePizza();
if(type.equals(“pepperoni”)) pizza = new PepperoniPizza();
if(type.equals(“clam”)) pizza = new ClamPizza();
if(type.equals(“veggie”)) pizza = new VeggiePizza();
return pizza;
}
}
Factory Pattern
22
We have just pushed the same
problem to another class!
Factory Pattern
23
Many clients can use the factory.
- a pizza restaurant
- a home delivery service
Factory Pattern
24
Factory Pattern
25
Abstraction of the
PizzaStore for franchising
Use of factory method
instead of factory object
Factory Pattern
26
public abstract class PizzaStore {
public Pizza orderPizza(String type) {
Pizza pizza = createPizza(type);
// bake, box and other stuffs
return pizza;
}
abstract Pizza createPizza(String type);
}
Factory Pattern
27
NYPizzaStore or ChicagoPizzaStore
can implement their own way of
creating pizza.
28
A MORE
FORMAL
LOOK…
Factory Pattern
29
• defines an interface for
creating an object
• subclasses decide which class
to instantiate
Definition
Factory Pattern
30
Outline
• Intent
• Motivation
• Applicability
• Structure
• Participants
• Collaborations
• Consequences
31
Factory Pattern Intent
• provide an interface
• for creating families of related
or dependent objects
• without specifying the
concrete classes
32
Factory Pattern Motivation
• loosely coupled
• a common interface rather
than a concrete class
• creates dependencies
among the steps or
ingredients
33
Factory Pattern Applicability
• independent of creation,
composition and representation
• configured with one of multiple
families of products
34
Factory Pattern Applicability
• related product objects to be
used together
• provide a class library of
products without revealing
implementations
35
Factory Pattern Structure
36
Factory Pattern Participants
• Abstract Factory
• Concrete Factory
• Abstract Product
37
Factory Pattern Participants
• Concrete Product
• Client
38
Factory Pattern Collaborations
• single instance of concrete factory
class to create certain product
• different instance of concrete
factory to create different products
• abstract factory defers to concrete
subclasses
39
Factory Pattern Consequences
• isolation of concrete classes
• easier product family exchange
• consistency among products
• supporting new products is
difficult
40
THANK YOU

Factory Pattern

Editor's Notes

  • #2 Welcome everyone
  • #3 Before diving in, let’s look into a scenario.
  • #4 Let’s say we have a pizza shop. When someone orders a pizza, the shop will prepare the pizza by first making a new pizza. So the ordering and then returning the ordered pizza might look like this.
  • #5 We see it can make only one type of pizza. But we have so many different types of pizza to offer!
  • #6 So it’s a problem! And what do we do when we have a problem? We solve it!
  • #7 Let’s say we have three types of pizza to offer. Cheese, Greek and Pepperoni pizza. So how can we change our system so that it can create any one of them according to the customer’s need?
  • #8 Here is a probable solution. We take the pizza type as an argument and then based on that instantiate the right type of pizza that our customer wants. Seems like a nice solution!
  • #9 But our customers want more varieties. They want more pizzas. So what do we do now?
  • #10 So now we have to add the new pizzas to our system so that the customers can order those. Doesn’t seem to be a big deal, right?
  • #11 So we remove the condition for the Greek Pizza and add conditions for the new pizzas. Now our system can handle the change in the business.
  • #13 The business will always change. It’s the harsh truth. We can do nothing about it.
  • #14 The business will always change. It’s the harsh truth. We can do nothing about it.
  • #17 In software engineering, a design pattern is a general reusable solution to a commonly occurring problem within a given context in software design.
  • #18 In software engineering, a design pattern is a general reusable solution to a commonly occurring problem within a given context in software design.
  • #19 In software engineering, a design pattern is a general reusable solution to a commonly occurring problem within a given context in software design.
  • #20 In software engineering, a design pattern is a general reusable solution to a commonly occurring problem within a given context in software design.
  • #21 In software engineering, a design pattern is a general reusable solution to a commonly occurring problem within a given context in software design.
  • #22 In software engineering, a design pattern is a general reusable solution to a commonly occurring problem within a given context in software design.
  • #29 Before diving in, let’s look into a scenario.
  • #30 Formal Definition
  • #31 This is the outline of our discussion. We are going to look at the intent, motivation, …, consequences
  • #32 The definition is a clear indication of the intent. Provide an interface for creating families of related or dependent objects without specifying their concrete classes.
  • #33 1)Loosely coupled 2)Clients commit to a common interface rather than a concrete class 3)The factory only creates dependencies among the steps or ingredients of a certain product
  • #34 The factory pattern should be used in these scenarios 1. A system should be independent of how its products are created, composed and represented. 2. A system should be configured with one of multiple families of products.
  • #35 The factory pattern should be used in these scenarios 3. A family of related product objects is designed to be used together and the designer needs to enforce this constraint. 4. The designer wants to provide a class library of products, and wants to reveal just their interfaces, not their implementations.
  • #37 1) AbstractFactory () declares an interface for operations that create abstract product objects. 2)ConcreteFactory () implements the operations to create concrete product objects. 3) AbstractProduct () declares an interface for a type of product object.
  • #38 · ConcreteProduct () defines a product object to be created by the corresponding concrete factory. implements the AbstractProduct interface. · Client uses only interfaces declared by AbstractFactory and AbstractProduct classes.
  • #39 1) A single instance of a concrete factory class is used to create a certain type of produce. 2) Clients use different instances of concrete factory classes to create different products. 3) The abstract factory defers creation of product objects to its concrete factory subclasses.
  • #40 1)Isolates concrete classes 2)It makes exchanging product families easy 3)It promotes consistency among products 4)Supporting new kinds of products is difficult