Abstract Factory Design Pattern là mẫu thiết kế trong nhóm Creational, tạo ra các họ đối tượng mà không cần chỉ ra lớp cụ thể. Đây là phiên bản mở rộng của kiểu thiết kế Factory giúp quản lý nhiều họ đối tượng cùng lúc
Ví dụ : Thiết kế một chương trình giúp khách hàng lựa chọn đồ nội thất theo phong cách hiện tại hoặc cổ điển
Giải pháp thực hiện
Tạo ra các interface
AbstractChair: giao diện chung cho các sản phẩm ghế.
AbstractSofa: Giao diện chung cho các sản phẩm sofa.
AbstractCoffeeTable: Giao diện chung cho các sản phẩm bàn cà phê.
AbstractFurnitureFactory: Giao diện định nghĩa phương thức tạo ra các đồ nội thất.
Triển khai mẫu nội thất hiện đại, với từng công dụng hoạt động MordernChair, ModernSofa, ModernCoffeTable
Triển khai mẫu nội thất cổ điển, với từng công dụng hoạt động
Tạo ra các sản phẩm nội thất theo các mẫu hiện đại và cổ điển ModernFurnitureFactory, VictorianFunitureFactory
Tạo ra một lớp khách hàng, giúp khách hàng lựa chọn đồ nội thất
Cuối cùng, sử dụng
Ví dụ : Thiết kế một chương trình giúp khách hàng lựa chọn đồ nội thất theo phong cách hiện tại hoặc cổ điển
Giải pháp thực hiện
Tạo ra các interface
AbstractChair: giao diện chung cho các sản phẩm ghế.
AbstractSofa: Giao diện chung cho các sản phẩm sofa.
AbstractCoffeeTable: Giao diện chung cho các sản phẩm bàn cà phê.
AbstractFurnitureFactory: Giao diện định nghĩa phương thức tạo ra các đồ nội thất.
#region Interface for Chair/Sofa/CoffeTable
// Ghế dùng để ngồi
interface AbstractChair
{
void SitOn();
}
// Ghế Sofa dùng để thư giãn
interface AbstractSofa
{
void Relax();
}
// Bàn Caffe dùng để đặt đồ vật
interface AbstractCoffeTable
{
void PlaceItem();
}
// Tạo interface định nghĩa các phương thức tạo ra đồ nội thất
interface AbstractFurnutureFactory
{
AbstractChair CreateChair();
AbstractSofa CreateSofa();
AbstractCoffeTable CreateCoffeTable();
}
#endregion
Triển khai mẫu nội thất hiện đại, với từng công dụng hoạt động MordernChair, ModernSofa, ModernCoffeTable
#region Create Modern Funiture
// Tạo các lớp nội thất phong cách hiện đại
class MordernChair : AbstractChair
{
public void SitOn()
{
Console.WriteLine("Sitting on a mordern chair !");
}
}
class ModernSofa : AbstractSofa
{
public void Relax()
{
Console.WriteLine("Relaxing on a modern sofa");
}
}
class ModernCoffeTable : AbstractCoffeTable
{
public void PlaceItem()
{
Console.WriteLine("Place a coffee on a modern Coffee Table !");
}
}
#endregion
Triển khai mẫu nội thất cổ điển, với từng công dụng hoạt động
#region Create Victorian Funiture
// Tạo các lớp nội thất phong cách Victorian VicChair, VicSofa, VicCoffeeTable
class VicChair : AbstractChair
{
public void SitOn()
{
Console.WriteLine("Sitting on a victorian chair !");
}
}
class VicSofa : AbstractSofa
{
public void Relax()
{
Console.WriteLine("Relaxing on a victorian sofa");
}
}
class VicCoffeeTable : AbstractCoffeTable
{
public void PlaceItem()
{
Console.WriteLine("Place a coffee on a victorian coffee table !");
}
}
#endregion
Tạo ra các sản phẩm nội thất theo các mẫu hiện đại và cổ điển ModernFurnitureFactory, VictorianFunitureFactory
#region Create Class Style for Funiture
class ModernFurnitureFactory : AbstractFurnutureFactory
{
public AbstractChair CreateChair()
{
return new MordernChair();
}
public AbstractSofa CreateSofa()
{
return new ModernSofa();
}
public AbstractCoffeTable CreateCoffeTable()
{
return new ModernCoffeTable();
}
}
class VictorianFunitureFactory : AbstractFurnutureFactory
{
public AbstractChair CreateChair()
{
return new VicChair();
}
public AbstractSofa CreateSofa()
{
return new VicSofa();
}
public AbstractCoffeTable CreateCoffeTable()
{
return new VicCoffeeTable();
}
}
#endregion
Tạo ra một lớp khách hàng, giúp khách hàng lựa chọn đồ nội thất
class Client
{
private AbstractChair? _chair;
private AbstractSofa? _sofa;
private AbstractCoffeTable? _coffeTable;
public Client(AbstractFurnutureFactory factory)
{
_chair = factory.CreateChair();
_sofa = factory.CreateSofa();
_coffeTable = factory.CreateCoffeTable();
}
public void ShowSet()
{
_chair?.SitOn();
_sofa?.Relax();
_coffeTable?.PlaceItem();
}
}
Cuối cùng, sử dụng
Client client = new Client(new ModernFurnitureFactory());
client.ShowSet();
إرسال تعليق