1

I have a code like this:

public class Hero extends Costam {

    private static Hero heroInstance;

    Weapon weapon;
    static Image idleImg = new ImageIcon("idle.png").getImage();
    static Image movingImg = new ImageIcon("moving.png").getImage();
    static Image fallingImg = new ImageIcon("falling.png").getImage();
    static Image jumpingImg = new ImageIcon("jumping.png").getImage();

    private LinkedList<Sprite> sprajty;
    private ArrayList<Segment> plansza;

    static int[] idleArray = new int[]{0, 1, 1, 2, 2, 3}, movingArray = new int[]{0, 1, 2, 3, 4, 5},
            fallingArray = new int[]{0}, jumpingArray = new int[]{0, 1, 2};

    static ArrayList<Image> obrazy = new ArrayList<Image>() {
        {
            add(idleImg);
            add(movingImg);
            add(fallingImg);
            add(jumpingImg);

        }
    };

    static ArrayList<int[]> tablice = new ArrayList<int[]>() {
        {
            add(idleArray);
            add(movingArray);
            add(fallingArray);
            add(jumpingArray);

        }
    };

    private Hero(ArrayList<Segment> plansza, LinkedList<Sprite> sprajty) {
        super(new Sprite(plansza, obrazy, tablice));
        this.plansza = plansza;
        weapon = new BasicWeapon(new ImageIcon("blue-portal.png").getImage());
        this.sprajty = sprajty;

        velocityX = 8;
    }

    //SINGLETON - ten get instance btw
    public Hero getInstance ()
    {
        if (heroInstance == null)
        {
            heroInstance = new Hero(plansza, sprajty);
        }
        return heroInstance;
    }

    public static Hero getInstance (ArrayList<Segment> plansza, LinkedList<Sprite> sprajty)
    {
        if (heroInstance == null) {
            heroInstance = new Hero(plansza,sprajty);
        }
        return heroInstance;
    }

As you can see, there is a getInstance method with parametres, which is not acceptable in Singleton. How to resolve it? Thank you. I tried to replace it by init method, but have no idea how to continue it.

2
  • 2
    This doesn't look like it should be a singleton, or any special kind of class at all. Commented Jan 9, 2016 at 23:40
  • Why should the Hero class be a singleton -- what is the reason for this design? Commented Jan 10, 2016 at 1:27

1 Answer 1

1

Right. It's not a Singleton. It is a Multiton which expands on the singleton concept to manage a map of named instances as key-value pairs.

Sign up to request clarification or add additional context in comments.

1 Comment

So, the code is proper and I should consider it as Mutliton? ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.