2017-11-17 2 views
0

私は抽象クラスを持っています。このクラスには、基本クラスから継承するクラスのインスタンスを返すメソッドと、インターフェイスを実装するメソッドが必要です。特定の型を返すようにオーバーライドするメソッドを制限する

public abstract class AbstractClass 
{ 
    abstract [DonotKnowTheType] GetClassInstance() //The Return type should be an instance of a class which implements TestClass and ISample 
} 

public class ChildClass : AbstractClass 
{ 
    override [DonotKnowTheType] GetClassInstance() 
    { 
     //Need to return instance of SampleClass in this example. This could vary, this should be an instance of a class which implements TestClass and ISample 
    } 
} 

public class SampleClass : TestClass,ISample 
{ 
    //Implementation 
} 

良いデザインでこれを達成するのを手伝ってください。 ChildClassのオーバーライドメソッドを記述する開発者が、TestClassとISampleを実装するクラスのインスタンスのみを返すように制限する必要があります。そうでない場合は、コンパイル時エラーを表示する必要があります。

+0

どのような方法がありますがこれを達成するの? – Azeeb

+0

正直なところ、これは理由がない、これはちょうど悪いAPIデザインです。 –

+0

@FilipCordas私はそのAPIが常に悪くないわけではないと思っています。例えば、流暢なAPIデザインは、このような機能に大きな利益をもたらすことができます。 –

答えて

1

この試してみてください:あなたのコメントの後

public abstract class TheEnforcer<T> where T: TestClass, IMyInterface 
    { 
     protected abstract T GetClassInstance(); 
    } 

    public class ThePoorClass : TheEnforcer<DerivedTestClass> 
    { 
     protected override DerivedTestClass GetClassInstance() 
     { 
      throw new NotImplementedException(); 
     } 
    } 

    public class TestClass 
    { 

    } 

    public class DerivedTestClass : TestClass, IMyInterface 
    { 

    } 

    public interface IMyInterface 
    { 

    } 

namespace First { 
    public abstract class TheEnforcer<T> where T : IMarkerInterface 
    { 
     protected abstract T GetClassInstance(); 
    }  

    public interface IMarkerInterface 
    { 

    } } 

namespace Second { 
    using First; 

    // All this is in separate name space 
    public class TestClass: IMarkerInterface 
    { 

    } 

    public class DerivedTestClass : TestClass, IMyInterface 
    { 

    } 

    public interface IMyInterface 
    { 

    } 

    public class ThePoorClass : TheEnforcer<DerivedTestClass> 
    { 
     protected override DerivedTestClass GetClassInstance() 
     { 
      throw new NotImplementedException(); 
     } 
    } } 
+0

TheEnforcerクラスが存在するレイヤーにTestClassが表示されません。 – Azeeb

+0

2番目の方法を参照してください。私は空のインターフェイスだけであるマーカーインターフェイスを使用しています。これが解決することを願っています。正直なところ、このようなことをする必要がある場合。私はあなたが手にしていない悪いオブジェクト指向設計を持っていると思います - まず固定する必要があります。 –

1

あなたがTestClassISamplecontraintであなたの抽象クラスは、一般的な作ることができます:

public abstract class AbstractClass<T> where T: TestClass, ISample 
{ 
    public abstract T GetClassInstance(); //The Return type should be an instance of a class which implements AbstractClass and ISample 
} 

public class ChildClass : AbstractClass<SampleClass> 
{ 
    public override SampleClass GetClassInstance() 
    { 
     //Need to return instance of SampleClass in this example. This could vary, this should be an instance of a class which implements AbstractClass and ISample 
     return new SampleClass(); 
    } 
} 
+0

これにより、SampleClassがISampleを実装するように強制しますが、それもbaseTestClassから継承していることを確認する必要があります。 – Azeeb

+0

@Azeeb:見落としています。 –

+0

@RandRandom:TestClassがタイプミス –

関連する問題