Delegation Pattern

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!
        }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值