PHP Miscellaneous refactoring-improving the design of existing code-2 moving between objects features-PHP Tutorial

Source: Internet
Author: User
PHP Miscellaneous refactoring-improving the design of existing code-2 moving between objects. Mind Map Index: MoveMethod (shift function) MoveField (shift value field) ExtractClass (Extract class) InlineClass (class inassociation, is to merge the current class into other mind map

Index:

Ø Move Method (shift function) Ø Move Field (shift value Field) Ø Extract Class (Extract Class) Ø Inline Class (concatenate Class, is to merge the current class to other classes.) Ø Hide Delegate (hiding the Delegate relationship) Ø Remove Middle Man (removing the Man in the Middle) Ø Introduce Foreign Method (introducing the external function) Ø Introduce Local Extension (introducing Local extensions) introduces how to reorganize your function by following the above PHP Miscellaneous "refactoring-improving the design of existing code" and continue to talk about refactoring.
Professional term delegate: delegate encapsulate: encapsulate introduce: introduce wrapper: overwrite preface "determines where the responsibility is" -- use refactoring to change the original design.

Explanation:

1. the Class is too many and bloated-Extract Class separates some of its responsibilities.

2. the Class does not assume enough responsibilities and there is no longer a reason for its own existence-Inline Class integrates it into another Class.

3. use another Class-Hide Delegate to Hide the relationship.

4. undertake (3). If the Client calls many Delegate Class functions through Middle Man (here, it is simple to call and only performs redirection, while Middle Man does not do much business logic, for example, the methods in the 10 Delegate classes correspond to 10 Middle Man methods. -- Remove Middle Man and use the Delegate Class directly. you can use the Delegate Method partially.

Move Method if the methods in a class have a lot of communication with the other class, then we will Create a new function with similar functions in another class, convert the old function into a simple Delegating Method, or remove the old function.

Class diagram:

Motivation:

1. if a class is highly coupled with another class, I will Move the Method. -- The class is simpler and easier to implement system delivery tasks.

2. to move some value fields, you must check whether another class is used more times than the resident object.

Move Field: the field in your class is used more by another class. Create a new field in another class and modify the old field.

Extract Class: If a Class does two tasks, create a new Class and move the related Field and Method from the old Class to the new Class.

Inline Class status: if one of your classes does not do much (not take enough responsibility), move all the features of the Class to another Class and then remove the original Class. Motivation: Inline Class is opposite to Extract Class. -- Reverse the Extract Class example because PhoneNumber is only used to read code and number. Hide Delegate status: the customer directly calls the Delegate Class Method of the Server Object. then, the user needs to establish the function Method on the Server side to Hide the Delegate relationship. People who have learned object technology know that although php allows you to declare a field as public, you should also hide the field (private ). As experience grows, there are more things worth encapsulating. Let's look at the following example:

$ Person-> getDepartment ()-> getManager () clearly revealed that to find the leader of Xiaocai, we must go through the department, so what we need to do is to hide the department. -- Reduces coupling.

Remove Middle Man: If a Class has done too much Simple Delegate, we will directly call the Delegate Class. Motivation: In the example of Hide Delegate, when the Department has more new methods, we must add corresponding methods to the Person for the Hide Delegate. At this time, the Person is completely changed to a Middle Man. at this time, we should directly call the Delegate Class -- Department. The significance of refactoring is that you never have to say sorry, as long as you fix the problem.

Introduce Foreign Method: there is an extra function in the previusend class that the class Client needs to use, but you cannot modify the previusend class, so you can create a function in the Client, and uses a previusend object as the parameter.

Introduce Local Extention: your Class requires some additional functions, but you cannot modify the current Class, so create a new Class to include these functions. Use Subclass or Wrapper. -- This is generally used when you cannot modify the source code. Take the Introduce Foreign Method example above as an example.

Use the Subclass method

Or use Wrapper

Summary

Note that "Extract Class", "Inline Class", "Hide Delegate", and "Remove Middle Man" are both opposite processes, you can see the flowchart in the preface. "Hide Delegate" is usually used when a small number of "Delegate methods" are used, while "Remove Middle Man" is used to call many "Delegate methods, we can directly use the Delegate Class for calling, while some of the Delegate methods are reserved as appropriate. "Extract Class" and "Inline Class", and "Extract Class" are often used to take on the Class that has become bloated due to excessive responsibilities, "Inline Class" is often used when the current Class is too irresponsible. -- I personally would rather "Extract Class" than "Inline Class ".

Merge index: Move Method (moving function) Move Field (moving Value Field) Extract Class (refining Class) Inline Class (concatenating Class, is to merge current Class to 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.