类接口设计问题

发布于 2024-11-30 01:07:28 字数 1167 浏览 0 评论 0原文

我对如何最好地构建新类的接口有疑问。还有另外两个类与新类进行通信:PlatformSensor。新类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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

太傻旳人生 2024-12-07 01:07:28

您可以使用设计模式“观察者”

然后,平台对象可以订阅探路者事件“开始重新计算”(立即停止移动或返回或...)和“计算完成”。当 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文