Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement delegate pattern (like in objective-c ) in java

I looked at an example where delegation pattern is explained for java. Didn't find much use for it (excuse the ignorance) as i feel it lacks the flexibility in objective-c. Is there way to dynamically set the delegate object as one can do in objective-c. Isn't that the whole point of delegation? My knowledge of java is very preliminary , so please explain a bit in detail.

like image 896
Rakesh Avatar asked Feb 22 '13 19:02

Rakesh


People also ask

How do you call a delegate method in Objective C?

To create one, you define a class that implements the delegate methods you're interested in, and mark that class as implementing the delegate protocol. Then you could create an instance of MyClass and assign it as the web view's delegate: MyClass *instanceOfMyClass = [[MyClass alloc] init]; myWebView.

What is delegate pattern in Java?

In software engineering, the delegation pattern is an object-oriented design pattern that allows object composition to achieve the same code reuse as inheritance. In delegation, an object handles a request by delegating to a second object (the delegate). The delegate is a helper object, but with the original context.

Is there delegate in Java?

The Java delegate system was introduced in version 5.0.


2 Answers

I think there are many ways ways to implement delegation pattern in Java, but probably none which feels like a built-in.

Take a look at the Wikipedia example. Your basic option is to manually implement an interface, and then simply forward the calls to a concrete instance which you can change during run-time as much as you wish.

Now depending on what tools you have and can use, you can make this forwarding more automatic. One idea, is to use aspect-oriented programming, like AspectJ.

Having an aspect compiler (or runtime) you could utilize annotations and come up with a simple extension to the language:

class Person {
  @Delegate Animal animal;
  ...
}

You'd then have to write an aspect that finds @Delegates and automatically adds forwarding methods in the class'es (eg. Person) interface.

If you are able to use a more groovy JVM language, then you wouldn't even have to write a single line of code, because such languages have delegates in the standard library:

You can have a look here, to see how it's done in Groovy. (essentialy exactly like the Person example syntax I came up with... but built-in!)

like image 82
emesx Avatar answered Sep 17 '22 23:09

emesx


This is how you fake a functional idiom in Java. Yes, it's hideous. If you want elegant functional idioms, use a language that doesn't treat functions like red-headed step-children. If you want more compile time safety, you can of course use more specific interface definitions so that you don't get IndexOutOfBounds and ClassCastException all over the place from programming errors.

public interface DelegatedFunction<T> {

T call(Object... args);

}

public class DoesSomeDelegatedTask {

  private List<DelegatedFunction<String>> delegatedFunctions = new ArrayList<>(1);

  public void addFunction(DelegatedFunction<String> function) {
    delegatedFunctions.add(function);
  }

  public void execute() {
    for (DelegatedFunction<String> function: delegatedFunctions) {
      System.out.println(function(something, someotherthing, whatever));
    }
  }
}

public class Main {
  public static void main(String[] args) throws Exception {
    DoesSomeDelegateTask doer = new DoesSomeDelegatedTask();
    doer.addFunction(new DelegatedFunction<String> () {
      @Override
      public String call(Object... args) {
        return ((SomeThings) args[0]).whatever((SomeOtherThing) args[1]//you get the idea
      }
    }
    doer.execute();
  }
}
like image 36
Affe Avatar answered Sep 17 '22 23:09

Affe