在 C# 中重构抽象类

发布于 2024-08-20 11:27:27 字数 1888 浏览 5 评论 0原文

抱歉,如果这听起来很简单,但我正在寻找一些帮助来改进我的代码:)

所以我目前有以下实现(我也写过):

public interface IOptimizer
{
    void Optimize();
    string OptimizerName { get; }
}

public abstract AbstractOptimizer : IOptimizer
{
    public void Optimize()
    {
        // General implementation here with few calls to abstract methods
    }
}

public abstract AbstractPriorityOptimizer : AbstractOptimizer 
{
     // Optimize according to priority criteria.    

     string Name 
     { 
        get { return "Priority Optimizer"; }
     }      
}

然后我有特定于技术的具体类:

TechnologyXPriorityOptimizer : AbstractPriorityOptimizer 
TechnologyYPriorityOptimizer : AbstractPriorityOptimizer 


现在我正在尝试添加一个通用优化器,它可以针对优先级以外的条件进行优化,所以我的尝试:

public abstract AbstractGenericOptimizer : AbstractOptimizer 
{
     // Optimize according to a generic criteria.    

     private readonly int param;

     public AbstractGenericOptimizer (int param) : base()
     {
          // param affects the optimization
          this.param = param;
     }        
}

并且我还需要特定于技术的实现,就像优先级优化器一样:

TechnologyXGenericOptimizer : AbstractGenericOptimizer 
TechnologyYGenericOptimizer : AbstractGenericOptimizer 


Q1. TechnologyXPriorityOptimizerTechnologyXGenericOptimizer 具有完全相同的“额外”方法,因为它们涉及相同的技术。有没有办法让这个方法对两个继承分支都通用?

Q2。对于 AbstractGenericOptimizer,优化器对于 int param 的特殊值有一个特殊的名称,因此扩展基本通用优化器类(其中 param 是硬编码的)是一个好主意)然后对于每个部门,都有一个特定于技术的实现:

AbstractSpecialName1Optimizer: AbstractGenericOptimizer
TechnologyXSpecialName1Optimizer: AbstractSpecialName1Optimizer
TechnologyYSpecialName1Optimizer: AbstractSpecialName1Optimizer

AbstractSpecialName2Optimizer: AbstractGenericOptimizer
....

重构此场景的最佳方法是什么?我觉得有一种更聪明的方法可以减少继承级别的数量。

谢谢!

Sorry if this sounds simple, but I'm looking for some help to improve my code :)

So I currently have the following implementation (which I also wrote):

public interface IOptimizer
{
    void Optimize();
    string OptimizerName { get; }
}

public abstract AbstractOptimizer : IOptimizer
{
    public void Optimize()
    {
        // General implementation here with few calls to abstract methods
    }
}

public abstract AbstractPriorityOptimizer : AbstractOptimizer 
{
     // Optimize according to priority criteria.    

     string Name 
     { 
        get { return "Priority Optimizer"; }
     }      
}

Then I have technology-specific concrete classes:

TechnologyXPriorityOptimizer : AbstractPriorityOptimizer 
TechnologyYPriorityOptimizer : AbstractPriorityOptimizer 

Now I'm trying to add a generic optimizer, one that optimizes for conditions other than priority, so my attempt:

public abstract AbstractGenericOptimizer : AbstractOptimizer 
{
     // Optimize according to a generic criteria.    

     private readonly int param;

     public AbstractGenericOptimizer (int param) : base()
     {
          // param affects the optimization
          this.param = param;
     }        
}

and I also need technology-specific implementation just like the priority optimizer:

TechnologyXGenericOptimizer : AbstractGenericOptimizer 
TechnologyYGenericOptimizer : AbstractGenericOptimizer 

Q1. TechnologyXPriorityOptimizer and TechnologyXGenericOptimizer have the same exact "extra" methods because they involve the same technology. Is there a way to keep this method common to both inheritance branches ?

Q2. For AbstractGenericOptimizer, the optimizer has a special name for special values of the int param, so would it be a good idea to extend the base generic optimizer class (where param is hardcoded) and then for every division, have a technology-specific implementation:

AbstractSpecialName1Optimizer: AbstractGenericOptimizer
TechnologyXSpecialName1Optimizer: AbstractSpecialName1Optimizer
TechnologyYSpecialName1Optimizer: AbstractSpecialName1Optimizer

AbstractSpecialName2Optimizer: AbstractGenericOptimizer
....

What would be the best way to refactor this scenario ? I feel that there is a smarter way of reducing the number of inheritance levels.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

初懵 2024-08-27 11:27:27

您可能应该使用包含而不是继承。

例如,您可以创建一个抽象 OptimizerStrategy 类,其中包含每种技术的具体实现,然后使 GenericOptimizerPriorityOptimizer 采用 OptimizerStrategy 作为泛型类型参数或构造函数参数。

You should probably use containment instead of inheritance.

For example, you could make an abstract OptimizerStrategy class with concrete implementations for each technology, then make the GenericOptimizer and PriorityOptimizer take an OptimizerStrategy as a generic type argument or constructor parameter.

枫以 2024-08-27 11:27:27

我倾向于说,仅使用继承无法获得所需的设计。您必须采用正交继承路径 - 一侧是优先级和通用,另一侧是技术 X 和 Y。您希望将两个路径中的代码组合成可能的四种组合,但这需要多重继承 - 从优先级或泛型以及技术 X 或 Y 继承。因为 C# 不支持多重继承,所以这不起作用。

我会尝试通过使用接口和策略模式来解决这个问题。这应该允许您将特定于所有四个核心组件的代码提取到单独的类中,然后在四个所需组合中的每个组合中始终合并其中两个。

Ad hoc I tend to say that you cannot get your desired design by using inheritance only. You have to orthogonal inheritance paths - priority and generic on one side and technology X and Y on the other. You want to combine code from both paths in the possible four combinations but this would require multiple inheritance - inheriting from priority or generic and technology X or Y. Because C# does not support multiple inheritance, this will not work.

I would try to solve this by using interfaces and the strategy pattern. This should allow you to extract the code specific to all four core components into separate classes and then merge always two of them in each of the four desired combinations.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文