Design Patterns notes-Abstract Factory patterns

Source: Internet
Author: User

Design Patterns notes-Abstract Factory patterns


Abstract:
Design Pattern learning-abstract factory pattern
This paper introduces the basic concepts of the abstract factory model and provides the C # Programming LanguageProgramExample

Abstract Factory mode provides an interface to the client, so that the client creates objects in multiple product families without specifying a specific type. This document continues

The previous example of the fast food restaurant.
Nowadays, fast food restaurants are often good and gradually grow. To adapt to the eating habits of people in different places, two series (equivalent to product families) of fast food are created: Northern series and southern series.

Column. Each series is headed by a chef.
Abstract Factory mode supports the principle of opening and closing for new product families, but does not support the principle of opening and closing for new products. For example, adding new product families, such as adding American series of fast food (equivalent to adding

A product family), as long as each product interface inherits a corresponding series of products in the United States, do not need to change the existingCode. However, if a new product is added, for example

Products, it does not support the open and closed principle, because you need to add the "steamed bread" method in the chef interface, this requires modifying the existing interface, the upper-layer interface is modified to inherit

The specific classes of this interface also need to be changed.
Conditions for using the abstract factory model:
1. A system should not depend on how a product is created, combined, or expressed.
2. There are multiple product families, and the system only consumes products in one of them.
3. products belonging to the same product family are used together.
4. The system provides a product library. All products are implemented using the same interface.

See the following C # implementation:

Using system;

Namespace abstractfactory
{
/// <Summary>
/// Example of the abstract factory Mode
/// </Summary>
Class abstractfactory
{

// Define the Abstract Factory of the cook
Public interface chef
{
// The public operations of the cook are defined here
// Return abstract noodles
Now.makenoodle ();
// Returns the abstract rice.
Rice makerice ();
// Return abstract bread
Bread makebread ();

}

// Defines the northern chef and implements the chef Interface
Class northchef: Chef
{
Public now.makenoodle ()
{
Console. writeline ("\ n making noodles ...");
Notesnotries = new northnotries ();
Return notify;
}
Public rice makerice ()
{
Console. writeline ("\ n making Northern Rice ...");
Rice rice = new northrice ();
Return rice;
}
Public bread makebread ()
{
Console. writeline ("\ n making bread ..");
Bread = new northbread ();
Return bread;
}
}

// Define the southern chef and implement the chef Interface
Class southchef: Chef
{
Public now.makenoodle ()
{
Console. writeline ("\ n is making noodle... ");
Now.notify = new southnotify ();
Return notify;
}
Public rice makerice ()
{
Console. writeline ("\ n is making southern rice... ");
Rice rice = new southrice ();
Return rice;
}
Public bread makebread ()
{
Console. writeline ("\ n is making Southern bread... ");
Bread = new southbread ();
Return bread;
}
}

// Define noodle Products
Public interface notify
{
// Define public operations for noodles
}

Class northnotify: Notify
{
Public northnotify ()
{
Console. writeline ("\ n a bowl of authentic Northern noodles produced ");
}
}

class southnotify: Notify
{< br> Public southnotify ()
{< br> console. writeline ("\ n a bowl of authentic Southern noodles produced ");
}< BR >}< br>
// define the rice product
Public interface rice
{< br> // The Public operation of the rice is defined here.
}< br>
class northrice: rice
{< br>
Public northrice ()
{< br> console. writeline ("\ n a bowl of authentic Northern Rice is generated, really delicious");
}< BR >}

Class southrice: Rice
{

Public southrice ()
{
Console. writeline ("\ n is produced by a bowl of authentic Southern rice, which is hard to eat ");
}
}

// Define the bread
Public interface bread
{
// Some public operations can be defined here
}
 
Class northbread: bread
{

Public northbread ()
{
Console. writeline ("\ n produced by an authentic Northern bread ");
}
}

class southbread: bread
{< br> Public southbread ()
{< br> console. writeline ("\ n an authentic Southern bread produces");
}< BR >}

/// <Summary>
/// Main entry point of the application.
/// </Summary>
[Stathread]
Static void main (string [] ARGs)
{
// For interface programming, create an object and return the upper interface whenever possible to avoid using a specific class
// The specific process of creating noodles, rice, and breads is hidden below
Chef northchef = new northchef ();
Northchef. makenoodle ();
Northchef. makerice ();
Northchef. makebread ();

Chef southchef = new southchef ();
Southchef. makebread ();
Southchef. makenoodle ();
Southchef. makerice ();

Console. Readline ();
}
}
}

 

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.