Factory Design
Patterns
Elements of Reusable Object-Oriented Software
Content
1. Design Patterns
2. Factory Method Design Pattern
a. Structure
b. Implementation
3. Abstract Factory Design Pattern
a. Structure
2
Design Patterns
3
Creational Patterns Structural Patterns Behavioral Patterns
Design patterns are solutions to general problems that software
developers faced during software development.
Factory Method Design Pattern
Define an interface for creating an object, but let subclasses decide
which class to instantiate.
4
Car Factory
Demo Class
Car Objects
Structure of Factory Method
5
asks
create
Implements
<<Interface>>
Car
- speed : float
- color : color
+ getCar() : Car
<<Class>>
Sport Car
+ getCar() : Car
<<Class>>
Luxury Car
+ getCar() : Car
<<Class>>
Mini Car
+ getCar() : Car
<<Class>>
Demo Class
+ main() : void
<<Class>>
Car Factory
+ getCar(type) : Car
Implementation of Factory Method
<?php
class CarFactory
{
public static function getCar($type) {
// assumes the use of an autoloader
$product = "Product_" . $type;
if (class_exists($product)) {
return new $product();
}
else {
throw new Exception("Invalid product type given.");
}
}
}
Implementation of Factory Method
<?php
// build a new Product_Sport_Car
$mySportCar = CarFactory :: getCar(“sport car");
// build a new Product_Mini_Car
$myLuxuryCar = CarFactory :: getCar(“luxury car");
// build a new Product_Mini_Car
$myMiniCar = CarFactory :: getCar(“mini car");
Abstract Factory Design Pattern
Abstract Factory patterns work around a super-factory which
creates other factories.
Provide an interface for creating families of related or dependent
objects without specifying their concrete classes.
8
Abstract Factory Design Pattern
9
Vehicle
Factory
Demo Class
BMW Factory
Volkswagen Factory
Car
Object
SUV
Object
Structure of Abstract Factory
10
<<Class>>
Demo Class
+ main() : void
<<Interface>>
Car
- speed : float
- color : color
+ getCar() : Car
<<Interface>>
SUV
- speed : float
- color : color
+ getSUV() : SUV
<<Class>>
BMW Car
+ getCar() : Car
<<Class>>
Volkwagen Car
+ getCar() : Car
<<Class>>
BMW SUV
+ getSUV() : SUV
<<Class>>
Volkwagen SUV
+ getSUV() : SUV
<<Class>>
BMW Factory
+ getBMW(type)
<<Class>>
Volkswagen
Factory
+ getVW(type)
<<Class>>
Vehicle Factory
+ getVehicle(type)
Implements
extends
extends
extends
use
extends
extends
Kanushka Gayan
SE intern @ Extrogene Software (Pvt) Ltd
kanushkanet@gmail.com
071 879 4546
Thank You!

Factory Design Pattern

  • 1.
    Factory Design Patterns Elements ofReusable Object-Oriented Software
  • 2.
    Content 1. Design Patterns 2.Factory Method Design Pattern a. Structure b. Implementation 3. Abstract Factory Design Pattern a. Structure 2
  • 3.
    Design Patterns 3 Creational PatternsStructural Patterns Behavioral Patterns Design patterns are solutions to general problems that software developers faced during software development.
  • 4.
    Factory Method DesignPattern Define an interface for creating an object, but let subclasses decide which class to instantiate. 4 Car Factory Demo Class Car Objects
  • 5.
    Structure of FactoryMethod 5 asks create Implements <<Interface>> Car - speed : float - color : color + getCar() : Car <<Class>> Sport Car + getCar() : Car <<Class>> Luxury Car + getCar() : Car <<Class>> Mini Car + getCar() : Car <<Class>> Demo Class + main() : void <<Class>> Car Factory + getCar(type) : Car
  • 6.
    Implementation of FactoryMethod <?php class CarFactory { public static function getCar($type) { // assumes the use of an autoloader $product = "Product_" . $type; if (class_exists($product)) { return new $product(); } else { throw new Exception("Invalid product type given."); } } }
  • 7.
    Implementation of FactoryMethod <?php // build a new Product_Sport_Car $mySportCar = CarFactory :: getCar(“sport car"); // build a new Product_Mini_Car $myLuxuryCar = CarFactory :: getCar(“luxury car"); // build a new Product_Mini_Car $myMiniCar = CarFactory :: getCar(“mini car");
  • 8.
    Abstract Factory DesignPattern Abstract Factory patterns work around a super-factory which creates other factories. Provide an interface for creating families of related or dependent objects without specifying their concrete classes. 8
  • 9.
    Abstract Factory DesignPattern 9 Vehicle Factory Demo Class BMW Factory Volkswagen Factory Car Object SUV Object
  • 10.
    Structure of AbstractFactory 10 <<Class>> Demo Class + main() : void <<Interface>> Car - speed : float - color : color + getCar() : Car <<Interface>> SUV - speed : float - color : color + getSUV() : SUV <<Class>> BMW Car + getCar() : Car <<Class>> Volkwagen Car + getCar() : Car <<Class>> BMW SUV + getSUV() : SUV <<Class>> Volkwagen SUV + getSUV() : SUV <<Class>> BMW Factory + getBMW(type) <<Class>> Volkswagen Factory + getVW(type) <<Class>> Vehicle Factory + getVehicle(type) Implements extends extends extends use extends extends
  • 11.
    Kanushka Gayan SE intern@ Extrogene Software (Pvt) Ltd kanushkanet@gmail.com 071 879 4546 Thank You!