Design pattern notes - abstract factory model

xiaoxiao2021-03-06  62

Design pattern notes - abstract factory model

Abstract: Introduced the basic concepts of abstract factory model, and give examples of program written in C # language

The abstract factory model provides an interface to the client so that the client creates objects in multiple product families without having to specify specific types. This article is still an example of the previous fast food restaurant. Now, the fast food store is often good, gradually developing, in order to fit the eating habits of different local people, two series (equivalent to product family) fast food, northern series and southern series. Each series is a big kitchen. Abstract factory models support the opening and closing principles for new product families, but the new product does not support the opening and closing principles. For example, add new product families, such as increasing the US series of fast food (equivalent to adding a product family), as long as you inherit a corresponding American product from each product interface, you do not need to change the existing code. But if new products are added, for example, the "Shantou" product is added, it does not support the opening and closing principle, because you have to increase the way to "Shantou" in the chef interface, this is to modify the existing interface. The upper layer interface is modified, inheriting the specific class of this interface. Conditions using abstract factory modes: 1 One system should not depend on how the product is created, combined, and expressed. 2 There are multiple product families, and the system only consumes products in one of the products in one of the products that belong to a product family. 4 The system provides a library of a product, all products are implemented in the same interface.

Look at the C # implementation below:

Using system;

Namespace AbstractFactory {///

/// Abstract factory mode example /// class abstractFactory {

// Define the chef's abstract factory public interface chef {// Here the chef's public operation // Return abstract noodle noodle makeenoodle (); // Return abstract rice rice makerice (); // Return abstract bread bow makebread ();

}

// Define the northern chef, realize the chef interface class northchef: Chef {public noodle makeenoodle () {console.writeline ("/ n make northern strips ..."); noide noodle = new northoodle (); return noodle;} public rice Makerice () {"/ N production Northern rice ..."); Rice Rice = new northrice (); returnrate;} public bow} ("/ N is making a northern bag. Bread break = new northbread (); return break;}}

// Define the South Chef, realize the chef interface class Southchef: Chef {public noodle makeenoodle () {Console.Writeline ("/ N is making south side ..."); noodle noodle = new Southnoodle (); return noodle;} public Rice makerice () {console.writeline ("/ N is making southern rice ..."); Rice Rice = new SouthRice (); return rice;} public bow makebread () {console.writeline ("/ N is making southern Bread ... "); Bread Bread = new Southbread (); returnide}} // Define Note Product Public Interface Noodle {// Here the noodle of the noodle} Class Northnoodle: Noodle {public Northnoodle () {Console. WriteLine ("/ N a bowl of authentic northern strips");}}

Class Southnoodle: Noodle {public Southnoodle () {Console.Writeline ("/ N a bowl of authentic southern strip generation");}} // Define rice products public interface rice {// Here the rice public operation} Class Northrice : Rice {public northrice () {Console.writeLine ("/ N a bowl of authentic northern rice meals, really delicious");}}

Class SouthRice: Rice {public Southrice () {Console.writeLine ("/ N a bowl of authentic southern rice is produced, it is really difficult to eat");}}

// Define bread public interface bread {/ / here can define public operations of some bread} Class Northbread: BREAD {public northbread () {Console.writeLine ("/ N A authentic northern bag generation");}}

Class Southbread: Bread {public Southbread () {console.writeline ("/ N an authentic southern bread is generated);}}

///

/// The primary entry point of the application. /// [static] static void main (String [] args) {// Programming for the interface, create an object as much as possible to return to the upper interface, avoid using the specific class // below to hide the specific creation of various noodles, rice bread and processes Chef northChef = new northChef (); northChef.MakeNoodle (); northChef.MakeRice (); northChef.MakeBread (); Chef southChef = new SouthChef (); southChef.MakeBread (); southChef.MakeNoodle (); SOUTHCHEF.MAKERICE (); console.readline ();}}}

转载请注明原文地址:https://www.9cbs.com/read-86045.html

New Post(0)