Printed from www.rmfusion.com A Developer website designed for Developers

Abstract Factory Pattern Code Review

Code Download

Define the Pattern Interface and Handler Objects

Define the IAbstractFactory interface to be used as the "blueprint" for the factory application classes.

interface IAbstractFactory { IAbstractBag CreateBag(); IAbstractShoes CreateShoes(); IAbstractBelts CreateBelts(); }

This interface is used to instantiate the different product classes (objects).

The FactoryGucci class (object) represents a factory class that instantiates a particular set of product classes. These product classes are related to each other.

class FactoryGucci : IAbstractFactory { public IAbstractBag CreateBag() { return new GucciBag(); } public IAbstractShoes CreateShoes() { return new GucciShoes(); } public IAbstractBelts CreateBelts() { return new GucciBelts(); } }

The FactoryPoochy class (object) represents a factory class that instantiates a different set of product classes. These product classes are related to each other.

class FactoryPoochy : IAbstractFactory { public IAbstractBag CreateBag() { return new PoochyBag(); } public IAbstractShoes CreateShoes() { return new PoochyShoes(); } public IAbstractBelts CreateBelts() { return new PoochyBelts(); } }

Define the Product Interfaces and Handler Objects

Define the IAbstractBag interface to be used as the "blueprint" for the product application classes.

interface IAbstractBag { String Material { get; } } interface IAbstractShoes { Int32 Price { get; } } interface IAbstractBelts { String Material { get; } Int32 Price { get; } }

This interface is used to define the different product classes (objects).

The GucciBag and PoochyBag classes (objects) represent particular product classes.

class GucciBag : IAbstractBag { public String Material { get { return "Crocodile skin"; } } } class PoochyBag : IAbstractBag { public String Material { get { return "Plastic"; } } }

The two classes (objects)are unrelated to each other and represent one type of product against another type of product.

The GucciShoes and PoochyShoes classes (objects) represent particular product classes.

class GucciShoes : IAbstractShoes { public Int32 Price { get { return 1000; } } } class PoochyShoes : IAbstractShoes { public Int32 Price { get { return 1000 / 3; } } }

The two classes (objects) are unrelated to each other and represent one type of product against another type of product.

The GucciBelts and PoochyBelts classes (objects) represent particular product classes.

class GucciBelts : IAbstractBelts { public String Material { get { return "Leather"; } } public Int32 Price { get { return 2000; } } } class PoochyBelts : IAbstractBelts { public String Material { get { return "Plastic"; } } public Int32 Price { get { return 2000 / 3; } } }

The two classes (objects) are unrelated to each other and represent one type of product against another type of product.

Define the Client Object

The Client class is responsible for invoking the required methods against a particular product factory.

class Client { private IAbstractBag bag; private IAbstractShoes shoes; private IAbstractBelts belts;
public Client(IAbstractFactory factory) { bag = factory.CreateBag(); shoes = factory.CreateShoes(); belts = factory.CreateBelts(); }
public void ClientMain() { Console.WriteLine(String.Format("I bought a bag which is made from {0}.", bag.Material)); Console.WriteLine(String.Format("I bought some shoes which cost {0}.", shoes.Price.ToString())); Console.WriteLine(String.Format("I bought a belt made from {0} and cost {1}.", belts.Material, belts.Price.ToString())); } }

Use the Abstract Factory Pattern

The Program class creates an instance of the Client class (object), which invokes methods in the different types of factory classes (objects).

class Program { static void Main(string[] args) { Client client = null;
FactoryGucci gucci = new FactoryGucci(); client = new Client(gucci); client.ClientMain(); client = null;
Console.WriteLine();
FactoryPoochy poochy = new FactoryPoochy(); client = new Client(poochy); client.ClientMain(); client = null;
Console.ReadLine(); } }