jeudi 12 avril 2018

Java observer / visitor, static visitor

So I have this class which basically handles scene loading. At different point of the program, you notify the visitor with different layer Ids which are supposed to load.

Since I have only one scene handler and logically there can not be more, and the ability to call the loadLayer method and Subscribe method has to be available everywhere, I decided to make it static.

Now my question is: Is this considered to be a bad OOP practice? If so, how should I go about making the class non-static and accessible from everywhere without having to pass it through arguments or in some other way.

Thanks for the help.



package backend.events;

import java.util.ArrayList;

public final class LoadLayerHandler
{
    private LoadLayerHandler()
    {

    }
    private final static ArrayList<LoadLayerEvent> subscribers = new ArrayList<LoadLayerEvent>();
    x
    public static final void subscribe(LoadLayerEvent subscriber)
    {
        subscribers.add(subscriber);
    }

    public static final void loadLayer(int layer)
    {
        for(LoadLayerEvent i : subscribers)
        {
            if(i.isBlackList() == false)
            {
                for(int j : i.getLayers())
                {
                    if(j == layer)
                    {
                        i.loadLayer();
                        break;
                    }
                    else
                        i.unloadLayer();
                }
            }
            else
            {
                for(int j : i.getLayers())
                {
                    if(j == layer)
                    {
                        i.unloadLayer();
                        break;
                    }
                    else
                        i.loadLayer();
                }
            }
        }
    }

Aucun commentaire:

Enregistrer un commentaire