Abstract Factory Pattern

Introduction and Implementaion of Abstract Factory Pattern


Category: Design Patterns Tags: C#


Introduction

        Abstract Factory Design Pattern is one of creational design pattern. Creational design patterns basically deal with object creation or group of related object. This pattern instantiates the objects in their basic form (base classes) and provides more flexibility in creation of targeted objects.

Implementation

        This design pattern separates the process of object creation. Abstract factory uses interface so client doesn’t know which object will be initialized at runtime. Let’s have an example to understand this.

"Let’s suppose one company sells laptops and desktops with some offers where user gets laptop bag for free if he buys laptop or a desktop table in case of desktop buy. Laptop/Desktop might be having some different properties and some common."

First we create Computer and Accessory classes:

abstract class Computer
{
public string Processor { get; set; }
}
abstract class Accessory
{
}

Processor is common hardware in Both Laptop and Desktop so we implemented it in Computer class. Now we have to use above base classes and we extend Computer class in Laptop and Desktop as well Accessory class in LaptopBag and DesktopTable

class Laptop : Computer
{
public string TouchPad { get; set; }
public Laptop()
{
Processor = "Intel i3";
TouchPad = "HP";
}
}

class Desktop : Computer
{
public string Mouse { get; set; }

public Desktop()
{
Processor = "Intel i5";
Mouse = "Moserbeer";
}
}

class LaptopBag : Accessory
{ }

class DesktopTable : Accessory
{ }

Laptop needs touchpad and desktop needs mouse, these cannot be implemented in Computer class because they are specific to Laptop and Desktop. Now we have to create Abstract Factory class which returns Computer and accessory.

abstract class ComputerAndAccessory
{
public abstract Computer GetComputer();
public abstract Accessory GetAccessory();
}

Now we will create Concrete Classes Extending ComputerAndAccessory:

class LaptopFactory : ComputerAndAccessory
{
public override Computer GetComputer()
{
return new Laptop();
}

public override Accessory GetAccessory()
{
return new LaptopBag();
}
}

class DesktopFactory : ComputerAndAccessory
{
public override Computer GetComputer()
{
return new Desktop();
}

public override Accessory GetAccessory()
{
return new DesktopTable();
}
}

Finally, we have created factories. Actually factory does type casting of specific type to general type (Returns Laptop/Desktop in form of Computer) as well it overrides correct method to initialize the correct classes.

Now we will create client class which will be using abstract factory and abstract product.

class Client
{
public Computer Computer { get; private set; }
public Accessory Accessory { get; private set; }

public Client(ComputerAndAccessory compAndAsso)
{
Computer = compAndAsso.GetComputer();
Accessory = compAndAsso.GetAccessory();
}
}

Which has two properties Computer and Accessory initializing them in constructor. Let’s see how does it work:

ComputerAndAccessory compassry = new DesktopFactory();
Client maincls = new Client(compassry);
Console.WriteLine(maincls.Computer.GetType().ToString());
Console.WriteLine(maincls.Accessory.GetType().ToString());
Desktop desktop = (Desktop)maincls.Computer;
Console.WriteLine(desktop.Processor);
Console.WriteLine(desktop.Mouse);

Console.WriteLine("------------------------");

compassry = new LaptopFactory();
maincls = new Client(compassry);
Console.WriteLine(maincls.Computer.GetType().ToString());
Console.WriteLine(maincls.Accessory.GetType().ToString());
Laptop laptop = (Laptop)maincls.Computer;
Console.WriteLine(laptop.Processor);
Console.WriteLine(laptop.TouchPad);

Here is the output on console:

OOP.Abstract.Desktop

OOP.Abstract.DesktopTable

Intel i5

Moserbeer

------------------------

OOP.Abstract.Laptop

OOP.Abstract.LaptopBag

Intel i3

HP

 

        Above code initializes Desktop and Laptop Factory using reference of ComputerAndAccessory class which is base of these two. We can see it automatically loads correct classes using overridden methods but we actually create object of factory and get correct classes initialized but client doesn’t know it.


Like 0 People
Last modified on 1 September 2018
Nikhil Joshi

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

Comments:

No Comments Yet

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

Existing User

Login via:

New User



x