• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

B&S: Data Layer Design with Singleton?

 
Ranch Hand
Posts: 1855
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
     
    author and jackaroo
    Posts: 12200
    280
    Mac IntelliJ IDE Firefox Browser Oracle C++ Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
     
    Ranch Hand
    Posts: 42
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
     
    A teeny tiny vulgar attempt to get you to buy our stuff
    a bit of art, as a gift, the permaculture playing cards
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic