Old saying: factory models, siblings

Source: Internet
Author: User
Tags lenovo

The following uses an instance to describe the factory mode.

Case:For a video website, a list page contains a category navigation page. You need to query the corresponding video by type. This is defined as sports and entertainment. Video Retrieval is not simply based on queries similar to classification tables. The query methods may be quite different. In order to make this module scalable, I will apply the factory model here to design and view their advantages.

The factory model isBig nameIt is widely used. It generally includes three methods:

1: simple factory Model: Defines a class to create instances of other classes. The created instance usually has a common parent class. It is also called the static factory method mode and belongs to the class creation mode. The essence of the simple factory mode is that a factory class dynamically determines the product class to be created (these product classes inherit from a parent class or interface) based on the input parameters.

UML class diagram of simple factory Mode

Simple factory ModeInstance:


1: unified interface imanagelist, declaring a method to retrieve video information.

Public interface imanagelist
{
Datatable getinformationlist ();

}

2: sports videos.

Public class sportslist: imanagelist
{
Datatable getinformationlist ()
{// The specific method is omitted ;}
}

3: entertainment videos.

Public class amusementlist: imanagelist
{
Datatable getinformationlist ()
{// The specific method is omitted ;}
}

4: factory type.

Public class simplefactory
{
Public static imanagelist createinstance (string stype)
{
Imanagelist list = NULL;
If (stype = "Sports ")
{List = new sportslist ();}

Return list;
}
}

5: client:

Imanagelist list = simplefactory. createinstance ("Sports ");
Datatable DR = List. getinformationlist ();


2: factory Method: The idea of the factory method mode: delay the instantiation of a class to the subclass. Defines a factory class used to create objects, which decides to instantiate a specific class derived from the same abstract class. This mode mainly includes three roles: factory class, abstract class (Interface), and specific class.

Factory methodClass diagram:

Factory methodInstance:


1: unified interface imanagelist, declaring a method to retrieve video information.

Public interface imanagelist
{
Datatable getinformationlist ();
}


2: factory base class.

Public abstract class managelist
{
Public abstract imanagelist createinstance (string stype );
}

3: specific factory type.

Public class methodfactory: managelist
{
Public override imanagelist createinstance (string stype)
{// Code omitted}
}

4: client:

Methodfactory F = new methodfactory ();
Imanagelist list = f. createinstance ("Sports ");
Datatable DR = List. getinformationlist ();


3: Abstract Factory

The user of the factory method may face two situations: the first case is that for a product, I clearly know which specific factory should be used to serve me, So I instantiate the specific factory, produce specific products. In the second case, I only need a product, and I don't want to know which factory is produced for me, that is, the decision on which specific factory to choose is on the producer side, they instantiate a specific factory based on the current system and return it to the user, which is transparent to the user.

In both cases, the specific implementation is different. In the first case, there is probably a public interface for each product and producer (factory, then, different factories produce different products, and the two have parallel class structures. In the second case, the common parent class of each specific Builder provides an interface in the form of a method, and the judgment logic of the specific factory instance to be returned is implemented in this method, you can call this method to obtain a specific factory instance.

Abstract FactoryClass diagram:



Abstract factories are generally implemented through factory methods (and can be constructed through prototype). If the factory method is compared to an independent small factory, the abstract factory is a big group, many factories are built under it. Although different products are generated, they all have the emblem of the group. For example, although Lenovo's mouse and keyboard are Logitech and the display is made by Philips, Lenovo's logo is applied. We have bought a Lenovo PC, and the whole set has a Lenovo style, which is different from that of Dell. This is what we usually call "Look and Feel. The style you want depends on the group you choose.

Abstract FactoryInstance:If we expand the process of retrieving video information above, we will modularize this part. If we read news instead of video information, the factory method is not suitable. we can define a factory superclass to manage their sub-factory classes and provide unified interfaces, in this way, the system functions can be extended.

Summary:Each of these three methods has its own advantages. From the class diagram above, according to the sequence I introduced, they become more and more complex, and their functions become more and more powerful. Scalability is more powerful than one, and the decoupling capability is also a wave forward. We can apply the factory mode just like using a server bound control (reapeter, datalist, and gridview) in. net. If you do not select the most complex one, you can only select the one that best suits you.

My model is only in the learning stage. If something is wrong, I still forget to give you some advice. :)

Note:This article references the following articles:

Http://hi.baidu.com/daping_zhang/blog/item/bd85d4397a1437f33a87ce0f.html

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.