【问题标题】:What design pattern do I use for this?我为此使用什么设计模式?
【发布时间】:2011-12-27 23:23:38
【问题描述】:

假设我正在为这个场景建模——我可以使用什么设计模式来最好地建模这个场景?

基类是 CellPhonePlan。 CellPhonePlan 有两个属性:

  • int MonthlyPrice
  • 字符串类型列表,StandardFeatures

其中 StandardFeatures 可能包含诸如“200 分钟通话时间”之类的值。

我还想为标准 CellPhonePlan 提供一些插件。比如

1) 家庭计划

  • int 价格
  • 字符串类型列表,功能

2) 周末计划

  • int 价格
  • 字符串类型列表,功能

我希望能够选择 StandardFeatures、FamilyPlan 和 WeekendPlan,并使其价格和功能反映我所做的选择。我也想知道如何使用设计模式最好地表示这一点!

谢谢


对不起,我想我没有解释得太清楚。我所追求的是基本计划加上家庭,再加上周末。所以所有的值加起来。

【问题讨论】:

    标签: design-patterns


    【解决方案1】:

    不需要设计模式...

    class CellPhonePlan
    {
        int MonthlyPrice { get; set; }
        List<string> Features { get; set; }
    }
    
    var standardPlan = new CellPhonePlan
    {
        MonthlyPrice = 10,
        Features = new List<string>() { "200 minutes call time", "texting" }
    };
    
    var familyPlan = new CellPhonePlan
    {
      MonthlyPrice = 20,
      Features = new List<string>() { "500 minutes call time", "texting", "shared between a family" }
    };
    
    var weekendPlan = new CellPhonePlan
    {
      MonthlyPrice = 5,
      Features = new List<string>() { "200 minutes call time", "but you can only talk on weekends" }
    };
    

    【讨论】:

      【解决方案2】:

      如果您真的想使用设计模式,这看起来很适合 Decorator pattern

      【讨论】:

      • 这太完美了,谢谢!我现在将尝试弄清楚如何将其标记为答案。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-01-20
      • 1970-01-01
      • 2017-07-15
      • 1970-01-01
      • 2010-10-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多