对应用程序隐藏系统特定详细信息
我想从我的应用程序逻辑中隐藏系统特定的实现。我选择了以下设计(抽象示例):
public class Mesh implements Drawable {
@Override
public void draw(API api) {
api.render(this);
}
}
public class OpenGL implements API {
@Override
public void render(Drawable drawable) {
// render drawable, using OpenGL
// another API implementing class might use DirectX instead
}
}
尤其是 draw(API api)
方法对我来说看起来很奇怪,因为传递给 API
的对象将传递自身到API
。
我的(天真的)问题是:
- 这最终是一个桥接模式吗?
- 这种方法是否正确实施? (目的是隐藏系统细节。)
- 您会推荐更好的方法吗?
I'd like to hide system specific implementations from my application logic. I have chosen the following design (abstract example):
public class Mesh implements Drawable {
@Override
public void draw(API api) {
api.render(this);
}
}
public class OpenGL implements API {
@Override
public void render(Drawable drawable) {
// render drawable, using OpenGL
// another API implementing class might use DirectX instead
}
}
Especially the draw(API api)
method looks odd to me because the object, the API
is passed to, will pass itself to the API
.
My (naive) questions are:
- Is this eventually a Bridge Pattern?
- Is this approach a proper implementation? (The aim is to hide system specifics.)
- Would you recommend a better approach?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果将 API 传递给 Mesh 的构造函数,那么这将是一个 Bridge 模式。另一方面,它看起来类似于访客模式。
这种方法可能是正确的,但我无法从简短的片段中看出这一点。一个好的设计应该是这样的(这是访问者模式的一个简单示例)。
情况下,这种引用传递是有意义的,因为 Drawable 的实现选择了 API 的正确(重载)方法。
如果没有更多信息,这就是我的推荐。
It would be a Bridge Pattern if you passed the API to the constructor of Mesh. On the other hand, it looks similar to the visitor pattern.
This approach may be proper, but I cant tell that from the short snippet. A good design could look like this (which is a simple example of the visitor pattern).
In this case, this reference passing makes sense, because the implementations of Drawable select the proper (overloaded) method of the API.
Without more information, this is what I would recommend.