design pattern intro+ factory method pattern

34
Design Patterns

Upload: lavanyam210

Post on 04-Dec-2014

467 views

Category:

Technology


0 download

DESCRIPTION

An example of factory method pattern.

TRANSCRIPT

Page 1: Design pattern intro+ factory method pattern

Design Patterns

Page 2: Design pattern intro+ factory method pattern

What are they?

• Repeatable solution to commonly occurring problems

• Language-independent strategies for solving common object oriented design problems

• They provide a lingo

Page 3: Design pattern intro+ factory method pattern

Is-A Relationship(Inheritance)

Animal

Page 4: Design pattern intro+ factory method pattern

Has-A Relationship(Composition)

Page 5: Design pattern intro+ factory method pattern

Animal a = new Animal()

Dog d = new Dog()

Animal aDog = new Dog()

Page 6: Design pattern intro+ factory method pattern

Pizza Store

Prepare

Bake

Cut

Box

Page 7: Design pattern intro+ factory method pattern

Create

Bake

Cut

Box

Page 8: Design pattern intro+ factory method pattern

Menu

Cheese Pizza

Veggie Pizza

Spicy Chicken Pizza

Barbeque Chicken Pizza

Page 9: Design pattern intro+ factory method pattern

Cheese Pizza

Veggie Pizza

Spicy Chicken Pizza

Barbeque Chicken Pizza

Pizza

Is-A Relationship

Page 10: Design pattern intro+ factory method pattern

MumbaiThin crustFresh VeggiesLess sauce

PuneThick crustFrozen VeggiesLots of sauce and cheese

What kinda pizza’s do people in Mumbai and Pune like?

Page 11: Design pattern intro+ factory method pattern

Store Locations

Mumbai Pune

Page 12: Design pattern intro+ factory method pattern

How would the customer’s order for a pizza be?

Choose store location

Choose pizza

Mumbai

Cheese Pizza

Page 13: Design pattern intro+ factory method pattern

public static void main(String[] a){ PizzaStore p = new PizzaStore(); p.orderPizza(“Pune”,”Cheese”);}

class Main {

}

Page 14: Design pattern intro+ factory method pattern

class PizzaStore{

Pizza createPizza(store, pizzaType){ if(store==“Mumbai”){ if(type==“Cheese”) return new MumbaiStyleCheesePizza(); if(type==“SpicyChickenPizza”) return new MumbaiStyleSpicyChickenPizza(); …. }else if(store==“Pune”){ if(type==“Cheese”) ….}

void orderPizza(store, pizzaType){ pizza = createPizza(store,pizzaType); pizza.bake(); pizza.cut(); pizza.box();}

}

Methods in the Pizza class or its sub-classes

Page 15: Design pattern intro+ factory method pattern

if(store==“Mumbai”){ if(type==“Cheese”) return new MumbaiStyleCheesePizza(); if(type==“SpicyChickenPizza”) return new MumbaiStyleSpicyChickenPizza(); ….}else if(store==“Pune”){ if(type==“Cheese”) ….}

Is this good code?

What if we open a new store in Gurgaon?What if we add a new type of Pizza?What if we stop making a pizza?

if (store==“Gurgaon”){…}

If(type==“Margherita” new margheritaPizza()

If(type==“Veggie”) new veggiePizza()

Page 16: Design pattern intro+ factory method pattern

Design Principle

Classes should be open for extension but closed for modification

class PizzaStore{

}

….

Page 17: Design pattern intro+ factory method pattern

Design Principle

Separate out what changes from what remains the same

void createPizza(store, pizzaType){..}

void orderPizza(store,pizzaType){..}

Page 18: Design pattern intro+ factory method pattern

How do we do that?

MumbaiStore PuneStore

PizzaStore

GurgaonStore ChennaiStore

Page 19: Design pattern intro+ factory method pattern

abstract class PizzaStore{

abstract Pizza createPizza(type);

void orderPizza(pizzaType){ pizza = createPizza(pizzaType); pizza.prepare(); pizza.bake(); pizza.cut(); pizza.box();}

}

Pizza pizza;

Page 20: Design pattern intro+ factory method pattern

class MumbaiStore extends PizzaStore{

public Pizza createPizza(String pizzaType){ if(pizzaType.equals(“Cheese”)) return new MumbaiStyleCheesePizza(); if(pizzaType.equals(“Veggie”)) return new MumbaiStyleVeggiePizza();

….}

}

Page 21: Design pattern intro+ factory method pattern

MumbaiStore PuneStore

PizzaStore

Code

Page 22: Design pattern intro+ factory method pattern

PizzaStore.java

//Has reference to pizza (Pizza pizza;)

// Has orderPizza(String type) method which internally calls:// pizza = createPizza(type)// pizza.bake()// pizza.cut()// pizza.box()

//Has an// abstract createPizza(String type) method that returns Pizza

Page 23: Design pattern intro+ factory method pattern

PuneStore.java

//extends PizzaStore

// Pizza createPizza(String type) method//returns pizza object based on the type

MumbaiStore.java

//extends PizzaStore

// Pizza createPizza(String type) method//returns pizza object based on the type

Page 24: Design pattern intro+ factory method pattern

Factory Method

abstract Pizza createPizza(type);

abstract Product factoryMethod(String type);

A factory method is abstract so that the subclasses handle the object creation.

A factory method returns a product that is typically used within methods defined in the super class.

A factory method isolates the client code in the superclass from knowing what kind of concrete product is actually created.

A factory method may be parameterised to select among several variations of a product.

Page 25: Design pattern intro+ factory method pattern

Customers!!!

Page 26: Design pattern intro+ factory method pattern

Joel’s order

I need Pune style Pizza.PizzaStore puneStore = new PuneStore();

I want Cheese PizzapuneStore.orderPizza(“Cheese”);

Internally within the orderPizza methodPizza pizza = createPizza(“Cheese”);pizza.bake();pizza.cut();pizza.box();

Page 27: Design pattern intro+ factory method pattern

MainClass.java

//public static void main(String[] a)

PizzaStore puneStore = new PuneStore();puneStore.orderPizza(“Cheese”);

Page 28: Design pattern intro+ factory method pattern

MumbaiStyleCheesePizza PuneStyleCheesePizza

Pizza

Page 29: Design pattern intro+ factory method pattern

Pizza.java

// Has bake() method// Has cut() method// Has box() method

MumbaiStyleCheesePizza.java//extends Pizza class

// Overrides bake() method

PuneStyleCheesePizza.java//extends Pizza class

// Overrides bake() method

Page 30: Design pattern intro+ factory method pattern

Factory Method Pattern

Factory Pattern encapsulates the object creation by letting subclasses decide what objects to create.

Page 31: Design pattern intro+ factory method pattern

Creator Classes

MumbaiStore PuneStore

PizzaStore

GurgaonStore ChennaiStore

Page 32: Design pattern intro+ factory method pattern

Product Classes

Cheese Pizza

Veggie Pizza

Spicy Chicken Pizza

Barbeque Chicken Pizza

Pizza

Page 33: Design pattern intro+ factory method pattern

Product

Concrete Product

Creator

Concrete Creator

factoryMethod()otherMethods()

factoryMethod()

Page 34: Design pattern intro+ factory method pattern

Thank you!