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

Source: Internet
Author: User
Mind Mapping

Index:

Ømove Method (removal function) Ømove Field (move range) Øextract Class (Refinement Class) Øinline class (inline class, that is, merge the current class into another class) Øhide Delegate (hidden delegate relationship) Ø Remove middle man (remove middleman) Øintroduce Foreign Method (Introducing an additional function) Øintroduce local Extension (introduction of local extensions) introduction to the above-mentioned PHP "refactoring-Improve the existing code Design to reorganize your functions and continue to talk about refactoring content.
Terminology delegate: Delegate encapsulate: Encapsulation introduce: Introducing wrapper: Overriding the preamble "deciding where to place the responsibility"-using refactoring to change the original design.

Explain:

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

2, class does not assume enough responsibility, there is no reason for the existence of a separate--inline class to integrate it into another class.

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

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

Move method if the methods in one class have a lot of communication with another class, then we Create a new function in another class that has a similar function, turning the old function into a simple delegating Method, or removing the old function.

Class Diagram:

Motivation:

1. If a class has a high degree of coupling with another class, I move the Method. --class is simpler and cleaner to achieve the task of system delivery.

2, to move some of the range, it is necessary to check whether the use of another class must use the number of objects in the number of times more.

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

Extract Class Status: One class does two classes, then a new class is created to move the relevant field and method from the old class to the new class.

Inline class Status: One of your classes does not do too many things (not responsible enough), then move all the attributes of class to another class, then remove the original class. Motivation: The Inline class is the opposite of extract class. --The Extract class example is reversed because PhoneNumber is used only to read code and number. Hide Delegate Status: The client calls the Delegate class method of the server object directly, then establishes the function method required by the client on the server side to hide the delegation relationship. People who have studied object technology know 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 encapsulate. Take a look at the following example:

$person->getdepartment ()->getmanager () clearly revealed that to find XIAOCAI leadership, we had to go through department, so we had to do something to hide department. --can reduce coupling.

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

Introduce Foreign Method condition: There is an extra function in class Previousend that a class client needs to use, but you cannot modify this class Previousend, then you create a function in the client. and takes a previousend entity as a parameter.

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

Using the Subclass method

or use wrapper

Summarize

Note that the "Extract class" and "Inline class", "Hide Delegate" and "Remove Middle man" are the opposite process, in particular, you can read the preface of the flowchart. "Hide Delegate" we used to use a small amount of "Delegate method", and "Remove middle man", used to invoke a lot of "Delegate method" when we can directly use Delegate Class , make a call, and some delegate method we keep as part of the situation. "Extract class" and "inline Class", "Extract class" are often used in classes that take on too much responsibility and become bloated, while the "inline class" is often used when the current class is "too irresponsible". Personally, I would rather "Extract class" than "Inline class".

http://www.bkjia.com/PHPjc/325376.html www.bkjia.com true http://www.bkjia.com/PHPjc/325376.html techarticle Mind Map Index: Move method (removal function) move Field (Shift range) Extract Class (Refinement Class) inline class (inline class, that is, merge the current class into other ... )

  • 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.