Simple Factory mode

Source: Internet
Author: User

Design mode Simple Factory + factory method + Abstract Factory

Read Catalogue

    • Simple Factory
    • Product tree (product grade) + product family
    • Factory method
    • Abstract Factory:
Back to the top of the 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 class ClassFactory  {public    static Personclass CreateObject (String str)    {      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 = new Whiteperclass ();          break;        Case "BLACK":          perclass = new Blackperclass ();          break;        Case "Yellow":          perclass = new Yellowperclass ();          break;      }      return perclass;}}  }

"Client":

static void Main (string[] args)      {         //personclass perwhite = new Whiteperclass ();//Although the "Product" class Personclass, A high-level approach is used to create objects without a factory, or to have some 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 = "small black";         Perclass.height = 2.1;         Console.WriteLine (Perclass.name + perclass.height);         Perclass = Classfactory.createobject ("yellow");         Perclass.name = "small yellow";         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.

Back to top product tree (product level) + 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

Back to top 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.

Back to the top of the 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:

  Interface Iuserdal  {    void CRUD ();  }  Class Sqluserdal:iuserdal  {public    void crud ()    {      Console.WriteLine ("SQL database user's crud");    }  }  class Oracleuserdal:iuserdal  {Public    void crud ()    {      Console.WriteLine ("Crud for Oracle Database User");    }  }

Department:

Interface Idepartmentdal  {    void CRUD ();  }  Class Sqldepartmentdal:  idepartmentdal  {public    void CRUD ()    {      Console.WriteLine (" SQL Database Department (crud ");    }  }  Class Oracledepartmentdal:idepartmentdal  {public    void CRUD ()    {      Console.WriteLine (" Oracle Database Department (crud ");    }  }

"Factory" class

  Interface Ifactory  {    iuserdal createuserdal ();    Idepartmentdal Createdepartmentdal ();  }  Class Sqlfactory:ifactory  {public    iuserdal createuserdal ()    {      return new Sqluserdal ();    Public Idepartmentdal Createdepartmentdal ()    {      return new Sqldepartmentdal ();    }  }  Class Oraclefactory:ifactory  {public    iuserdal createuserdal ()    {      return new Oracleuserdal ();    Public    idepartmentdal Createdepartmentdal ()    {      return new Oracledepartmentdal ();    }  }

Client:

      static void Main (string[] args)      {         //create SQL corresponding Dal         sqlfactory sqlfactory=new sqlfactory ();         Iuserdal Sqluserdal = Sqlfactory.createuserdal ();         Sqluserdal.crud ();         Idepartmentdal Sqldeparmentdal = Sqlfactory.createdepartmentdal ();         Sqldeparmentdal.crud ();                 Replace the database, you need to create the oracle corresponding DAL         oraclefactory oraclefactory = new Oraclefactory ();         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?

Simple Factory mode

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.