Reconstruction Method-Simplified function call [3], reconstruction method-Simplified Function

Source: Internet
Author: User
Tags class manager

Reconstruction Method-Simplified function call [3], reconstruction method-Simplified Function

Total returned directory

Directory of this section

  • Replace Parameter with Explicit Methods (Replace parameters with Explicit functions)
  • Preserve Whole Object (keep Object complete)
6 Replace Parameter with Explicit Methods (Replace the Parameter with a definite function) Summary

You have a function that takes different actions depending on the parameter value.Creates an independent function for each possible value of this parameter..

Motivation

If a parameter has multiple possible values, and these parameter values are checked by expressions in the function, and different behavior is performed according to different parameter values, this item should be used for reconstruction;

Benefits: "code check during compilation" and "clearer interface" (if the parameter value is used to determine the function behavior, the function user not only needs to observe the function, it also determines whether the parameter is "legal ". -- Valid parameters are rarely mentioned in the document and can be determined only by context );

Considering the benefits of "compile-time verification", we also deserve to do so to get a clear interface.

Example

The following code creates different subclasses under the Employee according to different parameter values.

class EmployeeType{    static Employee Create(int type)    {        switch (type)        {            case 0:                return new Engineer();            case 1:                return new Salesman();            case 2:                return new Manager();            default:                throw new ArgumentException("Incorrect type code value!");        }    }}class Employee{}class Engineer:Employee{    }class Salesman:Employee{   }class Manager:Employee{    }

Because this is a factory function, Replace Conditional with Polymorphism cannot be implemented, because the object is not created yet when this function is used. As it is foreseeable that there will not be too many new child classes in the Employee, you can create a factory function for each child class with confidence without worrying about the increase in the number of factory functions. First, create a new function based on the parameter values:

static Employee CreateEngineer(){    return new Engineer();}static Employee CreateSalesman(){    return new Salesman();}static Employee CreateManager(){    return new Manager();}

Find the function caller and run the following code:

Employee kent = EmployeeType.Create(0);

Replace:

Employee kent = EmployeeType.CreateEngineer();

After replacement, you can delete the Create () function.

Summary

If the function makes different responses based on parameters, it is time to refactor.

7 Preserve Whole Object (keep Object complete) Overview

You extract several values from an object and use them as parameters in a callback function call,Change to pass the entire object.

Motivation

We often pass several data items from the same object as parameters to a function. In this case, if the called function requires a new data item, all calls to the function must be searched and modified. However, this problem does not occur if the entire object is passed.

Advantages of this method: (1) making parameter columns more stable; (2) improving code readability.

Example
class Room{    public bool WithinPlan(HeatingPlan plan)    {        int low = DaysTempRange().GetLow();        int high = DaysTempRange().GetHigh();        return plan.WithinRange(low, high);    }    public TempRange DaysTempRange()    {        return new TempRange();    }}class HeatingPlan{    private TempRange _range;    public bool WithinRange(int low, int high)    {        return low >= _range.GetLow() && high <= _range.GetHigh();    }}class TempRange{    public int GetLow()    {        return 6;    }    public int GetHigh()    {        return 28;    }}

In fact, the information of the TempRange object cannot be separated and transmitted separately. You only need to pass the entire object to the WithinPlan () function:

class Room{    public bool WithinPlan(HeatingPlan plan)    {        return plan.WithinRange(DaysTempRange());    }    public TempRange DaysTempRange()    {        return new TempRange();    }}class HeatingPlan{    private TempRange _range;    public bool WithinRange(TempRange roomRange)    {        return roomRange.GetLow() >= _range.GetLow() && roomRange.GetHigh() <= _range.GetHigh();    }}class TempRange{    public int GetLow()    {        return 6;    }    public int GetHigh()    {        return 28;    }}
Summary

Although it is good to pass the entire object, things always have two sides. If the value is passed, the called function only depends on the value and does not depend on the object to which it belongs. However, if the entire object is passed, the object where the called function is located must depend on the parameter object. If this deteriorates the dependency structure, this item should not be used for reconstruction.

 

To Be Continued ......

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.