从方法覆盖的 C# OO 设计问题

标签 c# design-patterns reflection oop overriding

情况:

Assembly 1
________________________             ________________________
| Class A               |           | Class B               |
|-----------------------|           |-----------------------|
| Method someMethod     |---------->| Method otherMethod    |
|                       |           |                       |
|_______________________|           |_______________________|

程序集 1 是其他开发人员可以使用的应用程序。 我们将只给他们 .dll,这样如果我们不更改 api,我们就可以从应用程序发布更新。开发人员无法更改程序集 1 中的框架

方法是虚拟的,因此开发人员可以根据需要重写方法来实现自己的逻辑。

问题是开发人员不能覆盖 B 类中的 otherMethod, 他可以覆盖它,但 A 类将始终调用 B 类的方法,而不是被覆盖的方法。

Assembly 1
________________________             ________________________
| Class A               |           | Class B               |
|-----------------------|           |-----------------------|
| Method someMethod     |----XX---->| Method otherMethod    |
|                       |           |                       |
|_______________________|           |_______________________|
                \                                    |
                 \                                   |
                  \                                  |
Assembly 2         \                                 |
                    \                ________________|_______
                     \               | Class ExtendedB       |
                      \              |-----------------------|
                       \____________>| Method otherMethod    |
                                     |                       |
                                     |_______________________|

程序集 2 引用了程序集 1

部分类不起作用,因为它必须是同一个程序集,并且不能超过 2

这个问题有设计模式吗? 或者是否有其他带有反射的解决方案?

编辑添加了一个代码示例:

/* ASSEMBLY 1 */

namespace Assembly1
{
    public interface IAService
    {
        void TestMethod3();
        void TestMethod4();
    }

    public interface IBService
    {
        void TestMethod1();
        void TestMethod2();
    }

    public class AService : IAService
    {
        // Base implementation of AService
        public  virtual void TestMethod3()
        {
            //do something
        }
        public virtual void TestMethod4()
        {
            //do something
        }
    }

    public class BService : IBService
    {
        // Base implementation of BService
        public virtual void TestMethod1()
        {
            //do something
        }
        public virtual void TestMethod2()
        {
            //need to call AService implementation from assembly 2
        }
    }
}   





/* ASSEMBLY 2 */
namespace Assembly2
{
    public class NewAService : AService
    {
        public override void TestMethod3()
        {
            //default implementation which could be overridden
            base.TestMethod3();
        }

        public override void TestMethod4()
        {
            //default implementation which could be overridden
            //An implementation of IBService Should be called

            base.TestMethod4();
        }
    }
}

最佳答案

你应该重构

public interface IClassB
{
   void SomeMethod();
}
public Class A
{
    private IClassB myInstanceB = new ClassB();

    public ClassA(){}

    public ClassA(IClass B)
    {
      myInstanceB = B;
    }

    public void SomeMethod()
    {
        myInstanceB.SomeMethod();
    }
}

public ClassB : IClassB
{
   public void SomeMethod()
   {
      // some wicked code here...
   }
}

完成此重构后,开发人员可以通过使用空构造函数来使用默认实现。如果他们需要一些其他逻辑,而不是只需要实现接口(interface) IClassB 并将其传递给其他构造函数。

程序集 2 中的用法是这样的

public class NewFunctionalityClass : IClassB
{
    public void SomeMethod()
    {
       //something else
    }
}
public TestClass()
{
   public void ShowMethod()
   {
      var defaultObject = new ClassA();
      defaultObject.SomeMethod();  // default implementation

     var otherObject = new ClassA(new NewFunctionalityClass());
     otherObject.SomeMethod(); // here the new implementation will run
   }
}

关于从方法覆盖的 C# OO 设计问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3468578/

相关文章:

javascript - Node.js 中的工厂模式(express)

c# - 几乎相同方法中的不同行为异步/等待

design-patterns - 企业应用架构模式 - 试题?

java - "view"不可变对象(immutable对象)特殊情况的最佳模式

java - 使用反射获取具有原始类型参数的类方法

c# - 获取通用抽象类的属性名

Scala:获取对象类名称的正确方法?

c# - 基于ANTLR v3的成熟编译器

c# - 发布版本中抽象页面的问题

c# - 对 C# .NET 4.0 上 Powershell 的 System.Management 的引用不起作用