delegation example regarding java context

后端 未结 4 2151
情话喂你
情话喂你 2020-12-14 02:03

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

相关标签:
4条回答
  • 2020-12-14 02:16

    If you're referring to the delegation pattern, wikipedia has a great example, written in 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()
        }
    }
    
    0 讨论(0)
  • 2020-12-14 02:26

    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();
        }
    }
    
    0 讨论(0)
  • 2020-12-14 02:39

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

    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();
        }
    }
    
    0 讨论(0)
  • 2020-12-14 02:40

    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)

    0 讨论(0)
提交回复
热议问题