C#中的互斥抽象方法

时间:2016-05-17 16:07:00

标签: c#

我正在开发一个C#库,用户需要编写他/她自己的类,从库中继承给定的类。我不认为编写更多细节会有所帮助,所以即使我要求的内容看起来很奇怪,也请考虑它。

在一个类中,我想要以下行为:两个互斥的“abstract”方法,如果实现了一个,那么就不需要实现另一个(所以正确,它们不是真正的{{ 1}})。

我需要强制用户至少实现其中一种方法,因此仅仅声明两种方法abstract是不够的。实际上我可以声明两个virtual,但这意味着用户应该实现一个永远不会被调用的方法,当然我想避免这种情况。

是否有一个技巧或C#成语来做一些接近我想要的事情?也许有一些反思技巧,我几乎一无所知?

3 个答案:

答案 0 :(得分:7)

我认为你要做的是违反了许多面向对象的设计目标。

  

“我需要强制用户实现这些方法中的至少一种”

如果这两个类需要具有一个或另一个的功能,为什么不只是有一个抽象方法(或创建一个接口),并让两个类以不同方式覆盖该方法?然后强制每个类实现一部分功能,另一个实现另一个类。

我建议重新考虑你的方法,而不是花费大量的时间来尝试做出糟糕的方法。

编辑: 根据您的意见,我将尝试详细介绍。

您可以尝试以下内容。但我怀疑你需要大幅扩展才能让它发挥作用。但这无论如何都应该让你开始

public class ResultFromMethod1 {
    public bool optimized = false;
    // other results here
}

这将存储方法1的结果,并告诉您如何运行方法1。

public interface IInterfaceForMethod1 {
    ResultFromMethod1 Method1 ();
}

public interface IInterfaceForMethod2 {
    void Method2 (ResultFromMethod1 resultFromMethod1, Vector v);
}

这些是两种方法的接口。请注意,它们尚未实现。这只是实现它们的类的合同。

public class UnoptomizedImplementation : IInterfaceForMethod1, IInterfaceForMethod2 {
    #region IInterfaceForMethod1 implementation
    public ResultFromMethod1 Method1 () {
        ResultFromMethod1 resultFromMethod1 = new ResultFromMethod1 ();
        resultFromMethod1.optimized = false;
        // Method1 logic here
        return resultFromMethod1;
    }
    #endregion

    #region IInterfaceForMethod2 implementation
    public void Method2 (ResultFromMethod1 resultFromMethod1, Vector v) {
        if (!ResultFromMethod1.optimized) {
            //if NOT optimized
            //logic here
        }
        else {
            //throw exception
        }
    }
    #endregion
}

这些类运行method1未优化,然后有一个方法2,要求方法1不优化。如果在未经过优化时不需要method2,那么就不要实现method2接口。

public  class OptimizedImplementation : IInterfaceForMethod1, IInterfaceForMethod2 {
    #region IInterfaceForMethod1 implementation
    public ResultFromMethod1 Method1 () {
        ResultFromMethod1 resultFromMethod1 = new ResultFromMethod1 ();
        resultFromMethod1.optimized = true;
        // Method2 logic here
        return resultFromMethod1;
    }
    #endregion

    #region IInterfaceForMethod2 implementation
    public void Method2 (ResultFromMethod1 resultFromMethod1, Vector v) {
        if (ResultFromMethod1.optimized) {
            //if optimized
            //logic here
        }
        else {
            //throw exception
        }
    }
    #endregion
}

此类需要优化的method1的输出,否则会抛出异常。

我希望这会让你失去一条更易于管理的轨道。

答案 1 :(得分:2)

我建议实现接口

  public interface IMyMethod1 {
    void MyMethod1();
  }

  public interface IMyMethod2 {
    void MyMethod2();
  }

并注入依赖

  public class MyClass {
    ... 
    public MyClass(IMyMethod1 method1, IMyMethod2 method2) {
      if ((null == method1) && (null == method2))
        throw new ArgumentNullException("method1", 
          "You should provide either method1 or method2");

      m_Method1 = method1;
      m_Method2 = method2;
    }

    ...

    public void DoSomething() {
      ... 
      if (m_Method1 != null)
        m_Method1.MyMethod1(); 
      else if (m_Method2 != null)
        m_Method2.MyMethod2(); 
      ...
    }
  }

答案 2 :(得分:0)

为避免反射,请在没有这两种方法的情况下创建基类(抽象)。 然后,为两个"特殊"创建单独的类(抽象)继承您的基类。方法

这需要进行一些类型检查和转换,但这是我现在所做的全部。