• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to test the implemention of Singleton pattern?

 
WarrenII Pan
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,everyone! I just get around to do my assignment of B&S. I got a problem in implementing the Singleton pattern. To make a long story short,I write the following code to explain this situation:
There are two class, DataAccess and Test . The DataAccess class uses Singleton pattern and the Test class is to test whether the former class has implemented this pattern correctly.





My question is when I open several MS-DOS windows to run Test class separately, the result is not as what I expected. I think inthe first wndow,it shoud display:

Before setI,the Value of i:0
Is the same Object of DataAccess?DataAccess@757aef
After setI,the Value of i:10

and in the second window it should be:

Before setI,the Value of i:10
Is the same Object of DataAccess?DataAccess@757aef
After setI,the Value of i:20

But all the windows I opened display the same thing as in the first window.
what's wrong?
Your advice would be greatly appreciated
WarrenII Pan

[Andrew: Put code between [code] and [/code] ubb tags]
[ July 17, 2005: Message edited by: Andrew Monkhouse ]
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are running the above program in two different DOS windows, right?

If you are, you are running two completely separate instances of the JVM with absolutely no sharing of anything.

Is that you really want to do?
 
WarrenII Pan
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Barry and Andrew. Yes, I am running the above program in two different DOS windows. But all the windows I opened display the same object"DataAccess@757aef". Now it seem to me that it should be tested in RMI environment,is it rihgt?

Regards,
WarrenII Pan
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 12007
215
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi WarrenII,

As Bary said, if you are running in different JVMs, they are not sharing memory. The fact that your example shows that the local address of the variable within each separate JVM is coincidentally the same is irrelevant - if you and I both have our own copies of the same book, and we both happen to be open on page 5, it does not mean that there is only one book.
Now it seem to me that it should be tested in RMI environment,is it rihgt?
Well I would test with multiple threads in a single JVM before going to a Sockets or RMI test, but you could jump the step if you really wanted to.

Regards, Andrew
[ July 18, 2005: Message edited by: Andrew Monkhouse ]
 
WarrenII Pan
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Andrew. According to your instruction, I understand my mistake completely. Just like other friends in this forum said, it is always easy to agree with you. With all my friends here like you and others, I am sure I can pass the SCJD Exam definitely. Thanks the greatest forum and the greatest moderator!

Regards,
WarrenII Pan
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic