"Refactoring – Improving the design of existing code" reading notes----Remove middle man

Source: Internet
Author: User

If you find that a class has done too many simple delegate actions, you can consider whether you can allow the client to invoke the Trustee class directly. In hide delegate, we describe the benefits of encapsulating a trustee, but the benefits are also at the cost of adding the appropriate function to the delegate's service object each time you need to add a new function to the trustee. As the features of the trustee become more and more, you will find that the things you are dealing with become more and more complex, and you will feel that the service object is completely an intermediary (middle man), and you should consider whether you can allow the client to call the trustee directly.

You don't have to think about what level of encapsulation is reasonable, refactoring brings you the possibility of hide delegate and remove middle man, you don't have to think about it, because you can't think about it very thoughtful, the requirements are constantly changing, as the system design updates, your " The appropriate degree of concealment "will also occur corresponding changes, yesterday the appropriate package may become bloated today, the significance of the reconstruction is: you never have to say sorry, just fix the problem in the right place ."

Practice:

    • A new function is established on the service object to obtain the trustee function.
    • For each delegate function, delete the function in the service class and let the client calling the function call the trustee instead.
    • After each delegate function is processed, it is compiled and tested.

Example:

classperson{ Public: person*manager ()Const        {            returnM_department->Manager (); }    Private: Department*m_department;};classdepartment{ Public: Department ( person*Person ): M_manager (person) {} person*manager ()Const        {            returnM_manager; }    Private: person*M_manager;};

Here we take the example of hide delegate, we can see that person is Department service object, department is the trustee class of person, when the client code

Manager = John.manager ();

To be acquired, they are acquired indirectly through the person. But if a lot of functions require you to do this, you have to install a large number of simple delegate functions in person to handle this problem. This is the time to remove the middleman, first set up a function in person to obtain the Trustee object.

class person{    public:        const        {            return  m_ department;        }};

Then one by one, each delegate function, for such a function, we have to find the function through the person, and modify them, let him first get the Trustee object and then directly use the latter.

Manager = Johon.department ()->manager ();

Then I can delete the delegate function in the service object, and if I miss something, the compiler will tell me. For the sake of convenience, there may be circumstances to retain some of the delegation relationship, but also may need to hide the client relationship, let other customers directly use the trustee, for these reasons, you can also properly retain some simple delegate functions and put them in situ.

"Refactoring – Improving the design of existing code" reading notes----Remove middle man

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.