PHP talk "refactoring-improving the design of existing code" _php skills of moving characteristics between two objects

Source: Internet
Author: User
Tags wrapper
Mind Mapping

Index:

Ømove method (Move function)
Ømove Field (move domain)
Øextract Class (abstract class)
Øinline class (in which you are merging the classes to merge the current classes into other classes)
Øhide Delegate (hidden delegate relationship)
Øremove Middle man (remove middleman)
Øintroduce Foreign Method (Introducing an additive function)
Øintroduce local Extension (introducing native extensions)
Introduced
To undertake the previous PHP chat "refactoring-improve the design of existing code" to rearrange your function, continue to say refactoring aspects.

Professional terminology
Delegate: Commissioned
Encapsulate: Encapsulation
Introduce: Introducing
Wrapper: Covering
Objective
"Decide where to put the responsibility"--use refactoring to change the original design.

Explain:

1, class bear too much and bloated unbearable--extract class will part of the responsibility to separate.

2, class does not bear enough responsibility, there is no longer a separate reason--inline class to integrate it into another class.

3. Class uses another Class--hide delegate to hide the relationship.

4, to undertake (3), if the client through the middle man call a lot of delegate class functions (here is just a simple call, only do jump, while middle man did not do too much business logic, such as 10 delegate The method in Class corresponds to the 10 middle man's method)--remove middle Mans, directly using the delegate class, you can use delegate method in part.

Move method
If a method in a class has a lot of interaction with another class, we create a new function in another class that has a similar function, turns the old function into a simple delegating approach, or removes the old function.

Class Diagram:

Motivation:

1. If a class is highly coupled with another class, I move method. --class simpler, more cleanly implementation of system delivery tasks.

2, to move some domain, it is necessary to check whether the number of other classes must use the number of objects in more.

Move Field
Condition: The field in your class is used more by another class. Then create the new field in another class, and modify the old field.

Extract Class
Status: A class does what two classes do, then creates a new class, moves the relevant field and method from the old class to the new class.

Inline Class
Status: One of your classes does not do too much (not assuming sufficient responsibility), move all attributes of class to another class, and then remove the original class.
Motivation: Inline class is the opposite of extract class. --Reverse the Extract class example because the PhoneNumber is used only to read code and number.
Hide Delegate
Status: The client calls the method of the delegate class of server object directly, then creates the function methods required by the client on the server side to hide the delegate relationship.
Anyone who has studied object technology knows that while PHP allows you to declare field as public, you should also hide field (private). As experience grows richer, there are more things to be encapsulated.
Look at one of the following examples:

$person->getdepartment ()->getmanager () clearly revealed that in order to find XIAOCAI leadership, we have to go through department, so what we have to do is hide department. --can reduce coupling.

Remove Middle Mans
Condition: If a class does too much simple Delegate, then we call Delegate class directly.
Motivation: In the example in Hide Delegate when department has more new methods, we must add the appropriate method to the person to do the Delegate for the Hide Delegate. The person at this time has completely become a middle man, and we should call delegate class--department directly.
The point of refactoring is that you never have to say you're sorry, as long as you fix the problem.

Introduce Foreign method
Status: There is an extra function in the class previousend that a class client needs to use, but you cannot modify the class Previousend, then you create a function in the client and take a previousend entity as the argument.

Introduce local extention
Status: Your class needs some extra functions, but you can't modify the current class, so create a new class so that it contains these functions. Use subclass or wrapper. This is commonly used in situations where you cannot modify the source code.
Take the top introduce Foreign Method Example,

Using the Subclass method

or use wrapper

Summarize

Note that "Extract class" and "Inline class", "Hide Delegate" and "Remove Middle man" are the opposite process, specific understanding, you can see the preface of the flow chart.
"Hide Delegate" We often use a small number of "Delegate method", and "Remove middle man", used to invoke a lot of "Delegate method", we can directly use the Delegate Class , make calls, and some delegate method we reserve as part of the situation.
"Extract class" and "Inline class", "Extract class" are often used to take on too much responsibility and become bloated class, and "Inline class" is often used in the current class "too irresponsible" time to use. Personally, I would rather "Extract class" than "Inline class".

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.