PHP talks about the moving between objects in refactoring-improving the design of existing code. The mind map will introduce how to reorganize your function by following the above PHP discussion "refactoring-improving the design of existing code" and continue to talk about refactoring. Terminology: delegate an enca mind map
Introduction
Reorganize your function by following the above PHP Miscellaneous "refactoring-improving the design of existing code" and continue to talk about refactoring.
Terminology
Delegate: delegate
Encapsulate: Encapsulation
Introduce: Introduction
Preface
"Decide where to place the responsibility" -- use refactoring to change the original design.
Explanation:
1. the Class is too many and bloated-Extract Class separates some of its responsibilities.
2. Class is not responsible-Inline Class integrates it into another Class.
3. use another Class-Hide Delegate to Hide the relationship.
4. undertake (3) frequent changes to the Delegate Class -- Remove Middle Man.
Move Method
If there is a lot of communication between methods in one class and the other class, we will create a new function with similar functions in another class to 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
Condition: 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
Condition: 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: your Class Do not do too many things (do not assume enough responsibility), then Class All features Class And then remove the original Class .
Motivation: Inline Class And Extract Class On the contrary. -- Set Extract Class The example is reversed because PhoneNumber Read only Code And Number .
Hide Delegate
Status: directly called by the customer Server Object Of Delegate Class Of Method In Server Create the functions required by the client Method Used to hide the delegate relationship.
People who have studied object technology know that although Php Allow you Field Declared Public But you should also hide 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
Status: if Class Too much Simple Delegate Then we can directly call Delegate Class .
Motivation: in Hide Delegate In the example, when Department When there are more new methods Hide Delegate , Must be in Person Add the corresponding method to do Delegate . At this time Person Completely changed to Middle Man In this case, we should directly call 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
Status: there is an extra function in the previusend class to be used by the class Client, but you cannot modify the PreviousEnd class, so you can create a function in the Client, and uses a previusend object as the parameter.
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 ".
From Chuan Shan Jia
The article "refactoring-improving the design of existing code" mentioned above in PHP reorganizes your function and continues to talk about refactoring. Terminology: delegate enca...