views:

105

answers:

5

Hi All, I've a class with few properties A, B, C. I consider this as an input object as I use this in the logic to run some rules based on the values of these properties. Now, after running the rules, I created another class as an output with the same properties and with same values in addition to few other properties specific to the output.

Now, my question is how do I share these properties (A, B, C) among both the input and output classes without having to assign the value from input to the output manually. Can you suggest me an effective design I need to use? Is the abstract class concept comes into picture here? Or is there any other effective way?

A: 

Use an intermediate class that only has the properties and then access the instance of that class from your input and output objects, however in most instances it would be simpler to copy the values from your input to your output.

Toby Allen
A: 

Did you considered to use singleton design pattern? I'm not familiar with specification of your project, but seem like it can help you or at least will give you direction.

Artem Barger
A: 

You could define an implicit operator (C#) on the output class like so:

public static implicit operator OutputClass(InputClass m) 
{
   this.A = m.A;
   this.B = m.B;
   this.C = m.C;
}

Then all you would have to do is:

output = input;

and you would effectively bring over all the data from the input that you want in the output.

Joseph
A: 

Options - singleton - works but depends on reuse scenarios. Are you using any type of factory or instance pattern other than "new MyObj()"? if so the factory can do the work - which leads to the next method - injection. Does your language support injection or some kind (aka - spring). You can define a setter method, but leave it up to the injection framework and or factory methods to handle / do setup - again depends on use cases.

If its runtime based, you will need to use composition - delegate to either a reference to the original objects or an intermediate object (which in turn wraps A, B C etc).

Probably a bit left of field , but i am assuming your question is a simple case just to get some suggestions for a larger problem.

Are the properties to be dynamic - compile time vs runtime ? are they going to change (ie. handle dynamic updates from a db). Do you need to run in a test harness or with mock values and then test in the context of the full application ?

nso1
A: 

Without knowing more, i think inheritance and polimorphism is the way to go:

class input 
{
   public int A { get; set;}
   public int B { get; set;}
   public int C { get; set;}
}

class output : input {
   public int D { get; set;}
   public int E { get; set;}
   public int F { get; set;}
}

The you simple do:

void main()
{
 output tpt = new output();
 tpt.A = 3;
 tpt.B = 2;
 tpt.C = 4;

 tpt = dosomething((input)tpt);
}

public output dosomething(input inpt)
{
 //blablabla
 output tpt = (output)inpt;
 tpt.D = 1;
 tpt.E = 2;
 tpt.F = 3;

 return tpt;
}
Drevak