c# - 以下场景应遵循的设计模式

标签 c# .net oop design-patterns dependency-injection

我有三种不同类型的设置(每种设置对应一个类)。考虑一下,我们知道必须遵循的设置(比如 1、2 和 3)。现在,每个设置都需要不同的设置。

  1. 假设,我们有 SettingB、SettingC、SettingD,这对于除 SettingA 之外的所有三个设置都是通用的。所以,我所做的是,我为所有设置类型(A、B 和 C)设置了一个通用界面设置。

    interface IConfigureSettings
    {
      void ConfigureSettings();
    }
    

    对于每个设置类型,只有此方法才会公开以配置每个设置。

  2. 在 SettingA 中,我们有子设置(SubSettingsAA、SubSettingsAB、SubSettingsAC)。
    因此,为此,我有一个 SettingA 的抽象类,它由 SubSettingAA、SubSettingAB、SubSettingAC 继承。

    abstract class SettingA : IConfigureSettings
    {
      List<SettingA> _subSettings;
      public void ConfigureSettings()
      {
            //Should Perform configuration for the given site. This is what I need.
      }
    } 
    
  3. 在这些子设置中,SubSettingsAA 和 SubSettingsAB 可能有 2 种不同的配置(例如configurationA()、ConfigurationB())。 ConfigurationA() 不在 SubSettingsA 中。 SubSettingsAB 类包含 ConfiugrationA() 和 ConfigurationB()。

    class SubSettingsAA :  SettingA
    {
      ConfigurationB();
    }
    
    class SubSettingsAB :  SettingA
    {
      ConfigurationA();
      ConfigurationB();
    }
    
    class SubSettingAC : SettingA
    {
      ConfigurationC();
      ConfigurationD();
    }
    

现在,在 Setup1 的情况下,只有 ConfigurationA() 是可见的,而在 setup2 和 setup3 的情况下,一切都将被配置。 SubSettingAC 中的 Configuration's() 将针对所有三种设置进行配置。

所以,这就是我所拥有的。现在,我想要的只是为 SettingA 调用 ConfigureSettings() ,它应该根据设置类型配置所有内容。考虑一下,设置类型可用。

最佳答案

Patterns are not the panacea of program design. They do not replace traditional object-oriented analysis techniques like CRC cards or use-case modeling. To use an architectural analogy, analysis lets you determine that your house needs 200 amps of electricity. Patterns let you determine how the wiring will be installed. Patterns do help you think about the problems you may encounter while designing a system. Therefore, a generic, pattern-based solution is often more robust than a solution designed by one individual to solve a specific problem. Given the number of design patterns in common use (as well as many more being invented and discovered almost daily) it can sometimes be hard to choose the pattern that suits your needs. The first thing you should decide is whether the problem is fundamentally creational, structural, or behavioral. Some problems, of course, have aspects of two or even three, and may require you to mix and match patterns.

来自 gofpatterns

不过话虽如此,我认为您问题的第一部分似乎是创造性的,而 Builder 模式似乎是最接近的匹配项。我会使用组合(通过接口(interface))根据子设置来设置配置。此外,类 Configuration A 和 SubsettingsAC 似乎是 Configuration 和 Subsetting 类的基类。

关于c# - 以下场景应遵循的设计模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24137811/

相关文章:

c# - 在 Visual Studio 扩展中打开标准对话框

c# - 如何让我的 ServiceStack 测试使用 RestSharp 进行身份验证?

c# - SmtpClient.Timeout 属性是否适用于异步调用?

java - 非抽象且不重写抽象方法 - IDL

javascript - 在其原型(prototype)上定义对象属性的理由是什么?

c# - 通用 .NET 类中的 public static (const)

c# - Kinect NullReferenceException 错误

sql - 一个数据库用于一个客户或一个用于所有客户

c# - 计时器如何在 .NET 中工作?

c++ - 如何从 C++ 类中的排序调用比较器函数