aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes B&S: Data Layer Design with Singleton? 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 "B&S: Data Layer Design with Singleton? " Watch "B&S: Data Layer Design with Singleton? " New topic
Author

B&S: Data Layer Design with Singleton?

Darya Akbari
Ranch Hand

Joined: Aug 21, 2004
Posts: 1855
Hi,

I want to finish my data layer design and like to share with you the design I have so far :

Adapter Pattern:

  • DB interface as my Target interface
  • data access class Data as my Adapter class.
  • the data file as class Database as my Adaptee class


  • Facade Pattern:
  • data access class Data as my Facade class.
  • the locking mechanism as class LockMaster as a subsystem class
  • the data file as class Database as another subsystem class

  • LockMaster hold a static HashMap to keep locked records while Database hold a static ArrayList to cache the records read from the provided data file.

    Singleton Pattern:

    I plan to implement both subsystem classes LockMaster and Database as Singleton .

    Is there any reason I should not do so

    Regards,
    Darya


    SCJP, SCJD, SCWCD, SCBCD
    Andrew Monkhouse
    author and jackaroo
    Marshal Commander

    Joined: Mar 28, 2003
    Posts: 11484
        
      94

    Hi Darya,

    Isn't it redundant to have LockMaster and Database as Singletons, and simultaneously have static objects inside them?

    That is, if LockMaster is a Singleton, what is the advantage of using a static HashMap within it?

    Regards, Andrew


    The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
    Darya Akbari
    Ranch Hand

    Joined: Aug 21, 2004
    Posts: 1855
    Hi Andrew,

    it seems that making both classes Singleton is correct. The static HashMap in LockMaster is a leftover when the LockMaster class was not Singleton.

    I remove the static keyword from HashMap since the Singleton itself is already static and hence a static HashMap is really redundant.

    Thanks,
    Darya
    Ziji (Jay) Zhang
    Ranch Hand

    Joined: Dec 17, 2004
    Posts: 42
    This thread is really old,
    But anyway, I just post my thoughts here, I have been reading Max's book for
    a while, then recently I bought Book by Andrew and Terry. The 2nd edition is better in terms of mocking the real-world exam.

    Regarding the LockManager, and DataFileAccess (database) classes that
    are part of the Data class ( I prefer to refer this pattern as composition),
    in Andrew's book, both are declared as static member within dataclass (DvdFileAccess and ReservationManager are static members), therefore,
    for all instance of Data class, it is gauranteed to have only one instance of LockManager and DataFileAccess created when Data class is loaded, this is simple and straightforward.
    But If I use Singleton pattern for FileAccess, one question I have is how to pass the database file path into the DataFileAccess class if it is a singleton? Do I have to pass the path when I call DataFileAccess.createInstance()?

    When is the circumstance that I use a instance of Singleton over a class scoped static member ? I guess, If some one load the class twice, there will be two instances of DataFileAccess, but not the case in Singleton.


    Thanks, Ziji


    SCJP(94%)
    Ziji (Jay) Zhang
    Ranch Hand

    Joined: Dec 17, 2004
    Posts: 42
    This thread is really old,
    But anyway, I just post my thoughts here, I have been reading Max's book for
    a while, then recently I bought Book by Andrew and Terry. The 2nd edition is better in terms of mocking the real-world exam.

    Regarding the LockManager, and DataFileAccess (database) classes that
    are part of the Data class ( I prefer to refer this pattern as composition),
    in Andrew's book, both are declared as static member within dataclass (DvdFileAccess and ReservationManager are static members), therefore,
    for all instance of Data class, it is gauranteed to have only one instance of LockManager and DataFileAccess created when Data class is loaded, this is simple and straightforward.
    But If I use Singleton pattern for FileAccess, one question I have is how to pass the database file path into the DataFileAccess class if it is a singleton? Do I have to pass the path when I call DataFileAccess.createInstance()?

    When is the circumstance that I use a instance of Singleton over a class scoped static member ? I guess, If some one load the class twice, there will be two instances of DataFileAccess, but not the case in Singleton.


    Thanks, Ziji
     
    jQuery in Action, 2nd edition
     
    subject: B&S: Data Layer Design with Singleton?