Factory Design
Pattern
Marya Amouri
Introduction
Every codebase can be
improved
Using factory method
is a more extensible
alternative
Factory
“A factory is an
object for
creating objects”
Design Patterns
Creational Structural Behavioral
Factory is
Creational
Design Pattern
Factory Characteristics
Client Creator Product
Ask for a created Facilitates a The product of the
product creation creation
Why Factory?
reducing code single point more modular reusability more
duplication for objects and extensible testable
creation code code
Coupling
Tight Coupling Loosing Coupling
Factory Design Pattern Flavors
Simple Factory Abstract
Factory Method Factory
Simple Factory
A factory class creates
and returns instances
of various classes based
on input parameters.
Factory Method
Each subclass of a
superclass provides its
own factory method for
creating instances of its
specific class.
Intent Factory Method is a creational
design pattern that provides an
interface for creating objects
in a superclass, but allows
subclasses to alter the type of
objects that will be created
Factory Method
ICreator
FactoryMethod() Product = FactoryMethod()
Operation()
IProduct
Product Creator
FactoryMethod() return concrete Product
Operation()
Problem
Problem
When adding Sea Track:
Most of your code is coupled to the
Truck class.
Adding Ships into the app would require
making changes to the entire codebase.
Structure IProduct = CreateProduct()
IProduct
ICreator
+CreateProduct()
+DoStuff()
ProductA ProductB
+DoStuff() +DoStuff() CreatorB
CreatorA
+CreateProduct() +CreateProduct()
Return new ProductA
Solution IButton = CreateButton()
IDialog
IButton
+Render()
+Render()
+CreateButton()
+OnClick()
Windows Web
Button Button
WindowsDialog WebDialog
+Render() +Render()
+OnClick() +OnClick() +CreateProduct() +CreateProduct()
Return new WindowsButton()
Demo Code
// Define the interface for the Product
interface IProduct {
void Use();
}
class ProductA : IProduct
{
public void Use() { Console.WriteLine("Using ProductA");
}}
class ProductB : IProduct {
public void Use() {
Console.WriteLine("Using ProductB");
}}
interface IProductFactory {
IProduct CreateProduct();
}
class ProductAFactory : IProductFactory {
public IProduct CreateProduct() {
return new ProductA();
}
}
class ProductBFactory : IProductFactory { public IProduct CreateProduct() {
return new ProductB();
}
}
class Program
{
static void Main(string[] args)
{
IProductFactory factoryA = new ProductAFactory();
IProduct productA = factoryA.CreateProduct();
productA.Use();
IProductFactory factoryB = new ProductBFactory();
IProduct productB = factoryB.CreateProduct();
productB.Use();
}
}
Abstract Factory
Abstract Factory
is a creational design
pattern that lets you
produce families of related
objects without specifying
their concrete classes.
Abstract
Factory
factory interface is defined
that has several factory
methods for creating
families of related objects
Intent
Provides interface for
creating families of related
or dependent objects
without specifying there
concrete classes
Motivations
Chair Sofa Coffee Table
Art
Deco
Victorian
Modern
Problem
Furniture
Store Class-
Diagram
ICoffee
IChair
Table
Modern Victorian
Modern Victorian ArtCoffee
ArtChair Coffee Coffee
Chair Chair Table
Table Table
ISofa
Modern Victorian
ArtSofa
Sofa Sofa
Furniture Store
Chair Sofa Coffee Table
Art
Deco
Victorian
Modern
Furniture Store
Chair Sofa Coffee Table
Art
Deco
Victorian
Modern
solution
IFactory
-------------------------------------
+CreateChair(): IChair
+CreateCoffeTable():ICoffeeTablr
+CreateSofa():ISofa
ModernFactory VictorianFactory ArtDecoFactory
------------------------------------- ------------------------------------- -------------------------------------
+CreateChair(): IChair +CreateChair(): IChair +CreateChair(): IChair
+CreateCoffeTable():ICoffeeTablr +CreateCoffeTable():ICoffeeTablr +CreateCoffeTable():ICoffeeTablr
+CreateSofa():ISofa +CreateSofa():ISofa +CreateSofa():ISofa
Structure
Code
// Abstract Product A
interface IProductA {stringGetName( );}
// Abstract Product B
interface IProductB { string GetName( ); }
Code
// Concrete Product A1
class ProductA1 : IProductA
{
public string GetName() { return "Product A1"; }
}
// Concrete Product A2 class ProductA2 : IProductA {
public string GetName() { return "Product A2"; }
}
Code
// Concrete Product B1
class ProductB1 : IProductB
{ public string GetName() { return "Product B1"; } }
// Concrete Product B2
class ProductB2 : IProductB
{ public string GetName() { return "Product B2"; } }
Code
// Abstract Creator
interface ICreator {
IProductA CreateProductA();
IProductB CreateProductB(); }
// Concrete Creator 1
class Creator1 : ICreator {
public IProductA CreateProductA() { return new ProductA1(); }
public IProductB CreateProductB() { return new ProductB1(); } }
Code
// Concrete Creator 2
class Creator2 : ICreator {
public IProductA CreateProductA() {return new ProductA2(); }
public IProductB CreateProductB() { return new ProductB2(); } }
Factory
Purpose
method
vs Structure
Abstract Dependency
factory
Thank You