www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - singleton with "momento"

reply "Frustrated" <c1514843 drdrb.com> writes:
Lets suppose I have setup some code to use a singleton object. 
Now lets suppose I want to duplicate that code(say to run 
multiple times simultaneously).

The singleton pattern itself prevents multiple copies. One would 
need multiple instances to be able to run multiple times BUT in 
the context of each piece of code the object would be a singleton.

It seems one would need a singleton would allow in some cases to 
not be a singleton. While I'm sure there are some ways around 
this using a singleton directly I wonder if there is any modified 
pattern to handle this situation?

Basically if one thought of having a signal universe with an 
object in it that is a singleton, then decided to end up with 
multiple universes. In this case there would be copies of the 
singleton pattern but with regard to each universe they should 
behave as expected... and each universe is completely separated 
from all the others).

Anyone know how to easily deal with this?

In my code essentially I have a "universal context"(a singleton) 
that contains some universal objects(singletons). The context is 
just a container of all the important data that user code needs 
quick access too.

I know at some point I'll need to have multiple independent 
contexts to allow for some advanced processing. Hence I can't 
have them as singletons but I do want some level of uniqueness(No 
copies floating around in the universe).

(instead of having to save, change, then restore the context for 
every context switch)

I'm thinking of something like singleton!(class, universe) where 
universe is an id but I'm not sure if that is quite right.
Jan 09 2014
parent "FreeSlave" <freeslave93 gmail.com> writes:
On Thursday, 9 January 2014 at 21:51:46 UTC, Frustrated wrote:
 Lets suppose I have setup some code to use a singleton object. 
 Now lets suppose I want to duplicate that code(say to run 
 multiple times simultaneously).

 The singleton pattern itself prevents multiple copies. One 
 would need multiple instances to be able to run multiple times 
 BUT in the context of each piece of code the object would be a 
 singleton.

 It seems one would need a singleton would allow in some cases 
 to not be a singleton. While I'm sure there are some ways 
 around this using a singleton directly I wonder if there is any 
 modified pattern to handle this situation?

 Basically if one thought of having a signal universe with an 
 object in it that is a singleton, then decided to end up with 
 multiple universes. In this case there would be copies of the 
 singleton pattern but with regard to each universe they should 
 behave as expected... and each universe is completely separated 
 from all the others).

 Anyone know how to easily deal with this?

 In my code essentially I have a "universal context"(a 
 singleton) that contains some universal objects(singletons). 
 The context is just a container of all the important data that 
 user code needs quick access too.

 I know at some point I'll need to have multiple independent 
 contexts to allow for some advanced processing. Hence I can't 
 have them as singletons but I do want some level of 
 uniqueness(No copies floating around in the universe).

 (instead of having to save, change, then restore the context 
 for every context switch)

 I'm thinking of something like singleton!(class, universe) 
 where universe is an id but I'm not sure if that is quite right.
The requirement of uniqueness is often synthetic. I don't deeply understand your situation, but maybe my suggestion would help. Create one object for each context. Each context will contain its own "singleton". (or vise versa: create your "singletons" and make them to contain context). So you can hold context and corresponding singleton together. But in this case you can use your singletons only in member functions of corresponding instance. Static and global functions still have no "singleton" that belongs to them, so you probably will need to pass context to them. Also do you mean "Memento"?
Jan 10 2014