0

I have a class named Neuron, and when the Neuron object fires, it signals all of the objects of class Synapse to fire. I'm unsure how to send a signal to the Synapse from class Neuron, can anyone explain?

Bob Long
  • 51
  • 1
  • 8

2 Answers2

0

The Neuron class can be an event source for the Synapse class by using the PropertyChangeSupport class. The Synapse classes register as listeners and the Neuron class fires an event as needed.

Paul MacGuiheen
  • 628
  • 5
  • 17
0

The Neuron Object should contain a list of all instances of Synapse object, only then it will be able to send events or signals to Synapse objects. This is similar to Observer pattern. To give you an idea how this will work, please see the following

class Neuron {
    List<Synapse> subscribers = new ArrayList<>();
    private Neuron neuron;

    private Neuron(){}        

    public static Neuron getInstance(){
        if(neuron == null)
           neuron = new Neuron();
        return neuron;
    }

    public void addSubscribers(Synapse s){
        subscribers.add(s);
    }

    public void fireEvent(Event x){
        for(Synapse s: subscribers){
            subscriber.notifyEvent(x);
        }
    }
}





class Synapse{
    public void subscribe(){
       Neuron.getInstance().subscribe(this);
    }

    public void NotifyEvent(Event x){
       //to somthing...
    }
}