简体   繁体   English

以下场景要遵循的设计模式

[英]Design Pattern to be followed for the scenario below

I have three different types of setups(a class corresponding to each).我有三种不同类型的设置(对应于每个的类)。 Consider, that we know the setup that has to be followed (say out of 1, 2 and 3 ).考虑一下,我们知道必须遵循的设置(比如1、2 和 3 )。 Now, each setup requires different settings.现在,每个设置都需要不同的设置。

  1. Suppose, we have SettingB, SettingC, SettingD that is common to all the three setups except SettingA .假设,我们有SettingB、SettingC、SettingD ,它们对除SettingA之外的所有三个设置都是通用的 So, what I did was, I have a common interface Settings for all the settings type(A, B and C).所以,我所做的是,对于所有设置类型(A、B 和 C),我都有一个通用的界面设置。

     interface IConfigureSettings { void ConfigureSettings(); }

    Only this method will be exposed for each setting type to configure for each setup.对于每个设置类型,将仅公开此方法以针对每个设置进行配置。

  2. Within SettingA, we have sub settings( SubSettingsAA, SubSettingsAB, SubSettingsAC ).在 SettingA 中,我们有子设置( SubSettingsAA, SubSettingsAB, SubSettingsAC )。
    So for this, I have an abstract class for SettingA which is inherited by SubSettingAA, SubSettingAB, SubSettingAC.因此,为此,我有一个由 SubSettingAA、SubSettingAB、SubSettingAC 继承的 SettingA 抽象类。

     abstract class SettingA : IConfigureSettings { List<SettingA> _subSettings; public void ConfigureSettings() { //Should Perform configuration for the given site. This is what I need. } }
  3. Out of these sub settings both SubSettingsAA and SubSettingsAB may have 2 different configurations (say configurationA(), ConfigurationB() ).在这些子设置中, SubSettingsAA 和 SubSettingsAB 可能有两种不同的配置(比如configurationA(), ConfigurationB() )。 ConfigurationA() is not in SubSettingsA. ConfigurationA() 不在 SubSettingsA 中。 SubSettingsAB class contains both ConfiugrationA() and ConfigurationB(). SubSettingsAB 类包含 ConfigurationA() 和 ConfigurationB()。

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

Now, in case of Setup1 only ConfigurationA() is visible , whereas in case of setup2 and setup3 everything is to be configured.现在,在 Setup1 的情况下,只有 ConfigurationA() 是可见的,而在 setup2 和 setup3 的情况下,一切都需要配置。 Configuration's() in SubSettingAC is to be configured for all the three setups. SubSettingAC 中的 Configuration's() 将针对所有三个设置进行配置。

So, this is what I have.所以,这就是我所拥有的。 Now, what I want is to just call ConfigureSettings() for SettingA and it should configure everything based on the setup type.现在,我想要的只是为 SettingA 调用 ConfigureSettings(),它应该根据设置类型配置所有内容。 Consider, setup type is available.考虑一下,设置类型可用。

Patterns are not the panacea of program design. 模式不是程序设计的灵丹妙药。 They do not replace traditional object-oriented analysis techniques like CRC cards or use-case modeling.它们不会取代传统的面向对象分析技术,如 CRC 卡或用例建模。 To use an architectural analogy, analysis lets you determine that your house needs 200 amps of electricity.使用建筑类比,分析可让您确定您的房屋需要 200 安培的电力。 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.当然,有些问题有两个甚至三个方面,可能需要您混合搭配模式。

From gofpatterns来自gofpatterns

However having said that I think the first part of your questions seems to be creational and the Builder pattern seems to be the closest match.但是,话虽如此,我认为您问题的第一部分似乎是创造性的,而 Builder 模式似乎是最接近的匹配。 I would use composition ( through interfaces) to set the configurations as per the sub settings.我将使用组合(通过接口)根据子设置来设置配置。 Also the classes Configuration A and SubsettingsAC seem to be Base classes for Configuration and Subsetting classes.此外,Configuration A 和 SubsettingsAC 类似乎是 Configuration 和 Subsetting 类的基类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM