Objects by Design Forums
Show 15 posts from this thread on one page

Objects by Design Forums (http://forums.objectsbydesign.com/index.php)
- Object-Oriented Design and Patterns (http://forums.objectsbydesign.com/forumdisplay.php?forumid=14)
-- Enforcing the Singleton Pattern (http://forums.objectsbydesign.com/showthread.php?threadid=1224)


Posted by tausifk on 12-14-2004 09:39 AM:

Enforcing the Singleton Pattern

hi all,

I have a ObjectPooling class (Singleton). I have this ObjectPooling related stuff in a seperate package, say poolmanager package.

Now the situation is, I want to force everyone to use this class to get the object (in this case a connection) to work with. No one should be able to inherit or instantiate it on their own, and every one must get a connection through this class only (no direct connection).

I summarise it as:

1. No one should be able to inharit / instantiate it -->> this I can achieve, as singleton will not allow to instantiate it and to avoid inheritance I can define my class as final. - IS IT FINE?

2. No one should bypass it and get the direct connection / object on their own. -->> how can I avoid this and force them to use the ObjectPooling class to get the connection??

Is there only code review method which can ensure it or can I enforce it programatically as well??

thanks in advance


Posted by lbathija on 12-15-2004 10:21 PM:

hi,
the second part of your question is not clear. As to who the persons(clients) which you want, that they should use your class for getting the connection.

for the first part........
1) Yes, you can do "final class" and it can't be inherited.
2) You can also make the constructor of the class private, and give a static method in the class to get the instance of the class(Basically the singleton pattern with private constructor).

bye,
Lokesh.


Posted by tausifk on 12-16-2004 04:49 AM:

Hi Lokesh,
Thnks for your reply and sorry for not being clear.

SECOND PART: the users are nothing but another module developer in same project. I am just providing the API which they (other module develpors in same project) should make use of.
so I want them to force using my API to get the connection instead of getting connection directly...
example: they must use ObjectPool to get connection and should not get direct connection say DriverManager.getConnection()

FIRST PART : yes, you are right, I have that private constructor.. thats why i told they should not be able to instantiate the class. this constraint i m acheaving thru' private constructor only

waiting for first part answer, and if there is more i left in first part.. please do reply

thnks
Tausif


Posted by lbathija on 12-16-2004 09:00 PM:

hi,
I am still a bit unclear about what exactly you want. Anyway, I am telling you whatever I have understood.

1) If it's a generic thing like getting a connection to a Database, then I don't know how are you going to stop a person from saying DriverManager.getConnection().

2) If it's a thing which you have made, then what you do is implement a Proxy pattern. Do not expose the real class to the client. Instead expose a proxy class which internally creates the object of the required class and forwards the request.

Check the Proxy Pattern. See if it helps you.

bye,
Lokesh


Posted by tausifk on 12-16-2004 09:15 PM:

hi lbathija


Thanks for your message. You got it right on your 1st point. That's what I wanted to ask you, if some one tries to get a connection on generic thing what could be the concern?? I think I can achieve it only with code review??


All times are GMT. The time now is 12:23 PM.
Show 15 posts from this thread on one page

Powered by: vBulletin Version 2.3.5
Copyright © Jelsoft Enterprises Limited 2000 - 2018.
Copyright 1999-2005, Objects by Design, Inc.