Write C# delegate in java
Asked Answered
D

1

1

How can I write code below in java version?

I have read similar questions, but they are confusing, they answered that java didn't have delegate feature like c# had, in other hand they answered with their delegate implementation in java, but nothing is similar with my condition. I really hope it's clear on this question. I have been getting stuck since a week

    class Action
    {
        public delegate void ActionDelegate();
        public static ActionDelegate OnAction;

        public void DoAction()
        {
            Console.WriteLine("Action A");
            if (!ReferenceEquals(OnAction, null))
                OnAction();
        }
    }
    class TaskA
    {
        public TaskA()
        {
            Action.OnAction += DoTaskA;
        }
        private void DoTaskA()
        {
            Console.WriteLine("Do Task A");
        }

    }
    class TaskB
    {
        public TaskB()
        {
            Action.OnAction += DoTaskB;
        }
        private void DoTaskB()
        {
            Console.WriteLine("Do Task B");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            TaskA taskA = new TaskA();
            TaskB task = new TaskB();
            Action action = new Action();
            action.DoAction();
        }
    }

Output:

Action A
Do Task A
Do Task B
Press any keys to continue...
Designed answered 6/1, 2020 at 2:57 Comment(2)
Java uses a single-method interface for this concept. You may optionally apply @FunctionalInterface to such an interface.Intension
Could you give an example about how do you communicate between classes as code below did?, when Action A is called than Task A and Task B is called using the concept since i am newbie on java, what i understand from@FunctionalInterface is only to ensure that the functional interface can’t have more than one abstract method.Designed
H
1

something similar in java will be to use an interface

you can get the following results

Action A
Do Task A
Do Task B

with the codes below.

import java.util.ArrayList;
public class HelloWorld{

     public static void main(String []args){ 

            TaskA taskA = new TaskA();
            TaskB task = new TaskB();
            Action action = new Action();
            action.doAction();
     }
}

interface ActionDelegate {
    void doAction();
}

class Action{

    static public ArrayList<ActionDelegate> onAction = new ArrayList<>();
    public void doAction(){
        System.out.println("Action A");
        for(ActionDelegate ad: onAction){
            ad.doAction();
        }
    }
}
class TaskA implements ActionDelegate{

    TaskA(){
        Action.onAction.add(this);
    }
    public void doAction(){ 
            System.out.println("Do Task A");
    }

}

class TaskB implements ActionDelegate{

    TaskB(){
        Action.onAction.add(this);
    }
    public void doAction(){ 
            System.out.println("Do Task B");
    }

}
Hals answered 6/1, 2020 at 3:23 Comment(1)
Thanks for your answers, it helps me , it seems observer pattern implementation.Designed

© 2022 - 2024 — McMap. All rights reserved.