473,382 Members | 1,389 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Looking for a pattern

Hello,

I am looking for a good pattern. I have a rather large software app
that makes use of a service manager for its many services...
configuration, colors, data lookup, units, etc. Up until now the
service manager has been a singleton and anyone who wants access to a
service just asks the singleton.

Now we have a new requirement... run multiple instances of the
software in the same application space with different configurations.
<sarcasm>shocking</sarcasm>

So, now I need to think about a good design pattern to help me here.
I can only come up with two awkward options:

1. Pass a service manager key to every constructor of every class that
needs access to the service manager. The class can go to a singleton
to ask for the instance of the service manager by key. This is
awkward and I don't like it.

2. Create an interface for getting a service, and have every object in
the object tree implement the interface. Pass a "parent" object
reference to the "child" and implement the interfaces so they climb
the tree all the way to the root node to get an instance of the
service manager stored in the root node. This is better, but still
awkward.

Is there a better design pattern out there to do what I need? I am
using .NET C#, though it shouldn't matter too much (unless .NET
already has a service I can leverage).

Thanks,
Brian

Oct 31 '07 #1
13 1341
On Wed, 31 Oct 2007 19:30:23 -0000, Bilz <Br**********@gmail.com>
wrote, quoted or indirectly quoted someone who said :
>Now we have a new requirement... run multiple instances of the
software in the same application space with different configurations.
<sarcasm>shocking</sarcasm>
You need some sort of factory to create your service provider. Perhaps
it can cache them, and reuse an existing provider if its set of
configurations have already been used before.

You create a key class that contains the various distinguishing
initialisation parameters, then a hashCode that xors the various
fields. Then use this key class to create index into your HashMap
cache of pre-built providers.
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Oct 31 '07 #2
On Oct 31, 4:32 pm, Roedy Green <see_webs...@mindprod.com.invalid>
wrote:
On Wed, 31 Oct 2007 19:30:23 -0000, Bilz <BrianGeni...@gmail.com>
wrote, quoted or indirectly quoted someone who said :
Now we have a new requirement... run multiple instances of the
software in the same application space with different configurations.
<sarcasm>shocking</sarcasm>

You need some sort of factory to create your service provider. Perhaps
it can cache them, and reuse an existing provider if its set of
configurations have already been used before.

You create a key class that contains the various distinguishing
initialisation parameters, then a hashCode that xors the various
fields. Then use this key class to create index into your HashMap
cache of pre-built providers.
--
Roedy Green Canadian Mind Products
The Java Glossaryhttp://mindprod.com
Ok, that is fine... but how does all of the classes get the key? Do
you pass them in to every constructor, and keep track of the key all
the way down the object graph? This is what I am trying to avoid...
though I can't think of a way how.

Nov 1 '07 #3
Bilz wrote:
Hello,

I am looking for a good pattern. I have a rather large software app
that makes use of a service manager for its many services...
configuration, colors, data lookup, units, etc. Up until now the
service manager has been a singleton and anyone who wants access to a
service just asks the singleton.

Now we have a new requirement... run multiple instances of the
software in the same application space with different configurations.
<sarcasm>shocking</sarcasm>

So, now I need to think about a good design pattern to help me here.
I can only come up with two awkward options:

1. Pass a service manager key to every constructor of every class that
needs access to the service manager. The class can go to a singleton
to ask for the instance of the service manager by key. This is
awkward and I don't like it.

2. Create an interface for getting a service, and have every object in
the object tree implement the interface. Pass a "parent" object
reference to the "child" and implement the interfaces so they climb
the tree all the way to the root node to get an instance of the
service manager stored in the root node. This is better, but still
awkward.

Is there a better design pattern out there to do what I need? I am
using .NET C#, though it shouldn't matter too much (unless .NET
already has a service I can leverage).
I would prefer solution #1 over #2. Much more flexible.

As a long time solution that seems obvious.

For a dirty hack: if the two instances of the software are actually
running in different threads, then you could register each thread to
a given instance of the software and have the singleton create
based on thread.

Arne
Nov 1 '07 #4
Bilz wrote:
Hello,

I am looking for a good pattern. I have a rather large software app
that makes use of a service manager for its many services...
configuration, colors, data lookup, units, etc. Up until now the
service manager has been a singleton and anyone who wants access to a
service just asks the singleton.

Now we have a new requirement... run multiple instances of the
software in the same application space with different configurations.
<sarcasm>shocking</sarcasm>

So, now I need to think about a good design pattern to help me here.
I can only come up with two awkward options:

1. Pass a service manager key to every constructor of every class that
needs access to the service manager. The class can go to a singleton
to ask for the instance of the service manager by key. This is
awkward and I don't like it.
FWIW, that's what I used:
http://www.edmundkirwan.com/servlet/...47.html#doPost

In that method, an access is createed each time the user clicks a button on
a web page, and that access is passed to every object in the system that
needs it.

There is no magic solution.

