Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

A Question In The Monkhouse Book

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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 ]
 
Merlin Chen
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Maybe he went home and went to bed. And took this tiny ad with him:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic