2017-01-31 8 views
0

私は次のような状況で最適な抽象化を作成しようとしています。たぶん誰かが助けることができます。異なるタイプの抽象

これは私が現時点で持っていますものです:

public class Point{ 

    public double? Value {get;set;} 

    public Information DataInformation {get;set;} 

} 

public class RawPoint{ 

    //just something to process, not interesting for our example 

} 

public interface Service{ 

    List<Point> ProcessPoints(List<RawPoint> rawData); 

} 

public ConcreteService : Service{ 

    public List<Point> ProcessPoints(List<RawPoint> rawData){ 
     //process the points... 
    } 

}

今、私はポイントの新しいタイプをintoduceする必要が要求を持っている、のようなもの:

public class NewPointData{ 

    public double? Point_Max {get;set;} 

    public double? Point_Min {get;set;} 

} 

public NewPoint { 

    public NewPointData Value { get; set;} 

    public Information DataInformation {get;set;} 

} 

同じProcessPoints()メソッドで前に見たのと同じConcreteServiceを持っていて、リストを返すのではなく、エクステントを返すようにしたいと思います。b yはPointとNewPointの両方です(唯一の違いはValue属性のデータ型です)。 typeof()を使用せずにクライアントを抽象化/多型のdirrect使用のみでこれを達成する方法はありますか?その後

public class Point<TValue> 
{ 
    public TValue Value { get; set; } 
    public Information DataInformation { get; set; } 
} 

、にあなたのサービス・インターフェースを変更します:

+1

クラスの階層を作成するかどうか、またはインターフェイスを導入するかどうかを決定する際には、実際のデータ、動作、およびクラスを使用する方法が重要です。クラス名だけを提供しているのであれば、 –

答えて

2

使用Generics感謝をあなたがメソッドを呼び出すときにジェネリック型引数を提供する必要があります

public interface Service 
{ 
    List<Point<TValue>> ProcessPoints<TValue>(List<RawPoint> rawData); 
} 

var points = _service.ProcessPoints<double?>(data); 
// points is of type List<Point<double?>> 

var newPoints = _service.ProcessPoints<NewPointData>(data); 
// points is of type List<Point<NewPointData>>