【问题标题】:name of design pattern for below approach以下方法的设计模式名称
【发布时间】:2015-08-05 04:34:43
【问题描述】:

我有开源代码,我的目标是在不破坏开源结构的情况下向该代码添加一些功能。 例如

class OpensourceClass{
       String getValue(){
         return "";
       }
   }

所以我创建了一个接口 int 类,并在开源类中引入了该接口的一个实例

 class OpensourceClass{
       public static interface userImpl{
          String getValue();
       }

        private static userImpl obj;

      public static  void setUserImpl(userImpl ob){
          obj=ob;
      }
       String getValue(){
           if(userImpl)
              return userImpl.getValue();
             return "";
      }
    }

只是想知道这是哪种设计模式.. 是一种策略模式吗?

【问题讨论】:

    标签: design-patterns


    【解决方案1】:

    这是Strategy (Policy) 设计模式。根据上下文,它也可以称为State 模式。这两种模式非常相似,您可以查看此问题了解更多详情:What is the difference between Strategy Design pattern and State Design pattern?

    更广泛的术语是Inversion of control。使用策略设计模式是实现控制反转的基本技术之一。

    此外,通过尝试使您的开源类可扩展,您可以遵循Open/closed principle。正如this 文章中所述,策略模式是实现 Open/Closed 的方法之一。

    【讨论】:

      【解决方案2】:

      是的。是Strategy 模式。

      【讨论】:

        猜你喜欢
        • 2011-01-13
        • 2015-07-26
        • 2014-06-22
        • 1970-01-01
        • 1970-01-01
        • 2014-05-29
        • 2016-01-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多