1

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?

4

5 回答 5

0

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.

于 2009-06-03T16:22:45.810 回答
0

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.

于 2009-06-03T16:25:37.947 回答
0

您可以像这样在输出类上定义一个隐式运算符 (C#):

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

那么你所要做的就是:

output = input;

并且您将有效地从输入中获取您想要在输出中的所有数据。

于 2009-06-03T16:28:07.980 回答
0

选项 - 单例 - 有效,但取决于重用场景。您是否使用“new MyObj()”以外的任何类型的工厂或实例模式?如果是这样,工厂可以完成这项工作——这会导致下一个方法——注射。您的语言是否支持注入或某种(又名 - 弹簧)。您可以定义一个 setter 方法,但将其留给注入框架和/或工厂方法来处理/执行设置 - 再次取决于用例。

如果它基于运行时,您将需要使用组合 - 委托给原始对象的引用或中间对象(依次包装 A、BC 等)。

可能有点偏题,但我假设您的问题是一个简单的案例,只是为了获得一些针对更大问题的建议。

属性是动态编译时间还是运行时间?他们是否会改变(即处理来自数据库的动态更新)。您是否需要在测试工具或模拟值中运行,然后在完整应用程序的上下文中进行测试?

于 2009-06-03T16:29:14.610 回答
0

在不知道更多的情况下,我认为继承和多态是要走的路:

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

你做的很简单:

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;
}
于 2009-06-03T16:33:01.473 回答