Delegation Pattern
来源:互联网 发布:java 时间戳转换为秒 编辑:程序博客网 时间:2024/06/06 20:40
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 ]
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
- Decorator: is similar to delegation and uses it heavily to accomplish its task.
- Delegation is very common, it is used by many other pattern like Visitor, Observer, Strategy, and Event Listener.
Structure
This example is actually more advanced than just plain delegation, it shows how delegation makes it easy to compose behaviors at run-time.
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! }}
- Delegation Pattern
- (翻译)代理模式(Delegation pattern)
- Delegation
- C++基本功和 Design Pattern系列 Inheritance VS Delegation
- C++基本功和 Design Pattern系列(1) - Inheritance VS Delegation
- 设计模式:委托模式Delegation Pattern在MVC中的应用
- C++基本功和 Design Pattern系列(1-3) - Inheritance VS Delegation
- delegation模式
- Delegation model
- delegation 委托
- delegation机制
- 分发delegation
- 返还delegation
- 代理Delegation
- Kerberos Delegation
- Event delegation
- Delegation Token
- 关于Kerberos delegation
- C#利用NOPI处理Excel的代码
- Android开发之shape的使用
- 九九乘法表
- ScrollView嵌套GridView的解决办法
- 使用boundle在界面间传递对象
- Delegation Pattern
- 【ASP.NET开发】ASP.NET(MVC)三层架构知识的学习总结
- Krux Digital:调查显示用户平均每访问一个页面就会触发56次数据搜集行为
- 大三了,还是纠结
- iOS 数据存储之我见
- js中对输入内容进行特殊符号校验
- 计划 List
- TX面试——祭奠我死去的第一波校招
- Openwrt的FlashLayout