Factory Method Pattern

Introduction and Implementation of Factory Method Pattern


Category: Design Patterns Tags: C#


Introduction

        In previous article we implemented Abstract factory pattern, in this article we will implement factory method pattern which is also a creational design pattern. It says- “Define an interface to create objects and let subclass decide which classes to instantiate”. All creational patterns are used to create objects and all factory patterns use abstract classes.

Implementation

        Here we will implement abstract method pattern using an example of computer components. Let suppose we have to assemble laptops and desktops and we are provided different kind of components for assemble these (i.e. processor, mouse). Let’s create components:

class Component
{ }
class Motherboard :Component
{ }
class Processor :Component
{ }
class Mouse :Component
{ }
class TouchPad :Component
{ }

Above we have component base which is extended to different components like motherboard and processor. Now we need to create a factory which calls a method which initializes required components to assemble desktop/laptop.

abstract class Computer
{
List<Component> _components = new List<Component>();
public Computer()
{
this.AssembleComputer();
}

public List<Component> Components { get { return _components; } }
protected abstract void AssembleComputer();
}

Above class is having a list of components which are required to assemble desktop/laptop. Now we have to create concrete classes which actually decides which components are required for desktop/laptop.

class Desktop : Computer
{
protected override void AssembleComputer()
{
Components.Add(new Motherboard());
Components.Add(new Processor());
Components.Add(new Mouse());
}
}
class Laptop : Computer
{
protected override void AssembleComputer()
{
Components.Add(new Motherboard());
Components.Add(new Processor());
Components.Add(new TouchPad());
}
}

Above classes override AssembleComputer method and instantiate required components. Motherboard and processor are required for both desktop and laptop but mouse, touchpad are required for desktop, laptop respectively. Now let’s assemble laptop and desktop like(executing below code from Main method):

Computer[] computers = new Computer[2];
computers[0] = new Desktop();
computers[1] = new Laptop();
foreach (Computer computer in computers)
{
Console.WriteLine("Components of {0}:", computer.GetType().ToString());
foreach (Component c in computer.Components)
{
Console.WriteLine(c.GetType().ToString());
}
Console.WriteLine("");
}

Above we are creating two references of computer creating objects of laptop and desktop which initialize correct components required for assembling, output of code will be:

 

Components of OOP.FactoryMethod.Desktop:

OOP.FactoryMethod.Motherboard

OOP.FactoryMethod.Processor

OOP.FactoryMethod.Mouse

 

Components of OOP.FactoryMethod.Laptop:

OOP.FactoryMethod.Motherboard

OOP.FactoryMethod.Processor

OOP.FactoryMethod.TouchPad

 

We can see objects for laptop and desktop, TouchPad and Mouse are instantiated for respective sub classes (laptop and desktop).


Like 0 People
Last modified on 11 October 2018
Nikhil Joshi

Nikhil Joshi
Ceo & Founder at Dotnetlovers
Atricles: 126
Questions: 9
Given Best Solutions: 8 *

Comments:

No Comments Yet

You are not loggedin, please login or signup to add comments:

Existing User

Login via:

New User



x