委托模式-Delegate
来源:互联网 发布:js字符串可逆加密算法 编辑:程序博客网 时间:2024/06/05 06:52
委托模式是软件设计模式中的一项基本技巧。在委托模式中,有两个对象参与处理同一个请求,接受请求的对象将请求委托给另一个对象来处理。委托模式是一项基本技巧,许多其他的模式,如状态模式、策略模式、访问者模式本质上是在更特殊的场合采用了委托模式。委托模式使得我们可以用聚合来替代继承。
在这个例子里,类模拟打印机Printer拥有针式打印机RealPrinter的实例,Printer拥有的方法print()将处理转交给RealPrinter的方法print()。
class RealPrinter { // the "delegate"
void print() {
System.out.print("something");
}
RealPrinter p = new RealPrinter(); // create the delegate
void print() {
p.print(); // delegation
}
// to the outside world it looks like Printer actually prints.
public static void main(String[] args) {
Printer printer = new Printer();
printer.print();
}
}
复杂的Java例子
通过使用接口,委托可以做到类型安全并且更加灵活。在这个例子里,类C可以委托类A或类B,类C拥有方法使自己可以在类A或类B间选择。因为类A或类B必须实现接口I规定的方法,所以在这里委托是类型安全的。这个例子显示出委托的缺点是需要更多的代码。
interface I {
void f();
void g();
public void f() { System.out.println("A: doing f()"); }
public void g() { System.out.println("A: doing g()"); }
public void f() { System.out.println("B: doing f()"); }
public void g() { System.out.println("B: doing g()"); }
}
class C implements I {
// delegation
public void toA() { i = new A(); }
public void toB() { i = new B(); }
}
public class Main {
public static void main(String[] args) {
C c = new C();
c.f(); // output: A: doing f()
c.g(); // output: A: doing g()
c.toB();
c.f(); // output: B: doing f()
c.g(); // output: B: doing g()
}
}
在这个例子里,类模拟打印机Printer拥有针式打印机RealPrinter的实例,Printer拥有的方法print()将处理转交给RealPrinter的方法print()。
class RealPrinter { // the "delegate"
void print() {
System.out.print("something");
}
}
class Printer { // the "delegator"RealPrinter p = new RealPrinter(); // create the delegate
void print() {
p.print(); // delegation
}
}
public class Main {// to the outside world it looks like Printer actually prints.
public static void main(String[] args) {
Printer printer = new Printer();
printer.print();
}
}
复杂的Java例子
通过使用接口,委托可以做到类型安全并且更加灵活。在这个例子里,类C可以委托类A或类B,类C拥有方法使自己可以在类A或类B间选择。因为类A或类B必须实现接口I规定的方法,所以在这里委托是类型安全的。这个例子显示出委托的缺点是需要更多的代码。
interface I {
void f();
void g();
}
class A implements I {public void f() { System.out.println("A: doing f()"); }
public void g() { System.out.println("A: doing g()"); }
}
class B implements I {public void f() { System.out.println("B: doing f()"); }
public void g() { System.out.println("B: doing g()"); }
}
class C implements I {
// delegation
I i = new A();
public void f() { i.f(); }public void g() { i.g(); }
// normal attributespublic void toA() { i = new A(); }
public void toB() { i = new B(); }
}
public class Main {
public static void main(String[] args) {
C c = new C();
c.f(); // output: A: doing f()
c.g(); // output: A: doing g()
c.toB();
c.f(); // output: B: doing f()
c.g(); // output: B: doing g()
}
}
0 0
- 委托模式( delegate)
- 委托(delegate)模式
- 委托模式-Delegate
- Delegate委托设计模式
- Observer模式,delegate(委托)模式
- Strategy模式与Delegate委托
- Strategy模式与Delegate委托
- delegate代理(委托)设计模式
- IOS-委托代理(Delegate)模式
- iOS 委托代理delegate设计模式
- iOS 委托代理delegate设计模式
- 设计模式之委托代理(delegate)
- IOS-设计模式之委托(delegate)
- OC教程5-委托delegate模式
- 委托(Delegate)
- 委托(Delegate)
- delegate委托
- 委托(delegate)
- 移除UTF-8文件頭的BOM
- Redis源码分析(二十九)--- bio后台I/O服务的实现
- 关于CPU节能模式你应该知道的东西
- iOS开发中能提高效率的常用宏
- vim syntax highlighting for scala : A bash one liner
- 委托模式-Delegate
- 第九周 项目一 do-while语句
- Mybatis返回Map的一种实现
- python ImportError: No module named
- linux 获取某一天的日期
- 【Git学习笔记】历史版本中穿梭,仅需要两步git log \git rest --hard
- Tomcat负载均衡原理详解及配置(Apache2.2.19+Tomcat7.0.12)
- 【Leetcode】Best Time to Buy and Sell Stock II
- 第十周项目4-大奖赛计分(十个评委)