aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes A Question In The Monkhouse Book Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "A Question In The Monkhouse Book" Watch "A Question In The Monkhouse Book" New topic
Author

A Question In The Monkhouse Book

Merlin Chen
Greenhorn

Joined: Jan 06, 2007
Posts: 11
In the Book,it says:
Since there is only one physical file on disk, it is tempting to consider making the DvdFileAccess class a singleton�coding the class in such a way that only one instance of DvdFileAccess can exist at any given time. However, a lot of work can be performed in parallel if multiple clients are working on a multiple-CPU system, for example, converting
between a DVD value object and the bytes on file, or searching through the data file. In addition, if we were to make the DvdFileAccess class a singleton, any class that uses the DvdFileAccess class would have to be coded differently than if it is a standard class�if we were to later decide that this same class can be used to process multiple data files (with some simple modifications), we would have to modify all the classes that use DvdFileAccess. Therefore, this class is not a singleton.
-----------------------------------------------------------------------
but in the source code,the DvdFileAccess class is still static .



I'm not quite understand about this.Is my understanding on the words "singleton" is wrong.

[ August 25, 2007: Message edited by: Merlin Chen ]

[ August 25, 2007: Message edited by: Merlin Chen ]
[ August 25, 2007: Message edited by: Merlin Chen ]

SCJP 5.0(84%)<br />SCHM<br />Sun Certified Homicidal Maniac
rinke hoekstra
Ranch Hand

Joined: Apr 06, 2007
Posts: 152
Originally posted by Merlin Chen:
In the Book,it says:
Since there is only one physical file on disk, it is tempting to consider making the DvdFileAccess class a singleton�coding the class in such a way that only one instance of DvdFileAccess can exist at any given time. However, a lot of work can be performed in parallel if multiple clients are working on a multiple-CPU system, for example, converting
between a DVD value object and the bytes on file, or searching through the data file. In addition, if we were to make the DvdFileAccess class a singleton, any class that uses the DvdFileAccess class would have to be coded differently than if it is a standard class�if we were to later decide that this same class can be used to process multiple data files (with some simple modifications), we would have to modify all the classes that use DvdFileAccess. Therefore, this class is not a singleton.
-----------------------------------------------------------------------
but in the source code,the DvdFileAccess class is still static .



I'm not quite understand about this.Is my understanding on the words "singleton" is wrong.


Hi Merlin,

On Andrew's reasoning: I don't think his reasoning to NOT make it a singleton are very strong (not so strange, because lots of others use a singleton, so apparantly there is no strong reasoning against it, as well as in favour of it. In the end I think chosing a singleton is mostly a matter of taste).

He gives the following reasons for not having a singleton:

* more efficient in case of multiple CPU system - I think this consideration is way beyond the scope of the project.
* any class that uses this file will not use a constructor, but has to create the instance in a special way - to my opinion, you tipically want only one other class to be aware of this class, so I don't see the point of this argument.

Then, on your question:

He creates a static instance of this class in another class. That's different from having a singleton. You could still create many other static instances in many other classes; these will all be different instances of this class. If you create a singleton, only one instance ever can be created - if this instance is a static or non static member of some other class doesn't matter.
However, because (I think) there is only one class using the DVDFileAccess class, the effect is about the same: because it is a static member, only one DVDFileAccess instance will be known.


Then, on all the questions:

Personally, I believe you have more chance of getting all your questions answered if you have one subject / question per thread. And it is less confusing for the readers...
[ August 25, 2007: Message edited by: rinke hoekstra ]

_ _ ________________________ _ _ <br /> <br />Just SCJP (but 93%)
Merlin Chen
Greenhorn

Joined: Jan 06, 2007
Posts: 11
rinke,thanks for the answer.
You mean my last two questions are not clear enough?
I already post them again~~
[ August 25, 2007: Message edited by: Merlin Chen ]
 
 
subject: A Question In The Monkhouse Book