我对如何最好地构造一个新类的接口感到疑惑。将有另外两个类与新类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
}我对平台将如何与探路者交互并不是很满意。另一方面,如果我让平台获取整个路径,它将不得不定期检查是否有变化,并且可能不够频繁,从而进入检测到的任何威胁。
如何改进这种设计?
发布于 2011-08-17 19:03:19
您可以使用设计模式"Observer"。
然后平台对象可以订阅Pathfinders事件“已开始重新计算”(立即停止移动或返回或...)和“计算完成”。当Pathfinder对象上有新路径时,Platform对象可以一次请求整个数据。
https://stackoverflow.com/questions/7091548
复制相似问题