隐藏部分类的最简单方法公共方法/接口
我正在尝试设计机器人模拟器的 GUI 前端(实际上是一个简单的游戏)。但是,我不知道将模拟器组件(例如机器人和墙壁)传递到显示器的最佳方式。我想隐藏组件的非显示导向信息(例如机器人质量),但仍然能够识别正在打印的每个组件,即当我绘制组件时,我想以不同于墙壁的方式绘制机器人(也许机器人会有一个名字标签或其他东西)。
这是一张有望解释设计的图片:
也许有一种我还没有遇到过的有用的设计模式......
I am trying to design the GUI front end of a robot simulator (effectively a simple game). However, I don't know the best way of passing the simulator components (such as Robots and Walls) to the display. I want to hide the non-display oriented information of the components (such as the Robots mass), yet still be able to recognise each component im printing, i.e. when I'm drawing components I want to draw Robots differently that I do Walls (maybe the robot will have a name tag or something).
Here is a picture that will hopefully explain the design:
Maybe there is a useful design pattern that I haven't come across yet...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为你应该通过接口契约来设计它。
我会让你的墙壁、机器人和传感器成为用户界面需要了解的各种“事物”的实现。只有那些接口才应该在 UI 和模型之间共享。
例如,Robot、Sensor 应该实现一个名为 Printable 的接口:
Wall 应该实现一个扩展接口 PrintableTexture
您还可以创建并实现角度、方向等数据提供者类型接口。
例如:
要点是“打印”代码然后将检查已传递给它的 Printable 对象(或 Printable 对象列表)实现了哪些接口并做出适当的反应。
看看你的评论,我认为 PrintableRobot、PrintableWall 等是对界面基本概念的误解。接口应该更多地关注“某物提供什么或如何使用它”,而不是如何实现这一点的具体实现。通过将机器人、墙壁等放入可打印中,您可以给出实施的指示。
除此之外,您是否考虑过访客模式?您可以让每个实体实现访问者模式的接受部分,并让您的打印代码成为一个特殊的实现,该实现仅从对每个实体所做的更深入的了解中获取所需的内容......这不是我会做的,但是它可能适合你...
I think you should design this by interface contract.
I would make your walls, robots and sensors be implementations of various 'things' the UI needs to know about. Only those interfaces should be shared between the UI and your Model.
For example, Robot, Sensor should implement an interface called Printable:
Wall should implement an extended interface PrintableTexture
You could also create and implement data provider type interfaces for angle, direction, etc.
For example:
The main point is that the 'printing' code would then check for what interfaces are implemented by the Printable object (or list of Printable objects) that has been passed to the it and react appropriately.
Looking at your comments, I think that PrintableRobot, PrintableWall, etc is a misunderstanding of the fundamental concept of what an interface is. An interface should be more about 'what something provides or how you can use it' versus a concrete implementation of how this is achieved. By putting Robot, Wall, etc in Printable you are giving an indication of implementation.
This aside, have you considered the Visitor Pattern?? You could have each entity implement the accept part of the visitor pattern and have your printing code be a special implementation that only takes what it needs out of a deeper knowledge of what each entity does.... It's not what I would do, but it may suit you...
查看模型-视图-控制器设计模式。它将数据(机器人的速度、大小……)、表示(机器人的形状及其绘制方法)和行为(提高机器人速度)分开。
回答你的问题 - 隐藏类 API 部分的最简单方法是将此类拆分为多个部分(模型、视图、控制器)并根据某种模式(MVC 或模型视图演示器,它们有很多)连接它们其中)。
编辑:抱歉,我没有提供任何示例。我的建议是将 Robot 分成两个类:
然后 Simulator 包含 RobotUi 的集合(Simulator 是一个模型)和 SimulatorDisplay(=视图)在执行绘制方法时迭代 UI 对象。 RobotData 将隐藏在 RobotUi 内部。
Checkout the model-view-controller design pattern. It separates data (robot's speed, size,...), presentation (robot's shape and its paint method) and behavior (increase robot speed).
To answer your question - the simplest way to hide parts of a class's API is to split this class into multiple pieces (model, view, controller) and connect them according to some pattern (MVC, or model-view-presenter, they are many of them).
EDIT: Sorry for that I didn't provide any example. My suggestion is just to split Robot into two classes:
The Simulator then contains collection of RobotUi (Simulator is a model) and SimulatorDisplay (=view) iterates through the UI objects when performing paint method. The RobotData will be hidden inside RobotUi.