Iterate over methods

357 views Asked by At

I am having some trouble with this for loop. I am not sure if I have correctly set up this method.

Here is what it is asking me to do

Iterate over all the elements in the subscribers array. You should use the easier version of the for loop to do this. Inside the loop: If the element is not equal to null, then call the notify method on that subscriber. When you call that method you need to give it a Message argument: use the same Message that was given to this method. Note that here you are inside the notify method, and you're calling the notify method again. However, this is not recursion because the notify method you're calling is on a completely different ISubscriber instance.

package message;

public class Broadcaster implements IPublisher, ISubscriber {
    private Object [] members;
    private int i = 0;

    Broadcaster(int a) {
        a = 4;
        Object[] array = new Object[a];  
        members = array;    
    }

    @Override
    public void subscribe(ISubscriber subscriber) {
        members[i] = subscriber;       
        i++;   
    }

    @Override
    public void unsubscribe(ISubscriber subscriber) {
    }

    @Override
    public void notify(Message msg) {
    }
}
1

There are 1 answers

0
Dawnkeeper On

Usually you don't use an Array for this but something dynamic. e.g. a LinkedList

import java.util.LinkedList;
public class Broadcaster
{
    LinkedList<ISubscriber> subscribers = new LinkedList<ISubscriber>();

    public void subscribe(ISubscriber addMe)
    {
        if(addMe!= null)
        {
            if(! subscribers.contains(addMe))
            {
                this.subscribers.add(addMe);
            }
        }
    }

    public void unsubscribe(ISubscriber removeMe)
    {
        if(removeMe!= null)
        {
            this.subscribers.remove(removeMe);
        }
    }

    public void notifySubscribers() //this is called notify in your code
    {
        for(ISubscriber current : subscribers)
        {
            if(current != null)
            {
                current.beNotified(new Message());
            }
        }
    }
}

interface ISubscriber
{

    void beNotified(Message msg);    //also called notify for you; if possible avoid
}



If you are forced to use an array you get something like this:

public class Broadcaster
{
    int maxsubscribers = 4;
    ISubscriber[] subscribers = new ISubscriber[maxsubscribers];

    public void subscribe(ISubscriber addMe)
    {
        if(addMe != null)
        {
            for(int i = 0; i <maxsubscribers;i++)
            {
                if(subscribers[i] != null)
                {
                    if(subscribers[i].equals(addMe))
                    {
                        return; //already in array; not adding 
                    }
                }
            }

            for(int i = 0; i <maxsubscribers;i++)
            {
                if(subscribers[i] == null)
                {
                    subscribers[i] = addMe;
                }
            }
        }
    }

    public void unsubscribe(ISubscriber removeMe)
    {
        if(removeMe != null)
        {
            for(int i = 0; i <maxsubscribers;i++)
            {
                if(subscribers[i].equals(removeMe))
                {
                    subscribers[i] = null;
                }
            }
        }
    }

    public void notifySubscribers() //this is called notify in your code
    {
        for(ISubscriber current : subscribers)
        {
            if(current != null)
            {
                current.beNotified(new Message());
            }
        }
    }
}

The downside of using an array is that you either can only store a certain number of elements or have to change the size of the array as soon as you run out of free slots.

As you can see it is also a bit harder to avoid that subscribers are added twice to the array. It requires an addition loop just to check this while the LinkedList offers a convenient method for this check.

And last but not least: While it is possible to have a notify() and a notify(Message msg) method that do entirely different things this is not good practice as it can easily lead to mixing up the functions.