[Create mode series] concepts and differences between simple factory & factory method simple factory class and factory method class

Source: Internet
Author: User
1. Simple factory pattern simple factory class mode

Simple factory is that all products are produced by the respectice concrete method from simple factory.

The simple factory class means that all its products are produced by the specific methods of the simple factory.

Take the table production process described in abstract as an example. For a description of table production, see http://www.cnblogs.com/Winston/archive/2008/05/13/1194995.html

In simple factory mode, the production desktop (no matter square desktop or circular desktop) and the table leg (no matter thigh or mini leg) are all together.

We have a simple factory class as follows:
Public class simpefactory

{

Public bigleg create_bigleg ()
{
Return new bigleg ();
}

Public minileg create_minileg ()
{
Return new create_minileg ();
}

Public rectang_desktop create_rectangle_desktop
{
Return new rectangle_desktop ();
}

Public circle_desktop create_circle_desktop
{
Return new circle_desktop ();
}

}
Public class bigleg
{}
Public class minileg
{}
Public class rectangle_desktop
{}
Public class circle_desktop
{}

// Note: Of course, products can also inherit from abstract product classes. Implement products in their respective products.

In this case, the client can generate corresponding products according to the command, but there is no association between products. For example
Simpefactory factory = new simplefactory ();
Rectangle_desktop desktop = factory. create_rectangle_desktop ();
Minileg LEG = factory. create_mini_leg ();
That is to say, we may produce square desktops and mini legs, or round desktops and large table legs. Imagine what kind of freak would these two products be together!
This is the simple factory model.

2. factory method pattern factory method mode
factory method defines an interface used to create objects, so that the subclass decides which class to instantiate. This method is called lazy initiation. that is to say, for the specific product to be produced, it is delayed to the corresponding factory to create.
public abstract class creator
{< br> public abstract leg create_bigleg ();
public abstract leg create_minileg ();
public abstract desktop create_rentangle_desktop ();
public abstract desktop create_circle_desktop ();
}< br> public class tablecreator: creator
{< br> Public leg create_bigleg ()
{< br> return New bigleg ();
}< br> ......
}< br> then on the client, we can call
creator factory = new Tablecreator ();
the problem is that there is no association between the created products.

in this case, to create a series of products that are correlated or dependent, we can create a series of interfaces or abstract classes, that is, abstract factories. See the Article http://www.cnblogs.com/Winston/archive/2008/05/13/1194995.html. In this way, we cannot produce products with square desktops and mini table legs. You only need to issue commands to a factory to produce corresponding products. As for how the production process is implemented, it has been transferred to a specific factory by encapsulate (for example, mini table production factory and desk production factory). Obviously, it is impossible for each specific factory to generate a combination of square desktops and mini table legs (otherwise the director will be laid off :)).

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.