如何防止在更新接口后更新所有实现类

如何防止在更新接口后更新所有实现类

本文介绍了如何防止在更新接口后更新所有实现类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个WPF应用程序,它主要是一个图表应用程序。大约有30个图表。每个图表在数据库中都有自己的表,该数据库包含图表的配置信息。每个图表在应用程序中都有一个类,其中包含与图表相关的配置信息。

We have a WPF application which is primarily a charting application. There are approximately 30 charts. Each chart has its own table in a database which holds configuration information for the chart. Each chart has an class in the app containing configuration information related to the chart.

我们的应用程序中有一个界面( IChartConfiguration ),每个图表配置类都实现了这个界面。它允许应用程序以一致和类似的方式使用任何特定的图表配置。

We have an interface in our app (IChartConfiguration) which each chart configuration class implements. It allows the app to work with any particular chart configuration in a consistent and similar manner.

我们遇到的问题是每次我们需要向接口添加属性时,对于添加新配置属性的新图表,我们还没有处理完毕后,我们必须回到每个图表配置类,它们还没有实现这个新属性(通常是大多数)并添加它。满足接口实现。这不是一个巨大的负担,但在我看来必须有一个更好的方法来解决这个问题。

The 'problem' we have is that each time we need to add a property to the interface, for a new chart that adds a new configuration property we haven't already dealt with, we have to go back to each chart configuration class, that doesn't already have this new property implemented (most of them usually) and add it. to satisfy the interface implementation. This isn't a huge burden, but it seems to me there must be a better way to deal with this.

有没有更好的方法来解决这个问题?

Is there a better way to handle this?

推荐答案

您的问题出在其他地方,您遇到的有问题行为根本不存在问题。 这是接口的预期用途。通过实现您正在声明的接口,此类支持此接口。如果你不会更新所有实现该接口的类,你就不再支持这个合同。

Your problem lies elsewhere, the 'problematic' behavior you are experiencing isn't problematic at all. It is the intended use of interfaces. By implementing an interface you are stating, 'this class supports this interface'. If you wouldn't update all classes that implement that interface you are no longer upholding this contract.

你应该问自己的主要问题是您刚刚添加的配置属性是否适用于其他图表。如果不是,则不应在一个通用界面中指定。

The main question you should be asking yourself is whether the configuration property you just added makes sense for the other charts. If it doesn't it shouldn't be specified in one common interface.

只是一些值得探索的选择:

Just a few alternatives worth exploring:


  • 拥有一个主界面并扩展其他界面。

  • 具有常见实现的抽象类。

  • 划分行为跨多个界面。

我不知道你的图表之间究竟有什么区别,我不能推荐任何一种方法是最好的。

Without knowing exactly what the differences are between your charts, I can't recommend any one approach as being the best.

这篇关于如何防止在更新接口后更新所有实现类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 20:43