pattern to get single instance reference into MANY objects

S

sklett

I suspect the answer might be in one of the words of my subject, but here
goes anyway.

I'm working on a system that will execute a very long (300+) chain of task
objects. Here is some quick pseudo code to illustrate:

public class VideoAcquisition
{
public Image GetFrame(){};
// other stuff
}

internal interface ITask
{
bool Execute();
}

internal class DeviceInputTask : ITask
{
internal bool Execute()
{
// do stuff that requires a single instance reference of
VideoAcquisition
// do stuff that requires a single instance reference of
TestProcessManager
}
}

internal class VerifyDisplayTask : ITask
{
internal bool Execute()
{
// do stuff that requires a single instance reference of
VideoAcquisition
// do stuff that requires a single instance reference of
TestProcessManager
}
}

internal class TestProcessManager
{
private List<ITask> _tasks = new List<ITask>();

// properties, etc.
}

// at some point I add many, many instance of various types of objects that
implement ITask
// I then call them all sequentially
_tasks.ForEach(delegate(ITask t){ if(t.Execute() != true) break; } );

And finally to my question: Many of my ITask objects need a reference to a
single instance of TestProcessManager and VideoAcquisition. I can think of
several ways to accomplish this:
1) Make TestProcessManager and VideoAcquisition singletons
2) Make them static
3) Inject a reference to each when adding each ITask instance to the list.
Basically implement a method in TestProcessManager like Add(ITask) then set
a reference to the instances. sorta like:
void Add(ITask task)
{
task.VideoAcquisition = this._videoAqcuisition; // ITask doesn't
include these properties yet
task.TestProcessManager = this; // ITask doesn't include these
properties yet

this._tasks.Add(task);
}

The problem that I'm having is choosing a pattern (if these are even
patterns - I'm new to patterns) that will fit my needs. I know you are
probably thinking... "we need more info" - but there isn't a whole lot more.
VideoAcquisition will interface with video hardware (shocking!) and all
ITask objects will be contained in at least a single TestProcessManager.

I'm not sure if there is a pattern name for my setup I have here. At first
it reminded me of Chain of Responsibility, but indeed it is not at all like
that pattern. I'm sure that what I have done (assuming it's not a terrible
design) has been done before and maybe there is a name for it?? I'm
basically encapsulating dissimilar yet related functionality into different
ITask objects. I have DeviceInputTask, DisplayVerificationTask,
WaitForChangeTask, CaptureDisplayExpirationTask, etc, etc.

Anyway, to summarize... I have all these little objects that need to have
access to a couple of single instances and I'm not sure what the best
approach is. Any input is very welcome and appreciated.

Have a great weekend,
Steve
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

sklett said:
// at some point I add many, many instance of various types of objects that
implement ITask
// I then call them all sequentially
_tasks.ForEach(delegate(ITask t){ if(t.Execute() != true) break; } );

And finally to my question: Many of my ITask objects need a reference to a
single instance of TestProcessManager and VideoAcquisition. I can think of
several ways to accomplish this:
1) Make TestProcessManager and VideoAcquisition singletons
2) Make them static
3) Inject a reference to each when adding each ITask instance to the list.
Basically implement a method in TestProcessManager like Add(ITask) then set
a reference to the instances.

Why don't you just send them in the Execute call?
 
P

Peter Duniho

sklett said:
[...]
And finally to my question: Many of my ITask objects need a reference to a
single instance of TestProcessManager and VideoAcquisition. I can think of
several ways to accomplish this:
1) Make TestProcessManager and VideoAcquisition singletons

Assuming that the instances truly are singletons, or if they are not but
you will only ever have once instance and it's not a problem for the
instance to change after you've queued an ITask, it seems to me that
using the singleton pattern is fine. Appropriate, even.

If there is something more complicated about the scenario than above,
you should probably clarify that. I didn't see anything in your
question that would suggest any of the other solutions you posted would
be somehow better than just using a singleton.

Pete
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

UL-Tomten said:
Wouldn't that would turn ITask into something like
IVideoTestProcessTask?

In that case it already is. How you provide the method with the
information that it needs doesn't change what the method does.

Both implementations of Execute uses both a VideoAcquisition object and
a TestProcessManager object, so both have to exist in order to call
ITask.Execute.

All the alternatives that the OP lists seems to be aimed at avoiding to
send the object in the call, when that is actually the most straight
forward solition.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top