类接口设计问题
我对如何最好地构建新类的接口有疑问。还有另外两个类与新类进行通信:Platform
和 Sensor
。新类Pathfinder
将从Sensor
接收传感器数据,并在计算路径时考虑任何新数据。 Platform
沿着 Pathfinder
创建的路径移动,但如果 Sensor
检测到威胁,Pathfinder
将生成新的威胁Platform
下次尝试移动时将自动使用的路径。
我现在草拟的界面在伪 C++ 中看起来像这样:
class Sensor {
Detect() {
// Get Data
Pathfinder.Process(Data)
}
}
class Platform {
Move() {
while(Can move further)
Waypoint w = Pathfinder.GetNextWaypoint()
// Move towards w
if(Arrived at w)
Pathfinder.PassedWaypoint()
}
}
class Pathfinder {
Process(Data) {
// Adapt path to accomodate Data
RecalculatePath()
}
GetNextWaypoint() {
if(!Path calculated)
RecalculatePath()
return Path.front()
}
PassedWaypoint() {
Path.pop_front()
}
RecalculatePath() {
// Do pathfinding
}
vector<Waypoint> Path
}
我对平台与探路者交互的方式不太满意。另一方面,如果我让平台获取整个路径,它就必须定期检查是否有变化,而且可能不够频繁,从而进入检测到的任何威胁。
如何改进这个设计?
I'm having doubts about how to best construct the interface of a new class. There will be two other classes communicating with the new class, Platform
and Sensor
. The new class, Pathfinder
, will recieve sensor data from Sensor
and take any new data in account when calculating paths. Platform
is moving along the path created by Pathfinder
, but if Sensor
detects a threat, Pathfinder
will generate a new path which is to be used automatically by Platform
the next time it attempts to move.
The interface I've sketched up now looks like this in pseudo-C++:
class Sensor {
Detect() {
// Get Data
Pathfinder.Process(Data)
}
}
class Platform {
Move() {
while(Can move further)
Waypoint w = Pathfinder.GetNextWaypoint()
// Move towards w
if(Arrived at w)
Pathfinder.PassedWaypoint()
}
}
class Pathfinder {
Process(Data) {
// Adapt path to accomodate Data
RecalculatePath()
}
GetNextWaypoint() {
if(!Path calculated)
RecalculatePath()
return Path.front()
}
PassedWaypoint() {
Path.pop_front()
}
RecalculatePath() {
// Do pathfinding
}
vector<Waypoint> Path
}
I'm not really satisfied with how the platform will interact with the pathfinder. On the other hand, if I let the platform fetch the entire path, it will have to check periodically if something has changed, and potentially not often enough, thus walking into whatever threat is detected.
How can this design be improved?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用设计模式“观察者”。
然后,平台对象可以订阅探路者事件“开始重新计算”(立即停止移动或返回或...)和“计算完成”。当 Pathfinder 对象出现新路径时,Platform 对象可以立即请求整个数据。
You could use the design pattern "Observer".
Then the Platform object could subscribe to the Pathfinders events "Started recalculation" (immediately stop moving or go back or ...) and "Calculation finished". When there is a new path at the Pathfinder object, the Platform object can ask for the whole data at once.