fbpx
维基百科

委托模式

委托模式(delegation pattern)软件设计模式中的一项基本技巧。在委托模式中,有两个对象参与处理同一个请求,接受请求的对象将请求委托给另一个对象来处理。委托模式是一项基本技巧,许多其他的模式,如状态模式、策略模式访问者模式本质上是在更特殊的场合采用了委托模式。委托模式使得我们可以用聚合来替代继承,它还使我们可以模拟mixin

简单的Java例子 编辑

在这个例子里,模拟打印机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 attributes  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()  }  } 

参考 编辑

参见软件设计模式和后对象程序设计。


委托模式, delegation, pattern, 是软件设计模式中的一项基本技巧, 在中, 有两个对象参与处理同一个请求, 接受请求的对象将请求委托给另一个对象来处理, 是一项基本技巧, 许多其他的模式, 如状态模式, 策略模式, 访问者模式本质上是在更特殊的场合采用了, 使得我们可以用聚合来替代继承, 它还使我们可以模拟mixin, 简单的java例子, 编辑在这个例子里, 类模拟打印机printer拥有针式打印机realprinter的实例, printer拥有的方法print, 将处理转交给realpri. 委托模式 delegation pattern 是软件设计模式中的一项基本技巧 在委托模式中 有两个对象参与处理同一个请求 接受请求的对象将请求委托给另一个对象来处理 委托模式是一项基本技巧 许多其他的模式 如状态模式 策略模式 访问者模式本质上是在更特殊的场合采用了委托模式 委托模式使得我们可以用聚合来替代继承 它还使我们可以模拟mixin 简单的Java例子 编辑在这个例子里 类模拟打印机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 attributes 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 参考 编辑参见软件设计模式和后对象程序设计 取自 https zh wikipedia org w index php title 委托模式 amp oldid 78043539, 维基百科,wiki,书籍,书籍,图书馆,

文章

,阅读,下载,免费,免费下载,mp3,视频,mp4,3gp, jpg,jpeg,gif,png,图片,音乐,歌曲,电影,书籍,游戏,游戏。