PHP Talk about "refactoring-improving the design of the existing code" between the objects of moving characteristics _php tutorial

Source: Internet
Author: User
Mind Mapping







Introduced

Re-organize your functions in the "refactoring-improve existing code Design" section of PHP above, and continue to talk about refactoring.


Professional terminology

Delegate: Delegate
Encapsulate: Package
Introduce: Introducing

Objective

"Deciding where to put the blame"-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 irresponsible--inline class to integrate it into another class.
3. Class uses another Class--hide delegate to hide the relationship.
4, undertake (3) Delegate class often change--remove middle man.

Move Method

If a method in one class has a lot of communication with another class, then we create a new function with similar function in another class, 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: A class does two classes of things, 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 Class Did not do too many things (not assuming sufficient responsibility), then will Class all the features moved to another Class and remove the original Class .
Motive: Inline Class with the Extract Class instead. --Put Extract Class example against the past because PhoneNumber only as Read Code and the Number .

Hide Delegate

Status: Customer calls directly Server Object of the Delegate Class of the Method , then in Server to build the functions required by the client Method to hide the delegate relationship.

people who have studied the technology of the object know that although PHP allows you to Field declared as Public , but 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 Mans

Status: If a Class have done too much of Simple Delegate , then we call directly Delegate Class .
Motive: In Hide Delegate in the example where Department when there are more new ways, we are trying to Hide Delegate , we must be in Person add the appropriate method to do Delegate the use of. At this time Person completely turned into a Middle Mans , we should call directly at this point Delegate Class -- Department .
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

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









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

Excerpt from Chuanshan Armour

http://www.bkjia.com/PHPjc/478312.html www.bkjia.com true http://www.bkjia.com/PHPjc/478312.html techarticle Mind Map Introduction to undertake the above PHP "refactoring-improve the existing code design" to reorganize your function, continue to say refactoring content. Professional terminology delegate: Entrust eNCA ...

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