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