Abstract Factory model-Get McDonald's KFC to cook chicken-fin fries on a date

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.