The feature of moving objects between reconstruction methods [3 ].

Source: Internet
Author: User

The feature of moving objects between reconstruction methods [3 ].

Total returned directory

Directory of this section

  • Hide Delegate (Hide "delegation relationship ")

  • Remove Middle Man (Remove Man in the Middle)

5 Hide Delegate (Hide "delegation relationship") Overview

The customer calls another object through a delegate relationship.

Create all functions required by the customer in the service class to hide the delegate relationship.

Motivation

If a customer first obtains another object through the field or attribute of the service object and then calls the latter's function, the customer must be aware of this delegation relationship. In case of a change in the delegated relationship, the customer must also change accordingly. You can place a simple delegate relationship on the service object to hide the delegate relationship and remove this dependency. In this way, even if the delegated relationship changes in the future, the changes will be restricted to the service object and will not affect the customer.

Example

Let's look at the Person class that represents "Person" and Department class that represents "Department:

class Person{    public Department Department { get; set; }}
class Department{    private string _chargeCode;    public Person Manager { get; set; }    public Department(Person manager)    {        Manager = manager;    }}

If the customer wants to know who a person's manager is, he must first obtain the Department object:

Person john = new Person();var manager = john.Department.Manager;

This code exposes the working principle of the Department to the customer, so the customer knows that the Department is used to track the message "manager. If the Department is hidden from the customer, coupling can be reduced. To this end, we create a simple delegate function in Person:

class Person{    public Department Department { get; set; }    public Person GetManager()    {        return Department.Manager;    }}

Now, I have to modify all the users of Person and change them to the new function:

Person john = new Person();var manager = john.GetManager();

After modifying all the customers, you can set the Department get of Person to private.

public Department Department { private get; set; }

Of course, you can also change the attribute to a private field:

private Department _department;
Summary

"Encapsulation" is one of the most critical features, even if it is not the most critical feature of an object. "Encapsulation" means that the object should be aware of as few other parts of the system as possible. In this way, once a change occurs, there will be fewer objects to understand the change.

6 Remove Middle Man (Remove Man in the Middle) Summary

A class performs too many simple delegation actions.

Allows the customer to directly call the delegate class.

Motivation

Section 5th describes the benefits of "encapsulating trusted objects. However, such encapsulation also requires a price, that is, whenever the customer wants to use the new feature of the delegate class, a simple delegate function must be added to the server. This process will be very painful as the nature of the delegate class increases. The service class is completely "man-in-the-middle". In this case, the customer needs to directly call the trustee class.

Example

Let's take a look at the above example of "People and Departments.

class Person{    public Department Department { private get; set; }    public Person GetManager()    {        return Department.Manager;    }}
class Department{    private string _chargeCode;    public Person Manager { get; set; }    public Department(Person manager)    {        Manager = manager;    }}

To find someone's manager, the customer code may be written as follows:

Person john = new Person();var manager = john.GetManager();

In this way, both encapsulation and Department are simple. However, if a large number of functions do this, they have to place a large number of delegate actions between persons. This is the time to remove the man in the middle.

First, create a function in Person to obtain the delegate object:

public Department GetDepartment(){    return Department;}

Then process each delegate function one by one. For each such function, find the function used by Person and modify it so that it obtains the entrusted object first, and then directly uses the latter:

var manager = john.GetDepartment().Manager;

Then you can delete the GetManager () function of Person.

Summary

It is hard to say to what extent hidden is the most suitable. As the system changes, the "appropriate degree of hiding" Scale also changes accordingly. Yesterday you felt like an appropriate package. Today it may seem clumsy. Fortunately, we have Hide Delegate and Remove Middle Man, because we can make constant adjustments while the system is running.

Refactoring means you never have to say sorry-you just need to fix the problem.

 

To Be Continued ......

Related Article

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.