如何通过OOP模式规避双接口实现类的分支判断与多对象冗余?
问题背景
现有两个均继承自IGeneral的接口:
public interface IInterface1 : IGeneral public interface IInterface2 : IGeneral
它们分别由两个具有不同基类的类实现:
public class Class1: BaseClass1, IInterface1 public class Class2: BaseClass2, IInterface2
IInterface1与IInterface2存在部分相同成员和不同成员:
相同成员
Action<byte[], int> OnEvent1 { get; set; } Action<Type1> OnEvent2 { get; set; } public int Method1(); void Method2();
不同成员
// IInterface1 void MethodDifferent2(); // IInterface2 Task MethodDifferent2();
当前程序中需维护两个对象:
private IInterface1 objectType1; private IInterface2 objectType2;
并通过分支逻辑调用Method2:
if (config.test != null) { switch (config.test) { case "Type1": objectType1.Method2(); break; case "Type2": objectType2.Method2(); break; default: break; } } else { objectType2.Method2(); }
目前需要维护两个对象并使用分支判断,请问是否可通过OOP设计模式避免这种情况?
解决方案:通过适配器+抽象工厂消除分支
完全可以用OOP设计模式解决这个问题,核心是统一抽象通用行为,将对象创建与业务调用分离,具体实现步骤如下:
1. 定义统一业务接口
基于两个接口的通用成员,定义一个包含所有通用行为的抽象接口(也可直接扩展IGeneral),若需处理差异化方法,统一其签名:
public interface ICommonService : IGeneral { Action<byte[], int> OnEvent1 { get; set; } Action<Type1> OnEvent2 { get; set; } int Method1(); void Method2(); // 统一差异化方法的异步签名,适配两种实现 Task MethodDifferent2Async(); }
2. 适配器包装现有类
由于Class1和Class2的基类无法修改,用适配器模式将它们包装成实现ICommonService的类,抹平差异:
// 适配Class1,同步方法转异步 public class Class1Adapter : ICommonService { private readonly IInterface1 _innerInstance; public Class1Adapter(IInterface1 inner) { _innerInstance = inner; } public Action<byte[], int> OnEvent1 { get => _innerInstance.OnEvent1; set => _innerInstance.OnEvent1 = value; } public Action<Type1> OnEvent2 { get => _innerInstance.OnEvent2; set => _innerInstance.OnEvent2 = value; } public int Method1() => _innerInstance.Method1(); public void Method2() => _innerInstance.Method2(); public Task MethodDifferent2Async() { _innerInstance.MethodDifferent2(); return Task.CompletedTask; } } // 适配Class2,直接复用原有异步方法 public class Class2Adapter : ICommonService { private readonly IInterface2 _innerInstance; public Class2Adapter(IInterface2 inner) { _innerInstance = inner; } public Action<byte[], int> OnEvent1 { get => _innerInstance.OnEvent1; set => _innerInstance.OnEvent1 = value; } public Action<Type1> OnEvent2 { get => _innerInstance.OnEvent2; set => _innerInstance.OnEvent2 = value; } public int Method1() => _innerInstance.Method1(); public void Method2() => _innerInstance.Method2(); public Task MethodDifferent2Async() => _innerInstance.MethodDifferent2(); }
3. 抽象工厂封装对象创建
把根据配置创建对应适配对象的逻辑集中到工厂类,业务代码不再需要分支判断:
public class ServiceFactory { public static ICommonService CreateService(Config config) { if (config?.test == "Type1") { // 实际场景可替换为依赖注入创建实例 var class1 = new Class1(); return new Class1Adapter(class1); } // 默认返回Type2的适配对象 var class2 = new Class2(); return new Class2Adapter(class2); } }
4. 简化业务调用
现在业务代码只需维护一个ICommonService对象,直接调用方法即可:
// 初始化时通过工厂创建一次 private ICommonService _businessService = ServiceFactory.CreateService(config); // 调用Method2时无需分支,直接执行 _businessService.Method2();
扩展说明
- 后续新增同类接口/类时,只需新增对应的适配器和工厂内的创建逻辑,业务代码无需修改,符合开闭原则。
- 若项目使用依赖注入框架,可将工厂逻辑集成到DI容器中,通过配置直接注入对应
ICommonService实现,进一步简化初始化逻辑。
内容的提问来源于stack exchange,提问作者Danilo7
相关产品推荐
相关产品推荐

