Abstract factory pattern with C#
Abstract factory pattern is a creational design pattern. Creational design pattern is deals with object creation mechanism. Object creation mechanism is changed on the basis of problem. Abstract factory pattern provides an interface to create families of related or dependent objects without specifying their concrete class. It is identified on the basis of problem complexity. It is encapsulated the process of instantiation of family of classes. Abstract factory pattern is widely used in framework and libraries to encapsulate the family of classes.
Elements of Abstract Factory Pattern:
- Abstract Factory – An Interface to create families of related or dependent item object.
- Concrete Factory – Implement the interface to create families of related item object.
- Abstract Item – An interface to create concrete item object.
- Concrete Item – Implement the interface to create concrete item object.
- Client – Uses Interface (abstract Item) provided by abstract factory and access concrete object by this interface.
Implementation:
Here I have implemented abstract factory pattern on International Cricket Team by C#. The UML diagram is given below.
Step 1: Create interface for Item class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace AFP
{
/// Interface for Item class
public interface ICricketer
{
string BattingStrength();
string BowlingStrength();
string AllroundingStrength();
string IconPlayer();
}
}
Step 2: Create Factory Interface
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace AFP
{
/// A Factory interface
public interface ICricketerFactory
{
ICricketer GetCricketer(CricketerBase cricketerBase);
}
}
Step 3: Define the type of Base object
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace AFP
{
/// Define the type of Base Object
public enum CricketerBase
{
AustralianCricketer,
BangladeshiCricketer,
EnglishCricketer,
IndianCricketer,
PakistaniCricketer
}
}
Step 4: Create Concrete factory class for Asian Cricketer
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace AFP
{
/// Concrete factory class for Asian Cricketer
public class AsianCricketerFactory : ICricketerFactory
{
public ICricketer GetCricketer(CricketerBase cricketerBase)
{
ICricketer objICricketer = null;
switch (cricketerBase)
{
case CricketerBase.BangladeshiCricketer:
objICricketer = new BangladeshiCricketer();
break;
case CricketerBase.IndianCricketer:
objICricketer = new IndianCricketer();
break;
default:
break;
}
return objICricketer;
}
}
}
Step 5: Create Concrete factory class for European Cricketer
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace AFP
{
/// Concrete factory class for European Cricketer
public class EuropeanCricketerFactory : ICricketerFactory
{
public ICricketer GetCricketer(CricketerBase cricketerBase)
{
ICricketer objICricketer = null;
switch (cricketerBase)
{
case CricketerBase.EnglishCricketer:
objICricketer = new EnglishCricketer();
break;
default:
break;
}
return objICricketer;
}
}
}
Step 6: Create Item class for Bangladeshi Cricketer
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace AFP
{
/// Item class for Bangladeshi Cricketer
public class BangladeshiCricketer : ICricketer
{
public string BattingStrength()
{
return "60%";
}
public string BowlingStrength()
{
return "70%";
}
public string AllroundingStrength()
{
return "85%";
}
public string IconPlayer()
{
return "Shakib Al Hasan";
}
}
}
Step 7: Create Item Class for Indian Cricketer
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace AFP
{
///
/// Item class for Indian Cricketer
///
public class IndianCricketer : ICricketer
{
public string BattingStrength()
{
return "85%";
}
public string BowlingStrength()
{
return "60%";
}
public string AllroundingStrength()
{
return "70%";
}
public string IconPlayer()
{
return "Shachin Tendulkar.";
}
}
}
Step 8: Create Item class for Pakistani Cricketer
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace AFP
{
/// Item class for Pakistani Cricketer
public class PakistaniCricketer : ICricketer
{
public string BattingStrength()
{
return "75%";
}
public string BowlingStrength()
{
return "85%";
}
public string AllroundingStrength()
{
return "75%";
}
public string IconPlayer()
{
return "Shahid Afridi.";
}
}
}
Step 9: Create Item class for English Cricketer
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace AFP
{
/// Item class for English Cricketer
public class EnglishCricketer : ICricketer
{
public string BattingStrength()
{
return "75%";
}
public string BowlingStrength()
{
return "80%";
}
public string AllroundingStrength()
{
return "70%";
}
public string IconPlayer()
{
return "Kavin Pietersen";
}
}
}
Step 10: Create a client class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace AFP
{
/// Client Class
class Program
{
static void Main(string[] args)
{
AsianCricketerFactory objAsianFactory = new AsianCricketerFactory();
ICricketer objIAsianCricketer = objAsianFactory.GetCricketer(CricketerBase.BangladeshiCricketer);
Console.WriteLine("Bangladesh Cricket Team\nBatting Strength:" + objIAsianCricketer.BattingStrength());
Console.WriteLine("Bowling Strength:" + objIAsianCricketer.BowlingStrength());
Console.WriteLine("Allrounding Strength:" + objIAsianCricketer.AllroundingStrength());
Console.WriteLine("Icon Player:" + objIAsianCricketer.IconPlayer());
Console.WriteLine();
EuropeanCricketerFactory objEuropeanFactory = new EuropeanCricketerFactory();
ICricketer objIEuropeanCricketer = objEuropeanFactory.GetCricketer(CricketerBase.EnglishCricketer);
Console.WriteLine("England Cricket Team\nBatting Strength:" + objIEuropeanCricketer.BattingStrength());
Console.WriteLine("Bowling Strength:" + objIEuropeanCricketer.BowlingStrength());
Console.WriteLine("Allrounding Strength:" + objIEuropeanCricketer.AllroundingStrength());
Console.WriteLine("Icon Player:" + objIEuropeanCricketer.IconPlayer());
Console.ReadLine();
}
}
}
Output: