Reputation: 27605
I have a class which creates a "data-holder" object, and then modify this object with some modifier classes, similar to this:
public class Process {
public void Run() {
var dataHolder = new DataHolder();
var firstModification = new FirstModification(dataHolder);
firstModification.Run();
var secondModification = new SecondModification(dataHolder);
secondModification.Run();
//etc.
}
}
public class FirstModification {
DataHolder data_holder;
public FirstModification (DataHolder dh) {
data_holder = dh;
}
public void Run() {
// do something with data_holder
}
}
public class SecondModification {
// etc.
}
In this code, each modification constructor must receive dataHolder
as a parameter, with corresponding boilerplate, duplicated code in the modifier classes.
So I would like, if possible and/or recommended, that each modificator object would "already know" about the existence of the living dataHolder
object inside Process.Run()
method (its "parent scope", so to say), with no need to pass it as a parameter to the modifier constructors.
EDIT: I am trying to implement the Pipeline (aka Pipes and Filters) design-pattern, inspired in what is described here and here.
Thanks for any help!
Upvotes: 0
Views: 1912
Reputation: 6416
Use the ref
keyword for your parameter.
...
var firstModification = new FirstModification(ref dataHolder);
...
public FirstModification (ref dataHolder dh) {
// Make changes to dh
}
...
This will pass a reference to the dataHolder object from the Process class to your FirstModification method.
Upvotes: 0
Reputation: 4737
How about superclassing the Modifier classes and making the DataHolder object available from the Process class as a static variable?
public class SuperModifierClass
{
DataHolder dataHolder;
public SuperModifierClass()
{
dataHolder = Process.DataHolder;
}
}
public class FirstModifier
{
public FirstModifier() : base() // Is base called implicitly by default? I forgot...
{
}
}
Another option would be to look at inversion of control through frameworks such as MEF and Unity (google those terms). What these allow you to do in essence is to register objects which can be automatically loaded when instantiating new classes that have them as a parameter. You're going to say "well, if someone ever needs a DataHolder object, use this one" When you instantiate the modification classes through such frameworks, the 'container' as it's called actually resolves the parameter by asking if it's been registered and taking the registered instance.
Upvotes: 0
Reputation: 150148
You have to somehow associate the instances
var firstModification = new FirstModification()
etc. with the actual data.
The current approach is not a bad one. If you want your modification instances to have access to "parent scope", then you would have to pass the parent object reference into them. That actually provides them with more access to the parent object than they actually need, which I would discourage.
Upvotes: 1
Reputation: 1502446
So I would like, if possible and/or recommended, that each modificator object would "already know" about the existence of the living dataHolder object inside Process.Run() method (its "parent scope", so to say), with no need to pass it as a parameter to the modifier constructors.
Possible? Yes, via a thread-local variable to maintain "the current DataHolder
for this thread" (or just a simple static variable, even).
Recommended? No, I wouldn't say so. I see nothing wrong with you've got at the moment - what advantage do you think you would gain from making everything implicit?
There's nothing which allows you to go back up the stack and find local variables in the calling method though...
Upvotes: 3