user13346827
user13346827

Reputation:

Singleton Pattern Usage

public Singleton setValue(String string, Object object) {
        values.put(string, object);
        return this;
    }

    public static Singleton getInstance() {
        if (instance == null) {
            instance = new Singleton();
        }
        return instance;
    }

EDITTED This editted version is working well.

Upvotes: 0

Views: 57

Answers (2)

Method getInstance() is returning an Object type value.

You need to cast the object to: ((Singleton) Singleton.getInstance()).setValue(...)

If you want the class act like a builder, change the method to return a Singleton type object: public static Singleton getInstance() {..} and also in the setValue public static Singleton setValue(..)to be able to call multiple set as you wrote in the code.

Upvotes: 0

Jason
Jason

Reputation: 5244

setValue could chain by returning the instance of the class after performing some assignment.

    public Singleton setValue(String string, Object object) {
        // do some assignment
        return this;

    }

Upvotes: 1

Related Questions