现在的位置: 首页 > 综合 > 正文

Delegation Pattern

2014年12月10日 ⁄ 综合 ⁄ 共 2718字 ⁄ 字号 评论关闭

http://best-practice-software-engineering.ifs.tuwien.ac.at/patterns/delegation.html

 

Delegation Pattern

"Delegation is like inheritance done manually through object composition."
[Lecture slides of course 601: "Object-Oriented Software Development" at the University of San Francisco ]

Multiple and single inheritance in UML

It is a technique where an object expresses certain behavior to the outside but in reality delegates responsibility for implementing that behaviour to an associated object. This sounds at first very similar to the proxy pattern, but it serves a much different
purpose. Delegation is an abstraction mechanism which centralizes object (method) behaviour.

The diagram above shows how multiple-inheritance behavior can be accomplished in Java for actual classes. Of course delegation is not limited to scenarios where multiple-inheritance has to be avoided (as Java does not support this), but can be seen in general
as an alternative to inheritance. The same functionality can also be accomplished with interfaces, however sometimes the relationship you want between two classes is actually one of containment rather than inheritance.

Applicability / Uses

Use the delegation pattern when:

  • you need to reduce the coupling of methods to their class
  • you have components that behave identically, but realize that this situation can change in the future.

Generally spoken: use delegation as alternative to inheritance. Inheritance is a good strategy, when a close relationship exist in between parent and child object, however, inheritance couples objects very closely. Often, delegation is the more flexible
way to express a relationship between classes.

This pattern is also known as "proxy chains". Several other design patterns use delegation - the State, Strategy and Visitor Patterns depend on it.

Related Patterns

Structure

This example is actually more advanced than just plain delegation, it shows how delegation makes it easy to compose behaviors at run-time.

UML diagram of the Delegation Pattern shown below.

Sample

First lets create an Interface for our Delegates, since Delegates can be interchanged they all must implement the same interface:

public interface ISoundBehaviour {
        
        public void makeSound();
}

public class MeowSound implements ISoundBehaviour {

        public void makeSound() {
                System.out.println("Meow");
        }
}

public class RoarSound implements ISoundBehaviour {

        public void makeSound() {
                System.out.println("Roar!");
        }
}

Now lets create a cat class which uses the MeowSound per default:

public class Cat {  
  private ISoundBehaviour sound = new MeowSound();  

  public void makeSound() {  
    this.sound.makeSound();  
  }  

  public void setSoundBehaviour(ISoundBehaviour newsound) {  
        this.sound = newsound;  
  }  
}

Finally a Main class to test our delegation pattern:

public class Main {
        public static void main(String args[]) {
                Cat c = new Cat();
                // Delegation
                c.makeSound();          // Output: Meow
                // now to change the sound it makes
                ISoundBehaviour newsound = new RoarSound();
                c.setSoundBehaviour(newsound);
                // Delegation           
                c.makeSound();          // Output: Roar!
        }
}

 

抱歉!评论已关闭.