Objects by Design Forums Here you can view your subscribed threads, work with private messages and edit your profile and preferences Registration is free! Calendar Find other members Frequently Asked Questions Search Home  
Objects by Design Forums : Powered by vBulletin version 2.3.5 Objects by Design Forums > Main Forums > Object-Oriented Design and Patterns > Enforcing the Singleton Pattern
  Last Thread   Next Thread
Author
Thread Post New Thread    Post A Reply
tausifk
Member

Registered: Nov 2004
Location:
Posts: 30

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

Report this post to a moderator | IP: Logged

Old Post 12-14-2004 09:39 AM
tausifk is offline Click Here to See the Profile for tausifk Click here to Send tausifk a Private Message Find more posts by tausifk Add tausifk to your buddy list Edit/Delete Message Reply w/Quote
lbathija
Junior Member

Registered: Nov 2004
Location: India
Posts: 2

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.

Report this post to a moderator | IP: Logged

Old Post 12-15-2004 10:21 PM
lbathija is offline Click Here to See the Profile for lbathija Click here to Send lbathija a Private Message Find more posts by lbathija Add lbathija to your buddy list Edit/Delete Message Reply w/Quote
tausifk
Member

Registered: Nov 2004
Location:
Posts: 30

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

Report this post to a moderator | IP: Logged

Old Post 12-16-2004 04:49 AM
tausifk is offline Click Here to See the Profile for tausifk Click here to Send tausifk a Private Message Find more posts by tausifk Add tausifk to your buddy list Edit/Delete Message Reply w/Quote
lbathija
Junior Member

Registered: Nov 2004
Location: India
Posts: 2

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

Report this post to a moderator | IP: Logged

Old Post 12-16-2004 09:00 PM
lbathija is offline Click Here to See the Profile for lbathija Click here to Send lbathija a Private Message Find more posts by lbathija Add lbathija to your buddy list Edit/Delete Message Reply w/Quote
tausifk
Member

Registered: Nov 2004
Location:
Posts: 30

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??

Report this post to a moderator | IP: Logged

Old Post 12-16-2004 09:15 PM
tausifk is offline Click Here to See the Profile for tausifk Click here to Send tausifk a Private Message Find more posts by tausifk Add tausifk to your buddy list Edit/Delete Message Reply w/Quote
All times are GMT. The time now is 06:47 PM. Post New Thread    Post A Reply
  Last Thread   Next Thread
Show Printable Version | Email this Page | Subscribe to this Thread

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 

< Contact Us - Objects by Design >

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