"Refactoring – Improving the design of existing code" reading notes----introduce Foreign Method

Source: Internet
Author: User
Tags billing cycle

When you cannot get the source code of a class or have no permission to modify the class, you may have to ask for something else when you need it, such as a date class, you need to be able to let him return directly to the object of his day, but he does not, At this point you need to temporarily use the introduce Foreign method to make the transition, and at the appropriate time inform the author to move the Foreign method into his own class.

That's a lot of things, especially when you're in a group, when someone else has written a class, you're going to use it to do what you want to do, but if you find that the class doesn't provide the functionality you want, if you can change the source code, of course you can. You can add a line of functionality to the source code of your class directly. But if you can't get the source code, you can just code the client and add the function you want.

Of course, if you're in a customer class that only uses this feature once, it's no big deal, and you can add extra processing logic altogether. You may not even need the object that originally provided the service itself, but if you need to use the function multiple times, you have to repeat the code, remember?

Repeating code is the root of the software.

These duplicate codes should be pumped out using extract method to put into the same function, of course, if you have to perform this refactoring, this also gives you a clear signal: This function should be implemented in the class that provides the service.

If you find that you have added a large number of functions to a service class, or if you find that there are many classes that require the same additional functions, you should not use this refactoring, but should use the introduce Local Extension. But please do not forget that Foreign method is always expedient, and if possible, you should still move these functions through the move method to their ideal home. If you are unable to do so, you can also give this additional function to the owner of the service class, and ask him to help you implement this function in the service class.

Practice:

    • Create a function in the customer class to provide the functionality you need. This function should not invoke any of the attributes of the client class, if he needs a value, pass the value as a transmission to him, rather than let him invoke the attributes of your client class within the function itself. Here's a tip, in order to avoid the feature of the client class that might have been referred to by a function that would allow you to refine, we can give him a static feature in the newly created function , and everyone knows that C + + 's static forces you to not touch the attributes of the owning class.
    • Take a service class instance as the first parameter of this function.
    • annotating the function as "foreign method should be implemented in the service class" reminds you of the possibility to find these functions very quickly when you have the opportunity to move the additional functions into the service class in the future.

Example:

New Date (Perviousend.getyear (), Perviousend.getmonth (), Perviousend.getdate (        )1);

As we can see in the example, I need a function that spans a billing cycle in the client code, but the actual date does not provide this function, so I can only write a lengthy new. Of course, if there is a place like this we don't need to be too concerned, but if there is more, we need to use the Extract method for refactoring, this function is the date class of the external function. We can make this statement.

Date Newstart = nextday (perviousend); Static Date nextday (date arg) {    //  foreign method, should is on Date    returnNew  Date (Arg.getyear (), Arg.getmonth (), arg.getdate ()        1);}

As you can see, we make this function static so that we do not use any of the properties of the client class, and we pass the first parameter of the function as a service class object, so that we can get a simple and non-parametric interface when we make the Move method.

"Refactoring – Improving the design of existing code" reading notes----introduce Foreign Method

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.