Design mode Simple Factory + factory method + Abstract Factory

Source: Internet
Author: User
Tags case statement

Simple Factory

Advantages and disadvantages of a simple factory:

Cons: ① violates the OCP (open-closed principle). (When adding a method in the factory to create an object, you need to add a case statement in the original factory). Modifying the original class is a violation of design principles.

② increases the coupling of the client and factory classes.

Pros: ① Remove (non-degrade) the coupling of the client and the specific product. Add a factory class between the client and the specific product, adding coupling between the client and the factory class.

② encapsulates the factory class to realize the reusability of code multiplatform. The process of creating an object is encapsulated into a factory class that can be multiplatform (console +web+winform ... Phone) Call this factory.

③ Encapsulation Factory class, the process of creating objects (concrete logic) contains the necessary logic, according to the requirements of the client, we can dynamically to create objects. For example, in the form of reflection or new.

"Product" class

 public  class   Personclass { public  string  Name {get ; set        public  double  Height {get ; set     class   Whiteperclass:personclass {          class   Blackperclass:personclass {         class   Yellowperclass:personclass { }

"Factory" class:

  Public Static classClassFactory { Public StaticPersonclass CreateObject (stringstr) {Personclass Perclass=NULL; //You can also use logic such as reflection to create objects. When the client calls, it is not in the control of how I create the object. This is one of the benefits of encapsulation.       Switch(str) { Case " White": Perclass=NewWhiteperclass ();  Break;  Case "Black": Perclass=NewBlackperclass ();  Break;  Case "Yellow": Perclass=NewYellowperclass ();  Break; }      returnPerclass; }  }}

"Client":

 Static voidMain (string[] args) {         //Personclass perwhite = new Whiteperclass ();//Although the "Product" class is Personclass, a high-level approach is used, with no factory-created objects or a certain coupling. Personclass Perclass =NULL; Perclass= Classfactory.createobject (" White"); Perclass.name="Small white"; Perclass.height=1.7; Console.WriteLine (Perclass.name+perclass.height); Perclass= Classfactory.createobject ("Black"); Perclass.name="Little Black"; Perclass.height=2.1; Console.WriteLine (Perclass.name+perclass.height); Perclass= Classfactory.createobject ("Yellow"); Perclass.name="Xiao Huang"; Perclass.height=1.8; Console.WriteLine (Perclass.name+perclass.height);      Console.read (); }

Results:

Again the Simple factory:

The product itself uses polymorphism. It is possible to make a new product, which in itself reduces the coupling between the client and the "product", but does not remove the coupling. The coupling is further removed by the factory.

Cons: When adding a product, you need to add a case statement to the factory and modify the original factory class. violated the OCP open closure principle.

Summarize Simple factory:

Passes a string to the factory, returning an object. Create a variety of logic.

Product tree (product grade) + product family

All refer to the product. According to brand and manufacturer classification.

Product Tree (product grade): for products. refers to a product: The brand is different.

① car (product): BMW car, Audi car, Austrian extension car.

② Database (product): Ms-sql,oracle,access,db2,mongodb

Product Family : for the factory. A factory, produce a variety of products, a variety of products collectively known as a "family."

① BMW (Factory) Production: BMW bicycles, BMW tricycle 0.0, BMW's other products, clothing, shoes, chests and so on. It's all a family.

②ms-sql (Factory) Production: User table, Department table, role table

Factory Method

Features: Only one product tree is processed. It can be understood that only a single table is processed. It's also a place that distinguishes it from the abstract factory below.

Pros: It's about solving the drawbacks of simple factories (in violation of OCP). As with the abstract factory, the extension of the program can be changed to the database. Add the corresponding database factory.

Disadvantage: Can not add a "product", can only add a factory, to achieve the function of database Exchange. But only one table can be processed. Adding a table is an abstract factory.

Class Diagram:

Personal Summary: We usually use the factory model, generally belong to a simple factory or abstract factory, because it can be processed on multiple tables.

Abstract Factory:

Pros: To remove the OCP principle from a simple factory, by adding subclasses instead of changing the original class. To add "products".

Cons: Each additional product added: Need to add a product interface +sql products +oracle products, but also to increase the factory interface +sql factory +oracle a method of plant.

"Product" class

User:

  InterfaceIuserdal {voidCRUD (); }  classSqluserdal:iuserdal { Public voidCRUD () {Console.WriteLine ("crud for SQL database user"); }  }  classOracleuserdal:iuserdal { Public voidCRUD () {Console.WriteLine ("crud for Oracle database user"); }  }

Department:

InterfaceIdepartmentdal {voidCRUD (); }  classSqldepartmentdal:idepartmentdal { Public voidCRUD () {Console.WriteLine ("crud for SQL database Department"); }  }  classOracledepartmentdal:idepartmentdal { Public voidCRUD () {Console.WriteLine ("crud for Oracle Database Department"); }  }

"Factory" class

  Interfaceifactory {iuserdal createuserdal ();  Idepartmentdal Createdepartmentdal (); }  classSqlfactory:ifactory { PublicIuserdal Createuserdal () {return NewSqluserdal (); }     PublicIdepartmentdal Createdepartmentdal () {return NewSqldepartmentdal (); }  }  classOraclefactory:ifactory { PublicIuserdal Createuserdal () {return NewOracleuserdal (); }     PublicIdepartmentdal Createdepartmentdal () {return NewOracledepartmentdal (); }  }

Client:

      Static voidMain (string[] args) {         //create sql corresponding DALSqlfactory sqlfactory=Newsqlfactory (); Iuserdal Sqluserdal=Sqlfactory.createuserdal ();         Sqluserdal.crud (); Idepartmentdal Sqldeparmentdal=Sqlfactory.createdepartmentdal ();                 Sqldeparmentdal.crud (); //Replace the database, you need to create the oracle corresponding DALOraclefactory oraclefactory =Neworaclefactory (); Iuserdal Oracleuserdal=Oraclefactory.createuserdal ();         Oracleuserdal.crud (); Idepartmentdal Oracledeparmentdal=Oraclefactory.createdepartmentdal ();         Oracledeparmentdal.crud ();      Console.read (); }

Results:

Summarize:

① understand the product family and product tree (product grade), convenient memory class diagram.

② know the difference between factory method and abstract factory

③ factory methods and abstract factories have improved the shortcomings of simple factories (in violation of OCP).

In order to change the abstract factory add a "product", bring the cumbersome code, you think how to improve it?

Design mode Simple Factory + factory method + Abstract Factory

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.