委派模式
2015-07-19 09:26
323 查看
委派模式(Delegate)是面向对象设计模式中常用的一种模式。这种模式的原理为类B和类A是两个互相没有任何关系的类,B具有和A一模一样的方法和属性;并且调用B中的方法,属性就是调用A中同名的方法和属性。B好像就是一个受A授权委托的中介。第三方的代码不需要知道A的存在,也不需要和A发生直接的联系,通过B就可以直接使用A的功能,这样既能够使用到A的各种公能,又能够很好的将A保护起来了。一举两得,岂不很好!下面用一个很简单的例子来解释下:
<span style="font-size:18px;">class A{ void method1(){...} void method2(){...} } class B{ //delegation A a = new A(); //method with the same name in A void method1(){ a.method1();} void method2(){ a.method2();} //other methods and attributes ... } public class Test{ public static void main(String args[]){ B b = new B(); b.method1();//invoke method2 of class A in fact b.method2();//invoke method1 of class A in fact } }</span>
相关文章推荐
- 在windows环境下,使用Eclipse和gcc-Linaro工具链,对BeagleBone进行交叉编译和远程部署
- 欢迎使用CSDN-markdown编辑器
- C# 条件与&&与条件或||的使用总结
- Tree Recovery-UVA 536
- 那些神奇的"大师"是如何炼成的(科学、迷信、心理)
- Android Fragment
- 敏捷开发之Scrum扫盲篇
- 嵌入式学习答疑
- Android之——Handler随笔
- Merge Two Sorted Lists
- poj1649 Rescue(BFS+优先队列)
- (win8系统)VMware-workstation安装教程(图文)
- java虚拟机学习笔记 一
- 《textanalytics》课程简单总结(4):课程总结
- 三次面试总结
- [LeetCode] Longest Palindrome Substring 具体分析
- UIPopoverController和UIActionSheet在iPad中使用
- 7.18使用<a>标签
- cocos2d-x-3.1.1工程创建
- TCP/IP网络简介(来自与51CTO学院视频授课内容)