我有开源代码,我的目标是在不破坏开源结构的情况下为这段代码添加一些功能。 例如
class OpensourceClass{
String getValue(){
return "";
}
}
所以我创建了一个接口int类,并在opensource类中引入了该接口的实例
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 "";
}
}
只想知道这个设计模式.. 这是一种策略模式。
答案 0 :(得分:1)
是。它是Strategy
模式。
答案 1 :(得分:1)
这是Strategy (Policy)设计模式。根据上下文,它也可以称为State模式。这两种模式非常相似,您可以查看此问题以获取更多详细信息:What is the difference between Strategy Design pattern and State Design pattern?
更广泛的术语是Inversion of control。使用策略设计模式是实现控制反转的基本技术之一。
另外,通过尝试使您的开源类可扩展,您可以遵循Open/closed principle。正如this文章所述,战略模式是实现开放/封闭的方式之一。