有这样的设计模式吗?怎么称呼呢?
我有这样的设计:
public interface MyInterface {
public abstract List<Sth> getSth();
}
public class MyConcreteImplementation implements MyInterface {
private ConcreteSth mSth = new ConcreteSth();
public List<Sth> getSth(){
return mSth.getSth(additionalParams);
}
}
上面代码的目的是提供从其他类调用的统一方法。
这也能称为模式吗?如果可以怎么命名呢?
I have such design:
public interface MyInterface {
public abstract List<Sth> getSth();
}
public class MyConcreteImplementation implements MyInterface {
private ConcreteSth mSth = new ConcreteSth();
public List<Sth> getSth(){
return mSth.getSth(additionalParams);
}
}
Purpose of code above is to provide unified method to be called from other classes.
Can this be called a pattern? If so how to name it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在我看来,它就像一个适配器。它将
ConcreteSth
适配为MyInterface
。不管怎样,如果它完成了您期望的工作,您应该询问它是否只是出于好奇而存在的模式。
It looks to me like an Adapter. It adapts
ConcreteSth
toMyInterface
.Anyway, if it does the work you expect from it, you should be asking about whether it's a pattern only out of curiousity.
您在这里只是遵循基本的面向对象设计。对我来说,这看起来很简单组合,如果您真的热衷于设计模式,我可以扩展它成为委托的形式。
You are just following basic object oriented design here. This looks like simple Composition to me and if you are really keen on a design pattern I could stretch it to being a form of delegate.
适配器
这听起来真的很像适配器。您希望有一个适合您的界面的类。这里的接口是
MyInterface
,适配者是ConcreteSth
。Adapter
That's really sound like Adapter. You want to have a certain class adapted to your interface. The interface here is
MyInterface
and the adaptee isConcreteSth
.我将其称为适配器:它在现有接口(即
ConcreteSth
)周围包装了另一个方法(MyInterface.getSth
),而不更改功能。I would call it an Adapter: it wraps another method (
MyInterface.getSth
) around an existing interface (i.e. theConcreteSth
), without changing the functionality.工厂方法?
工厂方法模式的本质是“定义一个用于创建对象的接口,但让子类决定实例化哪个类。工厂方法让类将实例化推迟到子类”。
http://en.wikipedia.org/wiki/Factory_method_pattern
Factory method?
The essence of the Factory method Pattern is to "Define an interface for creating an object, but let the subclasses decide which class to instantiate. The Factory method lets a class defer instantiation to subclasses."
http://en.wikipedia.org/wiki/Factory_method_pattern