lundi 9 mai 2016

Singleton pattern with current Thread type authority

I'm not interested in instantiating POJOs by passing parameters through constructors. Also my design is such that I only need one instance of every type. Instantiating more Objects from each type is not desired.
So let's consider I have a class Launcher.java. I use singleton pattern and get one instance through static getInstance() method. Launcher.java needs a component named Transceiver.java. I also instantiate this class through singleton pattern.
Now I want to, for example, pass some parameters to my Transceiver only and only through Launcher instance.
What I do is like:

public void setParameter (int parameter) throws IllegalTypeAccessException
{
    if (Strand.getCurrentStrand() instanceof Launcher)
    // or if (Thread.getcurrentThread() instanceof Launcher)
    {
        this.parameter = paramater;
    }
    else
    {
        IllegalTypeAccessException e = new IllegalTypeAccessException(Strand.getCurrentStrand().getClass().getTypeName()
                                   + " Is not authorized to set property for this object");
        throw e;
    }
}

By doing so, unauthorized accesses are controlled. Only one Java type can set them and because I'm using singleton pattern for every type I use, then only one object can do that. Accesses are controlled in terms of both Type and Instance.
Also there is no need to boring and prone to mistake parameter passing through constructors.
Now I want to ask, are there any flaws that I should consider in this type of design? Will I face problems using this pattern?
Please feel free to edit my question if you think it may be improved to have professional answers.

Aucun commentaire:

Enregistrer un commentaire