开发者

Getting a single instance of an object

开发者 https://www.devze.com 2023-01-10 21:06 出处:网络
Let\'s say I have a class A and this class has a List l. There is also a class B that extends A that will access the list l, as shown below. In my program, there must only be an instance of the class

Let's say I have a class A and this class has a List l. There is also a class B that extends A that will access the list l, as shown below. In my program, there must only be an instance of the class B (I'm using Singleton pattern), and there must be only one instance of the List l also, so I'm doing it like this:

public abstract class A {

    protected List<String> l;

    public A() {}

    protected synchronized List<String> getList() {
        if (l == null)
            l = new LinkedList<String>();
        return l;
    }
}

//---

public class B extends A {

    private static B instance;

    private B() {
        super();
    }

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

What I want to do is:

In one instance of B:

System.out.println(super.getList().size()); //must print 0
super.getList().add("a");
System.out.println(super.getList().size()); //will print 1

In another instance of B:

System.out.println(super.getList().size()); //should print 1, cause i've already
                                            //added "a", but prints 0
super.getList().add("b");
System.out.println(super.getList().size()); //should print 2, but prints 1

That is not working as expected though. What am I doing wrong? Can anyone help me?


EDITED:

Hi, Bill the Lizard, here are the two classes:

public abstract class A {
    protected static List<String> l;

    public A() {}

    protected static synchronized List<String> getList() {
        if (l == null)
            l = new LinkedList<String>();
        return l;
    }
}

//---

public class B extends A {
    private static B instance;

    private B() {
        super();
    }

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

    public void metodo() {
        System.out.println(super.getList().size());
        super.getList().add("a");
        System.out.println(super.getList().size());
    }
}

And this is how i create an instance of B:

public class Cla开发者_开发知识库ssTeste {
    public static void main(String[] args) {
        B b = B.getInstance();
        b.metodo();
    }
}


I've run your main revised code, with the following main method,

public static void main(String[] args) {
 B b1 = B.getInstance();
 b1.metodo();

 B b2 = B.getInstance();
 b2.metodo();
}

and got the following output, which matches what you're looking for

0
1
1
2


It's not a singleton if you have more than one instance of B. (You may have meant reference)

Why don't you just do:

public abstract class A {

    protected List l = new LinkedList();

    public A() {}

    protected List getList() {
        return l;
    }
}

'l' will only be instantiated when A is instantiated (i.e. when B is instantiated).

public class B extends A {

    private static B instance = new B();

    private B() {
        // Don't need this, implicitly use no-args super-constructor by default
        // super();
    }

    public static B getInstance() {
        return instance;
    }
}

'instance' will only be instantiated when the class B is referenced for the first time, so you're probably fine there.


This is working for me.

B b1 = B.getInstance();
B b2 = B.getInstance();

b1.metodo();
b2.metodo();

I'm getting the following output.

0
1
1
2

The call to b1.metodo() is printing the size of the empty list and the size after adding "a" once. The call to b2.metodo() is printing the same size again, then the new size after adding "a" again.


The singleton only lives for the life of the program. And it is not shared between other instances of your program.

It sounds like you expect this to run once and get an output of 1, and it exits, then run again and get an output of 2. Or run two programs and get on to output 1, and the other to output 2.

This only works if you are using threads, not whole processes, and it will still reset each time you kill the process and start a new one.

0

精彩评论

暂无评论...
验证码 换一张
取 消