关于java上下文的委托示例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2989005/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 15:18:24  来源:igfitidea点击:

delegation example regarding java context

java

提问by sarah

What is delegation in Java? Can anyone give me a proper example?

Java中的委托是什么?谁能给我一个正确的例子?

回答by aioobe

If you're referring to the delegation pattern, wikipedia has a great example, written in java.

如果你指的是委托模式,维基百科有一个很好的例子,用 java 编写

I believe the longer example of the page above is the best one:

我相信上面页面中较长的示例是最好的示例:

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()
    }
}

回答by Andreas Dolk

That's delegation - exactly like in the real world:

那是委托——就像在现实世界中一样:

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;
   }
}

(Added Worker interface to get closer to the Delegator pattern)

(增加了 Worker 接口以更接近 Delegator 模式)

回答by Sankalp

Same example as aioobebut changed the class names to more intuitive ones. Deriving analogy to real world examples.

aioobe相同的示例,但将类名称更改为更直观的名称。推导与现实世界的例子的类比。

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();
    }
}

回答by Jolly Good

Here is a simple example of how Delegation is used:

以下是如何使用委托的简单示例:

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();
    }
}