Factory method mode-what Hamburger do you want

Source: Internet
Author: User

Factory method pattern analogy
Ask girls to go to McDonald's for a hamburger. Different girls have different tastes. Remember that it is an annoying thing. I generally use the factory method-factory method mode, take the girl to the waiter and say "I want a hamburger". What kind of hamburger should I ask the girl to tell the waiter directly.

Factory method pattern intent
The factory method defines an interface used to create objects, so that the subclass decides which class to instantiate. The factory method delays the instantiation of a class to its subclass.

Factory method pattern class diagram  Factory method mode implementation /// <Summary>
/// In the factory method mode, providing hamburgers of different tastes for girls is equivalent to my responsibility
/// </Summary>
Interface igirl
{
Ihamburg demandhamburg ();
} // <Summary>
/// Girl who likes to eat the first hamburger
/// </Summary>
Class girl1: igirl
{
Public ihamburg demandhamburg ()
{
Return new hamburg1 ();
}
} // <Summary>
/// Girl who likes to eat the second hamburger
/// </Summary>
Class girl2: igirl
{
Public ihamburg demandhamburg ()
{
Return new hamburg2 ();
}
} // <Summary>
/// Hamburger abstraction for different tastes
/// </Summary>
Interface ihamburg
{
Void improvehamburg ();
} // <Summary>
/// Hamburger of the first taste
/// </Summary>
Class hamburg1: ihamburg
{
Public void improvehamburg ()
{
Console. writeline ("hamburger for girls with the first taste ");
}
} // <Summary>
/// Hamburger of the second taste
/// </Summary>
Class hamburg2: ihamburg
{
Public void improvehamburg ()
{
Console. writeline ("hamburger with second taste ");
}
} // <Summary>
/// Factory method mode client application
/// </Summary>
Class Program
{
Static void main (string [] ARGs)
{
// Girls who like the first hamburger
Igirl girl = new girl1 (); // provides a girl's favorite hamburger
Ihamburg Hamburg = girl. demandhamburg ();
Hamburg. improvehamburg (); console. Read ();
}
} Factory method mode Summary I am no longer responsible for remembering what kind of hamburger every girl wants, but instead giving it to them. That is, the core factory category in the system is no longer responsible for the creation of all products, but the specific creation work is handed over to the Child class. The core factory category is only responsible for providing the interface for implementing the specific factory class, become an abstract factory role without touching the details of which product class should be instantiated.

Original blog post on Zhu Liit:ArticleAddress: http://www.zhuli8.com/sjms/method.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.