Yes, it feels awkward.

Yes, it feels as though there, "Should," be a better solution.

There isn't.

--
..ed

www.EdmundKirwan.com - Home of the mathematical laws of encapsulation.
Nov 1 '07 #5
Bilz <Br**********@gmail.comwrote:
I am looking for a good pattern. I have a rather large software app
that makes use of a service manager for its many services...
configuration, colors, data lookup, units, etc. Up until now the
service manager has been a singleton and anyone who wants access to a
service just asks the singleton.

Now we have a new requirement... run multiple instances of the
software in the same application space with different configurations.
<sarcasm>shocking</sarcasm>

So, now I need to think about a good design pattern to help me here.
I can only come up with two awkward options:

1. Pass a service manager key to every constructor of every class that
needs access to the service manager. The class can go to a singleton
to ask for the instance of the service manager by key. This is
awkward and I don't like it.

2. Create an interface for getting a service, and have every object in
the object tree implement the interface. Pass a "parent" object
reference to the "child" and implement the interfaces so they climb
the tree all the way to the root node to get an instance of the
service manager stored in the root node. This is better, but still
awkward.
Just out of curiosity, which method would you have chosen if someone had
told you up front "no globals"? (After all, a singleton is just a global
with a pretty wrapper.)
Nov 1 '07 #6
On Nov 1, 11:17 am, "Daniel T." <danie...@earthlink.netwrote:
Bilz <BrianGeni...@gmail.comwrote:
I am looking for a good pattern. I have a rather large software app
that makes use of a service manager for its many services...
configuration, colors, data lookup, units, etc. Up until now the
service manager has been a singleton and anyone who wants access to a
service just asks the singleton.
Now we have a new requirement... run multiple instances of the
software in the same application space with different configurations.
<sarcasm>shocking</sarcasm>
So, now I need to think about a good design pattern to help me here.
I can only come up with two awkward options:
1. Pass a service manager key to every constructor of every class that
needs access to the service manager. The class can go to a singleton
to ask for the instance of the service manager by key. This is
awkward and I don't like it.
2. Create an interface for getting a service, and have every object in
the object tree implement the interface. Pass a "parent" object
reference to the "child" and implement the interfaces so they climb
the tree all the way to the root node to get an instance of the
service manager stored in the root node. This is better, but still
awkward.

