Factory method mode [design mode learning-05]

Source: Internet
Author: User

The factory method mode defines a factory interface for creating a product object and delays the actual creation to the subclass. The core factory category is no longer responsible for product creation. In this way, the core class becomes an abstract factory role and is only responsible for the interfaces that must be implemented by specific factory subclass, the advantage of further abstraction is that the factory method mode enables the system to introduce new products without modifying the specific factory role.

The factory method model is derived from the simple factory model and solves many problems of the simple factory model. First, the 'open-close policy' is fully implemented to achieve scalability. Secondly, a more complex hierarchy can be applied to scenarios where product results are complex.

The factory method mode abstracts the simple factory mode. There is an abstract factory class (which can be an abstract class and an interface). This class is not responsible for specific product production, but only for some specifications. The specific production work is completed by its subclass. In this mode, the factory class and product class can usually correspond in sequence. That is, an abstract factory corresponds to an abstract product, and a specific factory corresponds to a specific product, which is responsible for producing the corresponding product.

The factory method pattern is the most typical template method pattern application.

The UML diagram of the factory method is as follows:

Factory method model roles and structures

Abstract Factory role: it is the core of the factory method model and has nothing to do with applications. Any factory class of the object created in the mode must implement this interface.

Concrete Creator: this is a specific factory class that implements Abstract Factory interfaces. It contains the logic closely related to applications and is called by applications to create product objects. There are two such roles: bulbcreator and tubecreator.

Abstract Product role: the super type of the object created in the factory method mode, that is, the common parent class of the product object or the common interfaces. In, this role is light.


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

Application of factory method mode

Factory methods are often used in the following two cases:

The first case is that the caller knows exactly which factory service should be used for a product, instantiate the specific factory, and produce a specific product. The iterator () method in the Java Collection belongs to this situation.


In the second case, only one product is needed, and you do not need to know which factory is for production, that is, the decision on which specific factory is used is decided by the producer, they instantiate a specific factory based on the current system and return it to the user, which is transparent to the user.

 

In order to compare the previous simple factory mode, the following uses the factory method mode to implement the calculator implemented in the previous factory mode:

The factorymethod. CS code is as follows:

Using system;
Using system. Collections. Generic;
Using system. text;

Namespace factory method_calculator
{

/// <Summary>
/// Operation class
/// </Summary>
Class operation
{
Private double _ numbera = 0;
Private double _ numberb = 0;

Public double numbera
{
Get {return _ numbera ;}
Set {_ numbera = value ;}
}

Public double numberb
{
Get {return _ numberb ;}
Set {_ numberb = value ;}
}

/// <Summary>
/// Obtain the calculation result
/// </Summary>
/// <Returns> </returns>
Public Virtual double getresult ()
{
Double result = 0;
Return result;
}
}

/// <Summary>
/// Addition class
/// </Summary>
Class operationadd: Operation
{
Public override double getresult ()
{
Double result = 0;
Result = numbera + numberb;
Return result;
}
}

/// <Summary>
/// Subtraction class
/// </Summary>
Class operationsub: Operation
{
Public override double getresult ()
{
Double result = 0;
Result = numbera-numberb;
Return result;
}
}
/// <Summary>
/// Multiplication class
/// </Summary>
Class operationmul: Operation
{
Public override double getresult ()
{
Double result = 0;
Result = numbera * numberb;
Return result;
}
}
/// <Summary>
/// Division class
/// </Summary>
Class operationdiv: Operation
{
Public override double getresult ()
{
Double result = 0;
If (numberb = 0)
Throw new exception ("the divisor cannot be 0. ");
Result = numbera/numberb;
Return result;
}
}

/// <Summary>
/// Factory Method
/// </Summary>
Interface ifacloud
{
Operation createoperation ();
}

/// <Summary>
/// The factory responsible for producing "+"
/// </Summary>
Class addfactory: ifacloud
{
Public operation createoperation ()
{
Return new operationadd ();
}
}

/// <Summary>
/// The factory responsible for producing "-"
/// </Summary>
Class subfactory: ifacloud
{
Public operation createoperation ()
{
Return new operationsub ();
}
}

/// <Summary>
/// The factory responsible for producing "*"
/// </Summary>
Class mulfactory: ifacloud
{
Public operation createoperation ()
{
Return new operationmul ();
}
}

/// <Summary>
/// The factory responsible for producing "/"
/// </Summary>
Class divfactory: ifacloud
{
Public operation createoperation ()
{
Return new operationdiv ();
}
}

}

The Code of program. CS is as follows:

Using system;
Using system. Collections. Generic;
Using system. text;

Namespace factory method_calculator
{
Class Program
{
Static void main (string [] ARGs)
{
Ifactory operfactory = new addfactory ();
Operation operation = operfactory. createoperation ();
Counter. numbera = 1;
Counter. numberb = 2;
Double result = response. getresult ();

Console. writeline (result );

Console. Read ();
}
}
}

 

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.