关于java上下文的委托示例
Java中的委托是什么?有人能给我举个恰当的例子吗?如果您指的是委派模式 我相信上面页面的较长示例是最好的:关于java上下文的委托示例,java,Java,Java中的委托是什么?有人能给我举个恰当的例子吗?如果您指的是委派模式 我相信上面页面的较长示例是最好的: 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()"); } } clas
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
void toA() { i = new A(); }
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()
}
}
这就是授权——与现实世界完全一样:
public interface Worker() {
public Result work();
}
public class Secretary() implements Worker {
public Result work() {
Result myResult = new Result();
return myResult;
}
}
public class Boss() implements Worker {
private Secretary secretary;
public Result work() {
if (secretary == null) {
// no secretary - nothing get's done
return null;
}
return secretary.work();
}
public void setSecretary(Secretary secretary) {
this.secretary = secretary;
}
}
(添加了Worker接口以更接近Delegator模式)与相同的示例,但将类名更改为更直观的类名。从现实世界的例子中得出类比
public static void main(String[] args) {
Boss boss = new Boss();
boss.toDeveloper();
boss.f();
boss.g();
boss.toSrDeveloper();
boss.f();
boss.g();
}
interface I {
void f();
void g();
}
class Developer implements I {
public void f() {
System.out.println("Developer: f() is too hard for me.");
}
public void g() {
System.out.println("Developer: g() is not in my domain.");
}
}
class SrDeveloper implements I {
public void f() {
System.out.println("Sr. Developer: Okay, I'll see f()");
}
public void g() {
System.out.println("Sr. Developer: I'll do g() too.");
}
}
class Boss implements I {
// delegation
I i;
public void f() {
i.f();
}
public void g() {
i.g();
}
void toDeveloper() {
i = new Developer();
}
void toSrDeveloper() {
i = new SrDeveloper();
}
}
下面是如何使用委派的一个简单示例:
interface IDogBehaviour {
public void doThis();
}
class BarkSound implements IDogBehaviour {
public void doThis() {
System.out.println("Bark!");
}
}
class WagTail implements IDogBehaviour {
public void doThis() {
System.out.println("Wag your Tail!");
}
}
class Dog {
private IDogBehaviour sound = new BarkSound();
public void doThis() {
this.sound.doThis();
}
public void setNewBehaviour( IDogBehaviour newDo ){
this.sound = newDo;
}
}
class DelegationDemo {
public static void main( String args[] ){
Dog d = new Dog();
//delegation
d.doThis();
//change to a new behaviour type - wag tail
IDogBehaviour wag = new WagTail();
d.setNewBehaviour( wag );
//Delegation
d.doThis();
}
}
非常感谢。委托优于继承的一个好处是运行时的变化,但我能想到的所有例子都更像是组合而不是委托。这个例子清楚地表达了我读过的文章的作者的意思。这个答案应该被标记为最好的答案,因为它描述了最好的授权原则!应为
私人秘书秘书=新秘书()的对象代码>被创建为调用返回secretary.work()代码>?@gbox确定。秘书必须由人力资源部雇用,并@将注入老板的办公室:)