Consider the following code example:
interface IData {
int Count();
}
interface IOperations {
IData Foo();
double Bar(IData a);
}
class Data1 : IData {
public int Count() { return 37; }
public double SomethingElse { get; set; }
}
class Ops1 : IOperations
{
public Data1 Foo() { return new Data1(); }
public double Bar(Data1 x) { ... }
}
Ops1 a = new Ops1();
Data1 data = a.Foo();
double x = a.Bar(data);
I could, of course, just use public IData Foo() { return new Data1(); }:
Ops1 a = new Ops1();
Data1 data = a.Foo() as Data1;
but with aseverywhere, the code quickly becomes confusing.
I wonder if there is a good design template to achieve this in a clear and powerful way?
Edit: It is important that operating systems and data have a common base class:
List<IOperations> ops = ...;
List<IData> data = ...;
List<double> result = ...;
for(int i=0; i<ops.Count; i++)
result[i] = ops[i].Bar(data[i]);
So, for the case of the return type, I am surprised that this is forbidden because I meet the requirements of the interface. In the case of parameters, some additional (template) layer may be required.
source
share