Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

B&S: Data Layer Design with Singleton?

 
Darya Akbari
Ranch Hand
Posts: 1855
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
     
    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 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
     
    Darya Akbari
    Ranch Hand
    Posts: 1855
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 42
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
     
    Ziji (Jay) Zhang
    Ranch Hand
    Posts: 42
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic