Simple factory, factory method, abstract factory of design pattern

Source: Internet
Author: User

Learning Source: C # design pattern, Liu Wei

The creation pattern describes how to separate the creation and use of objects, so that users do not have to care about the object's creation details when working with objects, thus reducing the coupling of the system and making the design easier to modify and extend.

Introduction:

Simple factory (Static factory)

1, Mode applicable environment

The factory class is responsible for creating fewer objects, because fewer objects are created and does not cause the business logic in the factory method to be too complex

The client knows only the parameters of the incoming factory class and does not care about how to create the object

2, disadvantages of the simple factory (Static Factory) method:

The factory class centralizes the creation logic of all products, is overloaded with responsibility, and once it does not work, the entire system is affected.

Increase the number of classes in the system (introduce a new factory class), increase the complexity of the system and the difficulty of understanding

System expansion difficult, once adding new products have to modify the factory logic

Factory classes do not scale well due to the use of static factory methods that cause the factory role to fail to form inheritance-based hierarchies

3, advantages of simple factory method

Enables the separation of object creation and use. The creation and use of the ==> class is separated to ensure a single responsibility.

The client does not need to know the class name of the specific product class that is created, only the parameters corresponding to the specific product class can be

By introducing a configuration file, you can change and add new product classes without modifying any client code, to some extent, improve the flexibility of the system.

Factory method Design pattern.

For simple factories, add factory method interfaces.

Further, it can be said that the abstract factory interface changes to abstract classes.

/to change an interface to an abstract classAbstract classLoggerfactory {//calling the business method of the logger class directly in the Factory class Writelog ()     Public voidWritelog () {Logger Logger= This.        Createlogger (); Logger. Writelog (); //the specific factory has been deferred to subclasses.    }         Public AbstractLogger Createlogger (); }usingSystem;usingSystem.Configuration;usingSystem.Reflection;namespacefactorymethodsample{classProgram {Static voidMain (string[] args) {Loggerfactory factory;//programming for abstract factory classesLogger Logger;//programming for abstract product classes//reading configuration Files            stringfactorystring = configurationmanager.appsettings["Factory"]; //Reflection-generated objectsFactory = (loggerfactory) assembly.load ("Factorymethodsample").            CreateInstance (factorystring); Logger=Factory.            Createlogger (); Logger.            Writelog ();        Console.read (); }    }}==>after further, the creation process is omitted.usingSystem;usingSystem.Configuration;usingSystem.Reflection;namespacefactorymethodsample{classProgram {Static voidMain (string[] args) {Loggerfactory factory;//programming for abstract factory classes//reading configuration Files            stringfactorystring = configurationmanager.appsettings["Factory"]; //Reflection-generated objectsFactory = (loggerfactory) assembly.load ("Factorymethodsample").            CreateInstance (factorystring); Factory. Writelog (); //use factory objects directly to invoke business methods for product objectsConsole.read (); }    }}

The third part. Abstract Factory.

Enhancements to the factory approach, such as the original product. Now it's a group of products. For example, different styles of skin, including different style buttons.

    //using the abstraction layer definitionSkinfactory Factory; Button BT; TextField TF; ComboBox CB;//reading configuration FilesstringFactorytype = configurationmanager.appsettings["Factory"];//Reflection-generated objectsFactory = (skinfactory) assembly.load ("Abstractfactorysample"). CreateInstance (Factorytype); BT=Factory. Createbutton (); TF=Factory. Createtextfield (); CB=Factory. Createcombobox (); bt. Display (); TF. Display (); CB. Display (); Console.read ();

2 product lines, 2 specific plants. 2*3 a specific product. 3 Abstract products.

Simple factory, factory method, abstract factory of design pattern

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.