【问题标题】:delegation example regarding java context关于 java 上下文的委托示例
【发布时间】:2011-02-28 15:52:13
【问题描述】:

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

【问题讨论】:

    标签: java


    【解决方案1】:

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

    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 模式)

    【讨论】:

    • 是否应该创建private Secretary secretary = new secretary();的对象来调用return secretary.work();
    • @gbox 当然可以。秘书必须由人力资源部聘用,并@injected到老板办公室:)
    【解决方案2】:

    如果您指的是委托模式,wikipedia has a great example, written in 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()"); }
    }
    
    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()
        }
    }
    

    【讨论】:

    • 谢谢!委托优于继承的一个好处是运行时更改,但我能想到的所有示例都更像是组合而不是委托。这个例子清楚地表达了我所读文章的作者的意思。
    • 这个答案应该被标记为最好的,因为它最好地描述了委托的原则!
    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

      这是一个如何使用委托的简单示例:

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

      【讨论】:

        猜你喜欢
        • 2014-04-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多