Refactoring notes -- Introduce an external function to reconstruct an external Function

Source: Internet
Author: User
Tags billing cycle

Refactoring notes -- Introduce an external function to reconstruct an external Function

This article is in the study of the summary, welcome to reprint but please note the Source: http://blog.csdn.net/pistolove/article/details/44313725



In the previous article, we introduced "Remove man-in-the-middle ". This article introduces the refactoring method of "introducing external functions.

Let's learn about This refactoring method.



Open door

Conclusion: You need to add a function to the class that provides services, but you cannot modify this class.

Solution: Create a function in the customer class and input a server class instance as the first parameter.

//beforeDate newStart = new Date(previous.getYear(),previous.getYear(),previous.getYear() + 1);
//afterDate newStart = nextDay(previous);private static Date nextDay(Date date){//......return new Date(date.getYear(),date.getMonth(),date.getDate() + 1)}



Motivation

You often encounter this: you are using a class, which is very good and provides you with a lot of services you need. Then, you need to add a new service, but this class cannot be supplied. So you began to curse: Why can't you do this? If you can modify the source code, you can add a new function. If not, you have to encode the function on the client to supplement the function you want.

If you only need to use this function once, the extra encoding is no big deal, and may not even need the class that originally provided the service. However, if you need to use this function multiple times, you have to repeat the code. Remember: repeated code is the source of all evil software. These repeated codes should be extracted and put into the same function. When performing This refactoring, if an external function is implemented, it is a clear signal that this function should be implemented in the class that provides services.

If a large number of external functions are created for a service class, or many classes require the same external functions, you should not use this item for reconstruction, but use "introduce local extension ". But don't forget: the addition of functions is always a matter of expediency. If possible, move these functions to their ideal home. Because of the ownership of the code, you cannot perform this operation, so you can hand over the added function to the service class and ask it to help implement this function in the service class.



Practice
(1) create a function in the customer class to provide the functions you need. (This function should not call any features of the customer class. If it requires a value, pass it as a parameter.) (2) use a service class instance as the first parameter of the function. (3) comment out the function as "plus function. It should be implemented in the service class "(so that these functions can be easily identified once there are subsequent changes)

Example
In the program, you need to cross a billing cycle. The original code is as follows:
Date newStart = new Date(previous.getYear(),previous.getYear(),previous.getYear() + 1);
The Code on the right of the value assignment operation can be extracted into an independent function. This function is an additional function of the Date class:
Date newStart = nextDay(previous);private static Date nextDay(Date date){//......return new Date(date.getYear(),date.getMonth(),date.getDate() + 1)}


This article mainly introduces the refactoring method-introducing an external function. This method is simple and easy to understand. It is not cumbersome here. Finally, I hope this article will help you. If you have any questions, please leave a message. Thank you. (PS: next article will introduce refactoring notes-introduce local extensions)


Rebuild note articles

Rebuild notes-getting started

Rebuilding notes-bad taste of code (I)

Rebuilding notes-bad taste of Code (Part 2)

Rebuilding notes -- Building a test body

Rebuilding notes -- refining Functions

Rebuilding notes-inline functions

Refactoring notes-inline temporary variables

Rebuilding notes -- replacing temporary variables with queries

Refactoring notes -- Introducing explanatory variables

Rebuilding notes -- breaking down temporary variables

Refactoring notes -- remove the value assignment to the parameter refactoring notes -- replace the function refactoring notes with function objects -- replace the algorithm refactoring notes -- remove the function
Rebuilding notes -- moving fields and restructuring notes -- Refining
Rebuilding notes-associating classes
Rebuilding notes-hiding "delegated relationships"
Rebuild notes -- remove man-in-the-middle
Rebuilding notes -- introducing additional functions


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.