C # factory mode and abstract factory mode [reprinted]

Source: Internet
Author: User

1. factory method ( factory method ) mode

Factory method (Factorymethod) Mode is the class creation mode. It is used to define a factory interface for creating product objects and delay the actual creation to the subclass.

The factory method mode is further abstract and promoted by the simple factory mode. Due to the use of polymorphism, the factory method pattern maintains the advantages of the simple factory pattern and overcomes its shortcomings.

in factory method mode, the core factory category is no longer responsible for the creation of all products, but the specific creation work is handed over to the subclass. This core class is only responsible for providing the interface that must be implemented by a specific factory, without touching the details of which product class is instantiated. This allows the factory method mode to allow the system to introduce new products without modifying the factory role.

InFactory methodIn the mode, the factory class and product class often have a parallel hierarchical structure, and they correspond one to one.

II. factory method mode roles and structures:

Abstract Factory ( creator ) role: is the core of the factory method mode, it is irrelevant to the application Program . Any factory class of the object created in the mode must implement this interface.

Factory ( concrete creator ) role: This is a specific factory class that implements Abstract Factory interfaces, contains the logic closely related to the application and is called by the application to create product objects.

abstract product ( product ) role: the super type of the object created in factory method mode, that is, the product object's common parent class or interfaces.

Specific product (Concrete Product) Role:This role implements the interface defined by the abstract Product role. A specific product is created in a specific factory, which is usually one-to-one.

 

Factory mode and abstract factory mode are constructor modes that are widely used. First, let's briefly introduce the application background of this constructor mode: In the process of developing a class, we usually need to develop corresponding constructors for this class (but in most cases ,. NET development environment will automatically create constructor by default), so that customers using this class can use constructor to instantiate this class. however, sometimes this happens: customers who need to use an object do not know (or should not know) which class of the several classes should be initialized. to solve this problem, we can use the factory mode to define an interface. Customers can use this interface to create an object. at the same time, we can also control which class to instantiate. A method is used in the instantiation process. This method needs to use external factors to determine which class to instantiate. sometimes, these external factors are a huge research topic and often involve multiple classes. for this reason, the abstract factory model is applied to this scenario, and its purpose is Provides an interface for creating a series of related or mutually dependent objects without specifying their specific classes. " , At least " You do not need to specify their specific classes " Meet our requirements. The following describes a UML class digoal in gof's design pattern framework2.0 and an easy-to-understand example: this figure is visually intuitive, we can clearly see the relationships between Abstract factories, factories, and customers. let's take a look at the example below: If my program needs a series of objects, such as bed, desk, Chair ..., To use them, we must generate them in the program based on user requirements and call the new operator one by one, so that the client program will know the information of the corresponding class and generate Code Obviously not flexible enough. They are obviously a type of furniture. In this case, we only need a factory that produces furniture. instead of using specific classes in the code, we can simply describe what we need and then get the desired object. first define a class, mainly declare a furniture interface, bed and chair class: Public Interface Ifurniture
{
}
Public   Class Bed: ifurniture
{
Public Bed ()
{
Console. writeline ( "I need a bed ! " );
}
}
Public   Class Desk: ifurniture
{
Public Desk ()
{
Console. writeline ( "I need a desk ! " );
}
} Public ClassChair: ifurniture
{
PublicChair ()
{
Console. writeline ("I need a chair!");
}
} Define a furniture factory class (you can use the type class in the reflection mechanism to obtain the type information of the class name specified by name, and then use system. activator to create an object based on this information ): Public   Class Furniturefactory
{
Public Ifurniture makefurniture ( String Name)
{
Ifurniture myfurniture =   Null ;
Try
{
Type type = Type. GetType (name, True );
Myfurniture = (Ifurniture) activator. createinstance (type );
}
Catch (Typeloadexception E)
Console. writeline ( " I dont know this kind of furniture,
Exception caught - { 0 } " , E. Message );
Return Myfurniture;
}
} Then the program is called on the client: StringFurniturename=Console. Readline ();
Ifurniture myfurniture;
Furniturefactory myfurniturefactory= NewFurniturefactory ();
Myfurniture=Myfurniturefactory. makefurniture (furniturename ); In this way, the expected implementation is achieved through this. of course, this example is relatively simple, but it achieves code flexibility through the idea of the factory model. in application software system development, there are many places to consider using the factory model. for example, when writing data-Layer Code, taking into account the portability and scalability of the program, it is not an ideal implementation method to adopt the factory mode for different databases.


 

--ArticleInformation from the Internet

 

Related Article

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.