Forums Register Login

B&S: Singleton and Facade

+Pie Number of slices to send: Send
Hi,

I've been searching the forum about this and there are TONS of topics. Yet I somehow can't really decide what is right or wrong.

Let's say I want to use a Facade like in Andrews book. My Data-class implements the interface provided by Sun, but has two worker-classes; one for locking(LockManager) and one for handling the file(FileManager). So in every method in Data.java, I call the appropriate methods in my worker classes. So far so good.

But lets say I want to go with the Singleton approach instead. I've read a lot of topics in here, and to simplify things I think using Singleton is the best approach for me. I want to keep things simple. Now I know I could make the Data-class a Singleton and the have all my logic directly in this class, both filehandling and locking. I would synchronize on the locks(HashMap) and probably the file too(RAF).

But what if I still want to go with the Facade pattern? Should I then make my worker classes Singletons too or is it enough to just make the Data-class a Singleton? I'm puzzled by this and can't really decide what is right or wrong.

Thanks in advance.
[ November 29, 2007: Message edited by: Allan Smith ]
+Pie Number of slices to send: Send
Hi,

I'm not so sure I understand what your facade would do.. however, I personally believe that you don't need to make your facade singleton..

You facade (I believe) would not keep vital information that requires no one else to have a different set of value..
I think it is different to "only want to instantiate a single instance" and "only 1 instance need to exist at once".

Should your facace have to responsibility to instantiate itself and have the knowledge that it is unique ? The responsability to instantiate it only once can be given to whatever-other-class using it (final for instance..).

On a side note, I started using singleton a lot when building GUI. I don't know if that is good or bad, but I like it For instance, my "bookingView" is a singleton.. The last used value are always there no matter from where is it re-opened (toolbar, menu...).
It is probably bad practices, I don't know..

Regards,
Alex
+Pie Number of slices to send: Send
Hi Alex,

My facade is the Data-class obviously. It should not contain any business logic at all, rather have two worker classes that do this instead.

Data.java
|--- Lockmanager.java(handles locking)
|--- FileManager.java(handles file I/O)

Now, if I go with this approach, and want to make my Data-class a singleton, i.e. there could only exist ONE instance and ONE instance only of my Data-class, should I make the two worker classes singletons too? Or have I completely misunderstood the purpose/usage of the singleton design pattern...

If I instantiate my Data-class with "getInstance" what happens with my two worker classes? How do they get instantiated or how SHOULD I instantiate them?

Thanks
[ November 29, 2007: Message edited by: Allan Smith ]
+Pie Number of slices to send: Send
ohh,

I misunderstood, I thought of a Business facade (business delegate) when replying...

My Data is a singleton too. My code assume there is only one instance opening only one file, and using the lock/unlock strategy..
I guess my singleton simply ensure that when multiple clients will try to connect, it is IMPOSSIBLE that my code provide 2 distinct Data classes.

If your helper classes are instantiated from your Data class, then I would say they don't need to be singleton.

Regards,
Alex
That which doesn't kill us makes us stronger. I think a piece of pie wouldn't kill me. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 877 times.
Similar Threads
B&S Design question
Is accessing static class variables from non-static methods bad practice?
B&S 2.3.2 - Data class as a singleton
Implements DBAcess - URLyBird 1.2.3
Worker classes - where to validate data
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 04:11:44.