The power of the Simple factory model

Source: Internet
Author: User

The factory model is dedicated to instantiating a large number of classes with common interfaces. Factory mode can dynamically decide which class to instantiate, without knowing in advance which class to instantiate each time. The factory model has the following forms:

    • Simple Factory Mode
    • Factory approach (Factory method) mode
    • Abstract Factory (Factory) mode

First, simple Factory mode

The Simple factory pattern returns an instance of a class in several possible classes, based on the data supplied to it. Typically, the class it returns has a common parent class and a public method.

Simple Factory mode is not actually a member of the GOF 23 design patterns.

Second, simple factory model role and structure:


Factory class role Creator (Lightsimplefactory): The factory class creates a product object under direct control of the client (create method).
Abstract Product role Product (Light): Defines the parent class of objects created by simple factories or interfaces that they collectively own. Can be a class, abstract class, or interface.
Specific product role Concreteproduct (Bulblight, Tubelight): Defines the object that the factory specifically processes.

Example:

usingSystem; Public Abstract classlight{ Public Abstract voidTurnOn ();  Public Abstract voidturnoff ();} Public classbulblight:light{ Public Override voidTurnOn () {Console.WriteLine ("Bulb Light are turned on"); }    Public Override voidturnoff () {Console.WriteLine ("Bulb Light is turned off"); }} Public classtubelight:light{ Public Override voidTurnOn () {Console.WriteLine ("Tube Light are turned on"); }    Public Override voidturnoff () {Console.WriteLine ("Tube Light is turned off"); }} Public classlightsimplefactory{Public StaticLight Create (stringLighttype) {      if(Lighttype = ="Bulb")         return Newbulblight (); Else if(Lighttype = ="Tube")         return Newtubelight (); Else         return NULL; }} Public classclient{ Public Static voidMain () {lightsimplefactory LSF=Newlightsimplefactory (); Light L= LSF. Create ("Bulb");      L.turnon ();      L.turnoff (); Console.WriteLine ("********************"); L= LSF. Create ("Tube");      L.turnon ();   L.turnoff (); }}
four, simple factory model EvolutionSimple Factory model Evolution (i)

In addition to the above usage, in some cases simple factory can be played by abstract product roles, and an abstract product class is also a sub-class factory.

Example:

usingSystem; Public classlight{ Public Virtual voidTurnOn () {} Public Virtual voidturnoff () {} Public StaticLight Create (stringLighttype) {      if(Lighttype = ="Bulb")         return Newbulblight (); Else if(Lighttype = ="Tube")         return Newtubelight (); Else         return NULL; }} Public classbulblight:light{ Public Override voidTurnOn () {Console.WriteLine ("Bulb Light are turned on"); }    Public Override voidturnoff () {Console.WriteLine ("Bulb Light is turned off"); }} Public classtubelight:light{ Public Override voidTurnOn () {Console.WriteLine ("Tube Light are turned on"); }    Public Override voidturnoff () {Console.WriteLine ("Tube Light is turned off"); }} Public classclient{ Public Static voidMain () {Light L= Light.create ("Bulb");      L.turnon ();      L.turnoff (); Console.WriteLine ("*********"); L= Light.create ("Tube");      L.turnon ();   L.turnoff (); }}

Simple Factory Model Evolution (II.)

All three characters are merged:

Five, advantages and disadvantages:

Advantages:
The factory class contains the necessary judgment logic to decide when to create an instance of the product class, and the client can dispense with the responsibility to create the product object directly, but only "consume" the product. The simple factory model achieves the separation of responsibilities through this approach.

Disadvantages:
When the product has complex multilayer hierarchy structure, the factory class only own, Status quo, is the disadvantage of the pattern. Because the factory class centralizes all product creation logic, the entire system is affected once it does not work properly.

At the same time, the system is difficult to expand, once adding new products will have to modify the factory logic, may cause the factory logic is too complex.

In addition, the simple factory pattern typically uses a static factory approach, which makes it impossible to inherit from subclasses, resulting in a factory role that cannot form an inheritance-based hierarchy.

The power of the Simple factory model

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.