Name: Bo Linna
Class: E7
Year: 4
Subject: OOAD
Question
1. អ្វីទៅ Abstract Factory Design Pattern
2. គូរ Basic UML Class Diagram
3. លើកយកឧទាហរណ៍ (ចំណោទបញ្ហា ) មកបញ្ជា ក់បន្ថែមដោយមានគូរ UML Class
Diagram ឲ្យត្រូវគំរូ UML Class Diagram នៃ Design Pattern នេះ ដើម្ប ី
ដោះស្រាយចំណោទបញ្ហា ព្រមទាំងសរសេរកូដឲ្យឆ្លើយតបនង
ឹ UML Class
Diagram នៃដំណោះស្រាយ។
Answer
1. Abstract Factory Design Pattern is a creational design pattern that lets you
produce families of related objects without specifying their
concrete classes.
Abstract factory patterns work around a super-factory which create
other factories.
Abstract factory pattern implementation provides us with a
framework that allows us to create objects that follow a general
pattern. So at runtime, the abstract factory is coupled with any
desired concrete factory which can create objects of the desired
type.
2. UML diagram
+ code
public interface AbstractFactory
AbstractProductA CreateProductA();
AbstractProductB CreateProductB();
public class ConcreteFactoryA : AbstractFactory
public AbstractProductA CreateProductA()
{
return new ProductA1();
public AbstractProductB CreateProductB()
return new ProductB1();
public class ConcreteFactoryB : AbstractFactory
public AbstractProductA CreateProductA()
return new ProductA2();
public AbstractProductB CreateProductB()
return new ProductB2();
public interface AbstractProductA { }
public class ProductA1 : AbstractProductA { }
public class ProductA2 : AbstractProductA { }
public interface AbstractProductB { }
public class ProductB1 : AbstractProductB { }
public class ProductB2 : AbstractProductB { }
public class Client
private AbstractProductA _productA;
private AbstractProductB _productB;
public Client(AbstractFactory factory)
_productA = factory.CreateProductA();
_productB = factory.CreateProductB();
3. Example
interface VehicleFactory
Bike GetBike(string Bike);
Scooter GetScooter(string Scooter);
class HondaFactory : VehicleFactory
public Bike GetBike(string Bike)
switch (Bike)
{
case "Sports":
return new SportsBike();
case "Regular":
return new RegularBike();
default:
throw new ApplicationException(string.Format("Vehicle '{0}' cannot be
created", Bike));
public Scooter GetScooter(string Scooter)
switch (Scooter)
case "Sports":
return new Scooty();
case "Regular":
return new RegularScooter();
default:
throw new ApplicationException(string.Format("Vehicle '{0}' cannot be
created", Scooter));
}
}
class HeroFactory : VehicleFactory
public Bike GetBike(string Bike)
switch (Bike)
case "Sports":
return new SportsBike();
case "Regular":
return new RegularBike();
default:
throw new ApplicationException(string.Format("Vehicle '{0}' cannot be
created", Bike));
public Scooter GetScooter(string Scooter)
switch (Scooter)
case "Sports":
return new Scooty();
case "Regular":
return new RegularScooter();
default:
throw new ApplicationException(string.Format("Vehicle '{0}' cannot be
created", Scooter));
interface Bike
string Name();
interface Scooter
string Name();
class RegularBike : Bike
public string Name()
{
return "Regular Bike- Name";
class SportsBike : Bike
public string Name()
return "Sports Bike- Name";
class RegularScooter : Scooter
public string Name()
return "Regular Scooter- Name";
class Scooty : Scooter
public string Name()
{
return "Scooty- Name";
class VehicleClient
Bike bike;
Scooter scooter;
public VehicleClient(VehicleFactory factory, string type)
bike = factory.GetBike(type);
scooter = factory.GetScooter(type);
public string GetBikeName()
return bike.Name();
public string GetScooterName()
return scooter.Name();
class Program
{
static void Main(string[] args)
VehicleFactory honda = new HondaFactory();
VehicleClient hondaclient = new VehicleClient(honda, "Regular");
Console.WriteLine("******* Honda **********");
Console.WriteLine(hondaclient.GetBikeName());
Console.WriteLine(hondaclient.GetScooterName());
hondaclient = new VehicleClient(honda, "Sports");
Console.WriteLine(hondaclient.GetBikeName());
Console.WriteLine(hondaclient.GetScooterName());
VehicleFactory hero = new HeroFactory();
VehicleClient heroclient = new VehicleClient(hero, "Regular");
Console.WriteLine("******* Hero **********");
Console.WriteLine(heroclient.GetBikeName());
Console.WriteLine(heroclient.GetScooterName());
heroclient = new VehicleClient(hero, "Sports");
Console.WriteLine(heroclient.GetBikeName());
Console.WriteLine(heroclient.GetScooterName());
Console.ReadKey();