Required refactoring skills (4): extract factory class

Source: Internet
Author: User

 

Extract factory class: Use a simple factory class to create an object instance. For example: for a client event, we may need to initialize an object instance and call several methods to perform a series of operations. If client events often need to be extended, the object instances may be different each time they are initialized. In order to encapsulate the actions of this initialization object and make this behavior easier to maintain, we need to hand over the initialization object action to the simple factory class for unified completion. Project Example: A small shopping mall. The requirements are described as follows: the administrator can add, delete, and modify the discount ratio and discount type of the current product on the background. At the beginning, we thought it was very simple. We thought that the user only had a discount. OK, add a drop-down list and put a 1-9 discount on it. Then, the price of X is enough. After unexpected design, it was rejected at that time. The customer said that we will not only get a discount, but may also get a rebate. The customer's requirements have always been changeable, and the requirement cannot be changed. It is only necessary to change the design. Let's take a look at the design in the current project. Code Forgot, most of the code below is derived from the big bull's Article Because the situation is almost the same, it will be helpful to explain the truth.
Verflow-X: hidden; overflow-Y: hidden; "id =" _ mcepaste "> Note: simple factories are suitable for some simple logics, it is not suitable for some complicated logic applications. The restructured code is directly posted below:

Extraction factory class:Create an object instance using a simple factory class.

For example:For a client event, we may need to initialize an object instance and call several methods to perform a series of operations. If client events often need to be extended, the object instances may be different each time they are initialized. In order to encapsulate the actions of this initialization object and make this behavior easier to maintain, we need to hand over the initialization object action to the simple factory class for unified completion.

Project instance:I have worked as a small shopping mall. The requirements are described as follows: the administrator can add, delete, and modify the discount ratio and discount type of the current product on the background. At the beginning, we thought it was very simple. We thought that the user only had a discount. OK, add a drop-down list and put a 1-9 discount on it. Then, the price of X is enough. After unexpected design, it was rejected at that time. The customer said that we will not only get a discount, but may also get a rebate. The customer's requirements have always been changeable, and the requirement cannot be changed. It is only necessary to change the design. Let's take a look at the design in the current project. The specific code is forgotten. Most of the code below is introduced from the article published by Daniel in the blog Park. because the situation is similar, it is easy to explain it.

Note:Simple factory is applicable to some simple logic, andNot suitable for complicated logic.
The restructured code is directly posted below:

 

Related abstract classes

  Abstract    Class  Supercash
{
Public Abstract Double 0; "> returntotalcash ( Double Money );
}
Class Normalcash: supercash
{
Public Override Double Returntotalcash ( Double Money)
{
Return Money;
}
}
Class Rebatecash: supercash
{
Private Double Moneyrebate = 1D;
Public Rebatecash ( Double _ Mrebate)
{
Moneyrebate = _ Mrebate;
}

Public Override Double Returntotalcash ( Double Money)
{
Return Money * Moneyrebate;
}
}
Class Returncash: supercash
{
Private Double Moneycondition = 0.0d ;
Private Double Moneyreturn = 0.0d ;
Public Returncash ( Double _ Mcondition, Double _ Mreturn)
{
Moneycondition = _ Mcondition;
Moneyreturn = _ Mreturn;
}

Public Override Double Returntotalcash ( Double Money)
{
If (Money > Moneycondition)
{
Return Money - Moneyreturn;
}
Return Money;
}
}

 

Cash consumption Factory

  Class  Cashfactory
{
Public Static Supercash createsupercash ( String Type)
{
Supercash = Null ;
Switch (Type)
{
Case " Normal " :
Supercash = New Normalcash ();
Break ;
Case " 80% off " :
Supercash = New Rebatecash ( 0.8 );
Break ;
Case " 50% off " :
Supercash = New Rebatecash ( 0.5 );
Break ;
Case " 100 per 300 " :
Supercash = New Returncash ( 300 , 100 );
Break ;
}
Return Supercash;
}
}

 

Called event

  Protected     Void Btntotal_click (  Object  Sender, eventargs E)
{
String Type = Ddltype. selecteditem. value;
Supercash = Cashfactory. createsupercash (type );
Double Total = Supercash. returntotalcash (convert. todouble (txtprice. Text) * Convert. todouble (txtnum. Text ));
Lbtotal. Text = Total. tostring ();
}

 

Take a look at the above Code and think about it. We found that to increase a discount, we still need to modify the background code. The modified part is as follows: (1) We need to go to the dropdownlist on the customer's front-end. Add new discount plan . (2) required Modify the cashfactory class . (3) need to increase Add a corresponding discount method for implementing the interface . How can we save these three steps? How can we achieve this through configuration alone? The corresponding modification scheme is as follows: (1) Use XML instead of hard Encoding , Dropdownlist to read data bound to XML. (2) In the factory class, Use reflection Obtain, initialize, and return instance objects. (3) If the possible types of commodity prices are determined, for example, there are only three situations: A: normal price; B: discount; C: How much is the purchased rebate, then, we can modify the user's needs only by modifying the configuration file. The following code is not written. If you are interested, try it.

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.