Third day of design model learning: 2.1 simple factory model

Source: Internet
Author: User

1. 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.

 

3. Program example:
Using System;

 

Namespace SimpleFactory

{

Public abstract class Light

    {

Public abstract void TurnOn ();

Public abstract void TurnOff ();

    }

 

Public class BulbLight: Light

    {

Public override void TurnOn ()

        {

Console. WriteLine ("BulbLight is TurnOn .");

        }

 

Public override void TurnOff ()

        {

Console. WriteLine ("BulbLight is TurnOff .");

        }

    }

 

Public class TubeLight: Light

    {

Public override void TurnOn ()

        {

Console. WriteLine ("TubeLight is TurnOn .");

        }

 

Public override void TurnOff ()

        {

Console. WriteLine ("TubeLight is TurnOff .");

        }

    }

 

Public class LightSimpleFactory

    {

Public Light Create (string lightType)

        {

If (lightType = "BulbLight ")

Return new BulbLight ();

Else if (lightType = "TubeLight ")

Return new TubeLight ();

Else

Return null;

        }

    }

 

Public class Client

    {

Public static void Main (string [] args)

        {

LightSimpleFactory lsf = new LightSimpleFactory ();

Light l = lsf. Create ("BulbLight ");

L. TurnOn ();

L. TurnOff ();

 

Console. WriteLine ("------------------------------------------------------");

L = lsf. Create ("TubeLight ");

L. TurnOn ();

L. TurnOff ();

 

Console. ReadLine ();

        }

    }

}

 

 

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;

 

Namespace SimpleFactory

{

Public class Light

    {

Public virtual void TurnOn ()

        {

        }

 

Public virtual void TurnOff ()

        {

        }

 

Public static Light Create (string lightType)

        {

If (lightType = "BulbLight ")

Return new BulbLight ();

Else if (lightType = "TubeLight ")

Return new TubeLight ();

Else

Return null;

        }

    }

 

Public class BulbLight: Light

    {

Public override void TurnOn ()

        {

Console. WriteLine ("BulbLight is TurnOn .");

        }

 

Public override void TurnOff ()

        {

Console. WriteLine ("BulbLight is TurnOff .");

        }

    }

 

Public class TubeLight: Light

    {

Public override void TurnOn ()

        {

Console. WriteLine ("TubeLight is TurnOn .");

        }

 

Public override void TurnOff ()

        {

Console. WriteLine ("TubeLight is TurnOff .");

        }

    }

 

Public class Client

    {

Public static void Main (string [] args)

        {

Light l = Light. Create ("BulbLight ");

L. TurnOn ();

L. TurnOff ();

 

Console. WriteLine ("------------------------------------------------------");

L = Light. Create ("TubeLight ");

L. TurnOn ();

L. TurnOff ();

 

Console. ReadLine ();

        }

    }

}

 

 

Merge all three roles:

 

 

Similar to Singleton, but different. The following is the reference code:

Using System;

 

Namespace SimpleFactory

{

Public class Light

    {

Private string lightType;

Private Light (string lightType)

        {

This. lightType = lightType;

        }

 

Public void TurnOn ()

        {

If (this. lightType = "BulbLight ")

Console. WriteLine ("BulbLight is TurnOn .");

Else if (this. lightType = "TubeLight ")

Console. WriteLine ("TubeLight is TurnOn .");

        }

 

Public void TurnOff ()

        {

If (this. lightType = "BulbLight ")

Console. WriteLine ("BulbLight is TurnOff .");

Else if (this. lightType = "TubeLight ")

Console. WriteLine ("TubeLight is TurnOff .");

        }

 

Public static Light Create (string lightType)

        {

Light light = new Light (lightType );

Return light;

        }

    }

 

Public class Client

    {

Public static void Main (string [] args)

        {

Light bulbLight = Light. Create ("BulbLight ");

BulbLight. TurnOn ();

BulbLight. TurnOff ();

 

Console. WriteLine ("------------------------------------------------------");

 

Light tubeLight = Light. Create ("TubeLight ");

TubeLight. TurnOn ();

TubeLight. TurnOff ();

 

Console. ReadLine ();

        }

}

}

 

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.
The simple factory model is not one of the 23 design models. So it has various problems. But it is an entry to learn other factory models. OCP is more critical. You can use the simple factory mode in a few complex environments.

Third day of design model learning: 2.1 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.