Moving between objects --- Hide Delegate (hiding delegation relationships)

Source: Internet
Author: User

Moving between objects --- Hide Delegate (hiding delegation relationships)

The customer calls another object through one delegate class.

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

 

Motivation:

Encapsulation is one of the most shutdown features even if it is not the most shutdown feature of an object. "Encapsulation" means that each object should have less knowledge of other parts of the system. In this case, once a change occurs, we need to know little about this change-this will make the system easier.

 

Anyone who has studied object technology knows that although Java declares a field as public, you should hide the field of the object. As your experience grows, you will find that there are more things that can be encapsulated (worth it.

 

If a customer needs to obtain another object through the service object field and then call the latter's function, the customer must know this level of delegation relationship. In case of a change in the delegated relationship, the customer must also change accordingly. You can place a simple delegate function on the service object to hide the delegate relationship and remove this dependency. In this way, even if there are future changes in the relationship between Wei Guo, the changes will be restricted to the target customers and will not affect the customers.

 

Example:

This example starts with two classes: the Person class that represents the "Person" and the Department class that represents the "Department.

Class Person {

Department _ department;

Public Department getDepartment (){

Reutrn _ department;

}

 

Public void setDepartment (Department department ){

_ Department = department;

}

}

 

Class Department {

Private String _ chargeCode;

Private Person _ manager;

 

Public Department (Person manager ){

_ Manager = manager;

}

Public Person getManager (){

Return _ manager;

}

}

..............

If the customer needs to know who the manager of a person is, he must first obtain the Department object:

Manager = john. getDepartment (). getManager ();

This code exposes the working principle of the Department to the customer, so the customer knows that the Department is used to track the "manager" information. If the Department is hidden from the customer, coupling can be reduced. For this purpose,

You can create a simple delegate function in the Person class:

Public Person getManager (){

Return _ department. getManager ();

}

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

Manager = john. getManager ();

As long as you have completed the delegate relationship for all functions of the Department and modified all customers of the corresponding Person, you can remove the access function getDepartment () in the Person.

 

 

 

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.