Just out of curiosity, which method would you have chosen if someone had
told you up front "no globals"? (After all, a singleton is just a global
with a pretty wrapper.)
if singletons are globals, then "no globals" is a mere illusion :-(

Nov 1 '07 #7
On 2007-11-01 09:29:57 -0700, Diego <jo********@gmail.comsaid:
>Just out of curiosity, which method would you have chosen if someone had
told you up front "no globals"? (After all, a singleton is just a global
with a pretty wrapper.)

if singletons are globals, then "no globals" is a mere illusion :-(
There's no official definition of a "global", so it's more a matter of
semantics than of illusion.

In the strictest sense, a singleton or other static member is not a
global at all. In a relaxed sense, any static member of any top-level
class is a global, and in an even more relaxed sense any static member
of any class is a global.

Personally, I prefer the strictest definition of "global": an
identifier that is accessible globally, without any decoration at all.
This correlates reasonably well to the usual use of the word "global"
in programming languages.

If you would consider a local static variable in a C++ function to be a
"global", then I'd agree that singletons and other static things in C#
would be "globals" as well. Otherwise, I'd say it's a bit of a stretch
to claim that.

Pete

Nov 1 '07 #8
Lew
Diego wrote:
if singletons are globals, then "no globals" is a mere illusion :-(
I don't know what that means. Static variables, singletons - both are globals
in some sense.

The singleton pattern isn't the magic bullet that so many people think.

--
Lew
Nov 1 '07 #9
Peter Duniho <Np*********@NnOwSlPiAnMk.comwrote:
Diego <jo********@gmail.comsaid:
Daniel T. wrote:
Just out of curiosity, which method would you have chosen if
someone had told you up front "no globals"? (After all, a
singleton is just a global with a pretty wrapper.)
if singletons are globals, then "no globals" is a mere illusion
:-(

There's no official definition of a "global", so it's more a matter
of semantics than of illusion.
The whole point of a singleton is to "Ensure a class only has one
instance, <i>and provide a global point of access to it.</i>" (GoF pg.
127 emphasis added.) Global access, means global IMO.

And the question still stands to the OP, if you had known from the
outset of the new feature, how would you have implemented it?
Nov 1 '07 #10
On 2007-11-01 13:20:33 -0700, "Daniel T." <da******@earthlink.netsaid:
The whole point of a singleton is to "Ensure a class only has one
instance, <i>and provide a global point of access to it.</i>" (GoF pg.
127 emphasis added.) Global access, means global IMO.
I understand that "singleton" has a perhaps a very specific meaning in
the particular book in which (it seems) this whole idea of "design
patterns" was first suggested.

However, I see nothing to prevent someone from implementing a singleton
that is not accessible globally. And I would still call it a singleton.

Furthermore, in C# any singleton itself is not accessible globally; you
need to start with the namespace, and then a particular class name, at
a minimum.

I don't see the point in raising the question of whether globals can
exist in C# anyway, but assuming the question has been raised it seems
silly to insist that the singleton pattern is somehow related to the
question. You have to define "global" first before you can say whether
the singleton pattern exists if and only if globals do, and once you've
defined "global", you don't need to look at the singleton pattern to
decide whether they exist in a language or not.

Pete

Nov 2 '07 #11
On Thu, 01 Nov 2007 01:45:54 -0000, Bilz <Br**********@gmail.com>
wrote, quoted or indirectly quoted someone who said :
>Ok, that is fine... but how does all of the classes get the key? Do
you pass them in to every constructor, and keep track of the key all
the way down the object graph?
you pass your parameters to the factory. It composes the key to see if
it built a suitable config object before, if not it calls the
constructor with the parms and adds it to the pool.

Imagine how you might cache Font bitmaps using family, size, style as
the key.
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Nov 2 '07 #12
Peter Duniho wrote:
On 2007-11-01 13:20:33 -0700, "Daniel T." <da******@earthlink.netsaid:
>The whole point of a singleton is to "Ensure a class only has one
instance, <i>and provide a global point of access to it.</i>" (GoF pg.
127 emphasis added.) Global access, means global IMO.

I understand that "singleton" has a perhaps a very specific meaning in
the particular book in which (it seems) this whole idea of "design
patterns" was first suggested.
They defined it and gave it the name.

It is a well known source.

It would be pure obfuscation to use the same well known term about
something different.
Furthermore, in C# any singleton itself is not accessible globally; you
need to start with the namespace, and then a particular class name, at a
minimum.
That has nothing to do with accessibility.

Arne
Nov 2 '07 #13
On Wed, 31 Oct 2007 19:30:23 -0000, Bilz <Br**********@gmail.com>
wrote:
>Hello,

I am looking for a good pattern. I have a rather large software app
that makes use of a service manager for its many services...
configuration, colors, data lookup, units, etc. Up until now the
service manager has been a singleton and anyone who wants access to a
service just asks the singleton.

Now we have a new requirement... run multiple instances of the
software in the same application space with different configurations.
<sarcasm>shocking</sarcasm>

So, now I need to think about a good design pattern to help me here.
I can only come up with two awkward options:

1. Pass a service manager key to every constructor of every class that
needs access to the service manager. The class can go to a singleton
to ask for the instance of the service manager by key. This is
awkward and I don't like it.

2. Create an interface for getting a service, and have every object in
the object tree implement the interface. Pass a "parent" object
reference to the "child" and implement the interfaces so they climb
the tree all the way to the root node to get an instance of the
service manager stored in the root node. This is better, but still
awkward.

Is there a better design pattern out there to do what I need? I am
using .NET C#, though it shouldn't matter too much (unless .NET
already has a service I can leverage).

Thanks,
Brian

Sounds like you just need to implement versioning in a WCF
application.
----------------
AndyW,
Mercenary Software Developer
Nov 15 '07 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Fabian | last post by:
Hi all there, I have already tried asking for help a couple of days ago. I try to rephrase better my problem: I need to grab a webpage that looks like this: <td width=80 align=center...
4
by: eric | last post by:
Greetings, I am looking for C++ implementations of a pattern similar to the following description. This pattern is a wrapper of sorts (in the general sense, not like a Decorator) for a single...
3
by: Steve | last post by:
Hi, I just know there must be some kind of well-known design pattern here, but I'll be damned if I can find it... Let me explain my situation. I have a hierarchy of classes for a GUI. All...
4
by: Jéjé | last post by:
Hi, I have a file which contain 1 pair of values by line like: Name1=Value1 = I nned to store these pair of values in a sortedlist. So the result expected for the 2 samples lines is: Key ...
3
by: ProvoWallis | last post by:
Hi, I'm looking for a little advice about regular expressions. I want to capture a string of text that falls between an opening squre bracket and a closing square bracket (e.g., "") but I've run...
13
by: Alan Silver | last post by:
Hello, MSDN (amongst other places) is full of helpful advice on ways to do data access, but they all seem geared to wards enterprise applications. Maybe I'm in a minority, but I don't have those...
9
by: vbfoobar | last post by:
Hello I am looking for python code that takes as input a list of strings (most similar, but not necessarily, and rather short: say not longer than 50 chars) and that computes and outputs the...
2
by: Rob | last post by:
I'm looking for a mod 10 script that you know to work well. I have googled and found a few different ones but I would like a 2nd opinion. If you can please link me to a mod 10 script that you have...
0
by: AMDRIT | last post by:
I am looking for better concrete examples, as I am a bit dense, on design patterns that facilitate my goals. I have been out to the code project, planet source code, and microsoft's patterns and...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.