Abstract factory pattern metaphor
It is essential to catch up with girls. McDonald's chicken wings, French fries and KFC's chicken wings and French fries are what girls like to eat. Although the taste is different, whether you take girls to McDonald's or KFC, just say "two chicken wings and two packs of fries" to the waiter. McDonald's and KFC produce chicken wings and chips. Factory. This process is the abstract factory mode.
Abstract factory pattern intent Abstract Factory Model (Abstract Factory) To create a series of related or interdependent interfaces without specifying their specific classes.
Abstract factory pattern class diagram
Abstract Factory mode implementation
/// <Summary>
/// Abstract Factory interface-fast food restaurant, which can be made of chicken wings and chips
/// </Summary>
Interface iquikmeal
{
Iwing createwing ();
Ichips createchips ();
}
/// <Summary>
/// Specific factory-KFC, make your own chicken wings and chips
/// </Summary>
Class KFC: iquikmeal
{
Public iwing createwing ()
{
Return new kfc_wing ();
}
Public ichips createchips ()
{
Return new kfc_chips ();
}
}
/// <Summary>
/// Specific factory-McDonald's, make your own chicken wings and chips
/// </Summary>
Class MCD: iquikmeal
{
Public iwing createwing ()
{
Return new mcd_wing ();
}
Public ichips createchips ()
{
Return new mcd_chips ();
}
}
/// <Summary>
/// Abstract product-chicken wings, which are produced by two fast food restaurants, KFC and McDonald's
/// </Summary>
Interface iwing
{
String getwing ();
}
/// <Summary>
/// Chicken wings made by KFC
/// </Summary>
Class kfc_wing: iwing
{
Public String getwing ()
{
Return "KFC's wing! ";
}
}
/// <Summary>
/// Chicken wings made by McDonald's
/// </Summary>
Class mcd_wing: iwing
{
Public String getwing ()
{
Return "McD's wing! ";
}
}
/// <Summary>
/// Abstract product-fries, which are produced by two fast food restaurants
/// </Summary>
Interface ichips
{
String getchips ();
}
/// <Summary>
/// Fried fries made by KFC
/// </Summary>
Class kfc_chips: ichips
{
Public String getchips ()
{
Return "KFC's chips! ";
}
}
/// <Summary>
/// Fries made by McDonald's
/// </Summary>
Class mcd_chips: ichips
{
Public String getchips ()
{
Return "McD's chips! ";
}
}
/// <Summary>
/// Client application
/// </Summary>
Class Program
{
Static void main (string [] ARGs)
{
// Select KFC for mm. If McDonald's is selected, you only need to replace KFC with MCD.
Iquikmeal meal = new KFC ();
// I want to eat chicken wings
Console. writeline (meal. createwing (). getwing ());
// I want to eat French fries
Console. writeline (meal. createchips (). getchips ());
Console. Read ();
}
}
Abstract Factory mode Summary
The biggest benefit of the abstract factory is that a fast food restaurant can be easily selected during the appointment, and the production process of the fast food restaurant has nothing to do with our customers, however, if a girl wants to have a drink or select a Pizza Hut, it will be troublesome, but it can be expanded in the same way.
Original blog post on Zhu Liit:ArticleAddress: http://www.zhuli8.com/sjms/factory.html