By Subhash Balachandran
Abstract Factory pattern is a creational pattern. Creational Patterns deal with best way to create an object. Abstract patterns define the interface to select or return one from the several families of related objects. The best non-software example is stamping equipment that create the auto body parts. The same machinery is used for generating the right door, left door and so on.
The abstract factory is a factory object that returns one of several factories. It can be used to return one of several related classes of objects, each of which can return several different objects on request.
In this case the interface Factory has two concrete implementations, ConcreteFactory1 and ConcreteFactory2. The getObject () inside these concrete classes returns Derived1 and Derived2 objects respectively. The client can decide which ConcreteFactory class has to be used during the run-times.
C# Example
Consider a brick game that generates the object each and every time that can be a Rectangle, Straight-line, Square, HShape, and ReverseH so on.
Object selection is decided by the client (i.e.) which factory is to be called.
using System;
using System.Collections.Generic;
using System.Text;
namespace BrickGame4AbsctractFactory
{
interface IBrickObjects
{
void Rotate();
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace BrickGame4AbsctractFactory
{
interface ISelect
{
IBrickObjects GetBrickObject();
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace BrickGame4AbsctractFactory
{
class ObjectH : IBrickObjects
{
public void Rotate()
{
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace BrickGame4AbsctractFactory
{
class ObjectHFactory:ISelect
{
public IBrickObjects GetBrickObject()
{
return (new ObjectH());
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace BrickGame4AbsctractFactory
{
class Rectangle : IBrickObjects
{
public void Rotate()
{
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace BrickGame4AbsctractFactory
{
class RectangleFactory : ISelect
{
public IBrickObjects GetBrickObject()
{
return (new Rectangle());
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace BrickGame4AbsctractFactory
{
class ReverseH : IBrickObjects
{
public void Rotate()
{
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace BrickGame4AbsctractFactory
{
class ReverseHFactory : ISelect
{
public IBrickObjects GetBrickObject()
{
return (new ReverseH());
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace BrickGame4AbsctractFactory
{
class Square : IBrickObjects
{
public void Rotate()
{
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace BrickGame4AbsctractFactory
{
class SquareFactory : ISelect
{
public IBrickObjects GetBrickObject()
{
return (new Square());
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace BrickGame4AbsctractFactory
{
class Straightline : IBrickObjects
{
public void Rotate()
{
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace BrickGame4AbsctractFactory
{
class StraightlineFactory : ISelect
{
public IBrickObjects GetBrickObject()
{
return (new Straightline());
}
}
}
Client Code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace BrickGame4AbsctractFactory
{
public partial class Main : Form
{
ISelect m_ObjSelect;
public Main()
{
InitializeComponent();
}
private IBrickObjects ActivateObject(string ObjectName)
{
switch (ObjectName)
{
case "Square":
m_ObjSelect = new SquareFactory();
break;
case "Straightline":
m_ObjSelect = new StraightlineFactory();
break;
case "Rectangle":
m_ObjSelect = new RectangleFactory ();
break;
}
return m_ObjSelect.GetBrickObject() ;
}
private void Main_Load(object sender, EventArgs e)
{
IBrickObjects l_objBrickObject = ActivateObject("Square");
l_objBrickObject.Rotate();
}
}
}
The IBrickObjects interface has the required method that has to be implemented by all brick objects. This class forms all the base class for all the brick objects.
The ISelect interface forms the interface for the factory objects of all the Brick object which will get the exact object on calling the GetObject method.