C # design mode (4)

Source: Internet
Author: User
ArticleDirectory
    • Comment

The factory mode is dedicated to instantiating a large number of classes with common interfaces. The factory mode dynamically determines which class to instantiate without having to know which class to instantiate each time in advance. The factory model has the following forms:

    • Simple factory Mode
    • Factory method
    • Abstract Factory Mode

 

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.

III, Program Example:

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.

References:
Min Hong, Java and mode, e-Industry Press
[Us] James W. Cooper, C # design model, Electronic Industry Press
[Us] Alan shalloway James R. Trott, design patterns explained, China Power Press
[Us] Robert C. Martin, Agile Software Development-principles, models and practices, Tsinghua University Press
[Us] Don box, Chris sells, 1st. Net essence: Public Language Runtime Library, China Power Press

Posted on Lu Zhenyu read (10104) Comments (20) EDIT add to favorites reference network Abstract category: Design Mode

Comment # Re: C # design mode (4)-simple factory pattern caca Can you give an example of "merge all three roles "? Reply to more comments

# Re: C # design mode (4)-simple factory pattern 2004-10-11 Lu Zhenyu @ caca

I think the UML diagrams of all three roles are sufficient to illustrate the problem. For some practical examples, refer to Java and pattern.

The combined effect is like the prototype:

Someobject S = someobject. Clone (); reply to more comments

# Re: C # design mode (4)-simple factory pattern 2004-12-21 aierong
Simple factory actually has poor scalability and does not implement the OCP principle to reply to more comments

# Re: C # design pattern (4)-simple factory pattern bear I also think that the simple factory and OCP are completely opposite to each other. Which one should they use? Reply to more comments

# Re: C # design mode (4)-simple factory pattern Lu Zhenyu simple factory mode is not one of 23 design modes. 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. Reply to more comments

# Re: C # design mode (4)-simple factory pattern Oriental spider is better at looking at the UML diagram. Learn to reply to more comments

# Re: C # design mode (4)-simple factory pattern lSQ I feel that there is no dependency between lightsimplefactory and bulblight and tubelight, and arrows should not be used
The dotted line is connected, and lightsimplefactory should be associated with light. A direct connection should be used.
Ask Mr. Lu to answer questions. Thank you for replying to more comments.

# Re: C # design mode (4)-simple factory pattern Lu Zhenyu @ lSQ

Note the create method of lightsimplefactory:

Public class lightsimplefactory
{
Public Light create (string lighttype)
{
If (lighttype = "bulb ")
Return new bulblight ();
Else if (lighttype = "tube ")
Return new tubelight ();
Else
Return NULL;
}
}

New bulblight (); New tubelight (); if there is no dependency, how can we create these objects? That is to say, lightsimplefactory must know the bulblight and tubelight types, so there is dependency. Reply to more comments

# Re: C # design mode (4)-simple factory pattern mongodan is good. The biggest benefit of simple factory is to centralize changes in one place. In addition, the dependencies in lightsimplefactory. Create () can also be eliminated. My approach is:

1. Declare and construct the sub-public interface ilightcreator
{
Light create ();
}

Public class bulblightcreator: ilightcreator
{
Public Light create ()
{
Return new bulblight ();
}
}
. 2. register the constructor creators. Register ("bulb", new bulblightcreator ());
Creators. Register ("tube", new tubelightcreator ());
.

3. Create public class lightsimplefactory. Create (string lighttype) in simple factory)
{
Ilightcreator creator = creators. Find (lighttype );
Return creator. Create ();
}

The constructor is actually the factory method. In this way, the subaccount is constructed through registration. 3. The original switch () process is eliminated, and the dependency is removed. Among them, register () and find () are easy to understand and the method will not be written.

The new type can be extended through the newly registered constructor. Of course, registration in 2 Code This is just an example. It can be implemented in the configuration file, for example: <lightcreators>
<Add name = "bulb" type = "bulblightcreator,"/>
<Add name = "tube" type = "tubelightcreator,"/>
</Lightcreators>

When I first arrived, please correct me for any mistakes.

Reply to more comments

# Re: C # design mode (4)-simple factory pattern netx reflection is not better? Reply to more comments

# Re: C # design mode (4)-simple factory pattern 2006-04-21 this is an application that tells you about this mode. Of course there are good methods, but I will leave the question here! Reply to more comments

# Re: C # design mode (4)-simple factory pattern my goodness 2 eliminates the client's direct dependency on the product class and is also one of the advantages of a simple factory. Reply to more comments

# Re: C # design model (4)-simple factory pattern game helps you feel the difference between simple factory and factory methods is not great, they solve the same problem, there is no need to divide it into two modes. Let's reply to more comments.

# Re: C # design mode (4)-simple factory pattern hang _ heying sets a [attribute] for each subclass
Compare attributes and names with reflection in C # To eliminate the dependency between factory methods and subclass.
In addition, you can go to the MS lecture design mode video to reply to more comments.

# Re: C # design mode (4)-simple factory pattern 2006-08-16 20:01 msjqd simple engineering mode easy to operate, mainly to build a factory class (although this factory class may be an abstract product class) in this way, you can manage classes with public interfaces in a centralized manner. the advantage is centralized management. the disadvantage is that when the business logic is strong, it is a little difficult to implement the simple factory Patten.

Reply to more comments

# Re: C # design mode (4)-simple factory pattern 2006-09-08 20:10 finally understand a bit
Reply to more comments

# Re: C # design mode (4)-simple factory pattern Heqing is too late. What I cannot learn at school is supported! Reply to more comments

# C # design mode (4)-simple factory pattern [trackback] Sean (Chen Yang) C # design mode (4) -The simplefactorypattern factory mode is used to instantiate a large number of classes with common interfaces. The factory mode dynamically determines which class to instantiate without having to know which class to instantiate each time in advance. The factory mode is... view the original article to reply to more comments.

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.