0

so i'm coming from a c#.net background,, and i'm trying to do this in java

Class A{

public delegate void EventHandler();

    public static event EventHandler workComplete();

void doWork(){

    // DO WORK  

}

void onWorkComplete(){

    // Riase Event

    workComplete();

}

} Class Main{

A a = new A();

a.workComplete += () -> {

    // On Class A Complete Work

    Console.WriteLine("Work Complete");

`
}; }

I search for delegates in Java sense event are requers delegates as in c#.net,, but it's look like that Java don't have delegates ,, then i star to search how events work in Java then i fall into three things 1- Action Listener 2- Events Listener 3- and a lot of interfaces 4- observer pattern

and it's all mix up to me ,, maybe because i just understand C# events that's uses delegates.

So if any one could give a sample example like the c#.net code above ,,, in Java that's will be a very helpful.

Thank You.

1
  • Java uses the observer pattern (commonly reffered to as Listeners in java) for events. Google java listeners for examples. Commented Nov 30, 2014 at 18:08

1 Answer 1

1

I'm going to make this quick and dirty. Unfortunately, in Java you'll have to write more code to produce the same results.

I've split the code into 3 sections:

  • an interface which kind of resembles the delegate declaration
  • class A which contains the event declaration, subscription logic and raise logic
  • class B which is the "event consumer" and which declares the event handler

Good luck!

// this is what you used to have as a delegate declaration {
public interface IWorkCompletedListener {
    public void someMethodName(Object sender, Object args);
}
// }

// class A will be the event provider
public class A {

    // this is what you used to have for an event declaration {
    // used for the storage of listeners
    private static Vector<IWorkCompletedListener> workComplete = new Vector<IWorkCompletedListener>();

    // used for +=
    public static void addWorkCompleteListener(IWorkCompleteListener listener) {
        A.workComplete.add(listener);
    }
    // used for -=
    public static void removeWorkCompleteListener(IWorkCompleteListener listener) {
        A.workComplete.remove(listener);
    }
    // }

    // this is what you would use to raise the event {
    private static void raiseWorkCompletedListener(Object sender, Object args) {
        for (IWorkCompletedListener listener : A.workComplete)
            listener.someMethodName(sender, args);
    }
    // }

    // and the test code
    public static void doWork() {
        // Stuff goes here
        A.raiseWorkCompletedListener(null, null);
    }

}

// class B will be the event consumer
public class B {

    public static void subscribeAndCallDoWork() {
        // this is what += used to be
        A.addWorkCompleteListener(new IWorkCompletedListener {
            public void someMethodName(Object sender, Object args) {

                // I didn't want to force class B to implement the interface
                // hence I used an anonymous interface implementation
                // from which I'm calling the "event handler"
                B.onWorkCompleted(sender, args);

            }
        });

        A.doWork();
    }

    private static onWorkCompleted(Object sender, Object args) {
       // expect to be called here
    }

}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.