By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,016 Members | 2,255 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,016 IT Pros & Developers. It's quick & easy.

pattern to get single instance reference into MANY objects

P: n/a
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
Aug 11 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
sklett wrote:
// 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?

--
Göran Andersson
_____
http://www.guffa.com
Aug 11 '07 #2

P: n/a
sklett wrote:
[...]
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
Aug 11 '07 #3

P: n/a

On Aug 11, 1:43 pm, Göran Andersson <gu...@guffa.comwrote:
_tasks.ForEach(delegate(ITask t){ if(t.Execute() != true) break; } );

Why don't you just send them in the Execute call?
Wouldn't that would turn ITask into something like
IVideoTestProcessTask?

Aug 11 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.