Simple factory Method

Source: Internet
Author: User

Simple factory Method
If you encounter problems, you can use computers to analyze and solve problems intuitively. However, such a thinking model can only be used to limit and solve problems,
However, the written program is not necessarily easy to maintain, not easy to expand, and not easy to reuse. In order to make the code easy to write, easy to expand, and easy to reuse, we need to learn the design mode.
1. Tight coupling and loose coupling
Using the object-oriented nature, encapsulation, inheritance, and Polymorphism
2. Single Responsibility Principle: For a class, there should be only one reason for its change
3. Simple factory Method
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.
///


/// Operation class
///
Public class Operation
{
Private double _ numberA = 0;
Private double _ numberB = 0;

///
/// Number
///
Public double NumberA
{
Get
{
Return _ numberA;
}
Set
{
_ NumberA = value;
}
}


///
/// Number B
///
Public double NumberB
{
Get
{
Return _ numberB;
}
Set
{
_ NumberB = value;
}
}


///
/// Obtain the calculation result
///
///
Public virtual double getResult ()
{
Double result = 0;
Return result;
}



}


///
/// Addition class
///
Class OperationAdd: Operation
{
Public override double getResult ()
{
Double result = 0;
Result = NumberA + NumberB;
Return result;
}
}


///
/// Subtraction class
///
Class OperationSub: Operation
{
Public override double getResult ()
{
Double result = 0;
Result = NumberA-NumberB;
Return result;
}
}
///
/// Computing Factory
///
Class OperationFactory
{
Public static Operation createOperate (string operate)
{
Operation operator = null;
Switch (operate)
{
Case "+ ":
{
Operator = new OperationAdd ();
Break;
}
Case "-":
{
Operator = new OperationSub ();
Break;
}

}


Return response;
}
}


Advantages:
The factory class is the key to the entire model. It contains the necessary logic judgment and determines the specific class object to be created based on the information given by the outside world. By using the factory class,
The external world can get rid of the embarrassing situation of directly creating specific product objects. It only needs to be responsible for "consuming" objects. You don't have to worry about how these objects are created and organized.
Clarify their respective responsibilities and rights, which is conducive to the optimization of the entire software architecture.
Disadvantages:
Because the factory class integrates the creation logic of all instances, in violation of the High Cohesion responsibility allocation principle, all creation logic is centralized into a factory class; the classes it can create can only be considered in advance,
To add a new class, you need to change the factory class.
When the number of product categories in the system increases, the requirements for the factory category to create different instances based on different conditions may arise.
This kind of judgment on the condition is intertwined with the judgment on the specific product type, and it is difficult to avoid the spread of module functions, which is very unfavorable for system maintenance and expansion;
These shortcomings have been overcome in the factory method model.
Use Cases:
The factory class is responsible for creating fewer objects;
The customer only knows the parameters passed into the factory class and does not care about how to create objects (logic;
Because simple factories are easy to violate the High Cohesion responsibility allocation principle, they are generally used only in simple cases.

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.