Simple factory mode: simple factory pattern (converted from a. Net)

Source: Internet
Author: User
Simple factory Mode

The simple factory mode returns instances of one of several possible classes based on the data provided to it. Generally, the class it returns has a public parent class and a public method.

The simple factory mode is not actually one of the 23 gof design patterns.

Ii. Simple factory mode roles and structures:

Factory role creator (lightsimplefactory): The factory class creates product objects under the direct control of the client (Create method.

Abstract Product role light: defines the parent class or interfaces of objects created in a simple factory. It can be a class, abstract class, or interface.

Product role concreteproduct (bulblight, tubelight): defines the specific objects processed by the factory.

III,ProgramExample:

 Using System;Public   Abstract   Class Light { Public   Abstract   Void Turnon (); Public   Abstract   Void Turnoff ();} Public   Class Bulblight: Light { Public   Override   Void Turnon () {console. writeline ( "Bulb light is turned on" );} Public   Override   Void Turnoff () {console. writeline ("Bulb light is turned off" );}} Public   Class Tubelight: Light { Public   Override   Void Turnon () {console. writeline ( "Tube light is turned on" );} Public   Override   Void Turnoff () {console. writeline ( "Tube light is turned off" );}} Public   Class Lightsimplefactory { Public Light create ( String Lighttype ){ If (Lighttype = "Bulb" ) Return   New Bulblight (); Else   If (Lighttype = "Tube" ) Return   New Tubelight (); Else              Return   Null ;}} Public   Class Client { Public   Static   Void Main () {lightsimplefactory LSF = New Lightsimplefactory (); light L = LSF. Create ("Bulb" ); L. turnon (); L. turnoff (); console. writeline ( "-----------------" ); L = LSF. Create ( "Tube" ); L. turnon (); L. turnoff ();}}

Iv. Evolution of the simple factory Mode

Evolution of simple factory mode (1)

In addition to the above usage, in some cases, simple factory can be assumed by the abstract Product role. An abstract product class is also a factory of sub-classes.

Program example:

 Using System; Public   Class Light { Public   Virtual   Void Turnon (){}Public   Virtual   Void Turnoff (){} Public   Static Light create ( String Lighttype ){ If (Lighttype = "Bulb" ) Return   New Bulblight (); Else   If (Lighttype = "Tube" ) Return   New Tubelight (); Else              Return   Null ;}}Public   Class Bulblight: Light { Public   Override   Void Turnon () {console. writeline ( "Bulb light is turned on" );} Public   Override   Void Turnoff () {console. writeline ( "Bulb light is turned off" );}} Public   Class Tubelight: Light { Public   Override   Void Turnon () {console. writeline ( "Tube light is turned on" );}Public   Override   Void Turnoff () {console. writeline ( "Tube light is turned off" );}} Public   Class Client { Public   Static   Void Main () {Light L = light. Create ( "Bulb" ); L. turnon (); L. turnoff (); console. writeline ( "-----------------" ); L = light. Create ( "Tube" ); L. turnon (); L. turnoff ();}}

Evolution of simple factory mode (II)

Merge all three roles:

Similar to Singleton, but different.

V. Advantages and Disadvantages:

Advantages:
The factory class contains the necessary judgment logic to determine when to create a product class instance. The client can avoid the responsibility of directly creating product objects, instead of simply "consuming" products. The simple factory model achieves division of responsibility through this approach.

Disadvantages:
When the product has a complex multi-layer hierarchical structure, the factory class only has its own, and should not change, is the disadvantage of the model. Because the factory class integrates the creation logic of all products, the entire system will be affected once it fails to work normally.

At the same time, it is difficult to expand the system. Once a new product is added, the factory logic has to be modified, which may cause the factory logic to be too complex.

In addition, the simple factory mode usually uses the static factory method, which makes it impossible to inherit from sub-classes, and the factory role cannot form a hierarchy based on inheritance.

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.