开发者

Class interface design woes

开发者 https://www.devze.com 2023-03-29 04:20 出处:网络
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 reci

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 b开发者_运维技巧e 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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号