抽象类选择性地基于派生类型揭示方法

发布于 2025-02-06 12:01:34 字数 1744 浏览 1 评论 0原文

我正在编写一个实现工厂timeManager生成两种类型对象的程序; 计时器stopwatch。这两个对象均来自抽象类timepiece,并共享常见方法。我希望工厂返回类型timepiece,但是由于计时器实现了额外的方法,所以我不能。如何使timepiece公开timer的额外方法,而无需将它们暴露于sepwatch创建这些相应类型时?我希望这有意义,因为很难解释。

timemanager.h

enum TimePieceType{TIMER, STOP_WATCH};

class TimeManager {
public:
    TimeManager();
    ~TimeManager();

    void Tick();
    TimePiece* AddTimePiece(TimePieceType timeType);
};

timepiece.h

enum RunningState { STOPPED, RUNNING, PAUSED };

class TimePiece {
private:
    unsigned long startTime = 0;
    unsigned long currentTime = 0;
    unsigned long pauseOffset = 0;

protected:
    Time time;
    RunningState state = STOPPED;

public:
    TimePiece() = default;
    virtual ~TimePiece() = default;

    void Stop();
    void Start();
    void Pause();
    void Resume();
    void Reset();
    virtual void Tick();

    Time GetTime();
    RunningState GetState();
    virtual uint8_t GetId() = 0;
};

stepwatch.h

class StopWatch : public TimePiece {
private:
    uint8_t watchId = 0;
    static int8_t currentId;
    
public:
    StopWatch();
    virtual ~StopWatch();

    uint8_t GetId();
};

Timer.h

class Timer : public TimePiece {
private:
    uint8_t timerId = 0;
    static int8_t currentId;
    unsigned long timeout;

public:
    Timer();
    ~Timer();

    void Tick() override;

    void SetTimeoutMs(unsigned long timeout);
    void SetTimeoutSec(unsigned long timeout);

    uint8_t GetId();
    Time GetRemainingTime();
    unsigned long GetTimeout();
};

I am writing an program that implements a factory TimeManager to generate two types of objects; a Timer and a StopWatch. Both of these objects are derived from an abstract class TimePiece, and share common methods. I would like the factory to return type TimePiece, but because Timer implements extra methods, I cant. How can I make TimePiece expose Timer's extra methods without exposing them to StopWatch when those respective types are created? I hope this makes sense, as it is a little hard to explain.

TimeManager.h

enum TimePieceType{TIMER, STOP_WATCH};

class TimeManager {
public:
    TimeManager();
    ~TimeManager();

    void Tick();
    TimePiece* AddTimePiece(TimePieceType timeType);
};

TimePiece.h

enum RunningState { STOPPED, RUNNING, PAUSED };

class TimePiece {
private:
    unsigned long startTime = 0;
    unsigned long currentTime = 0;
    unsigned long pauseOffset = 0;

protected:
    Time time;
    RunningState state = STOPPED;

public:
    TimePiece() = default;
    virtual ~TimePiece() = default;

    void Stop();
    void Start();
    void Pause();
    void Resume();
    void Reset();
    virtual void Tick();

    Time GetTime();
    RunningState GetState();
    virtual uint8_t GetId() = 0;
};

StopWatch.h

class StopWatch : public TimePiece {
private:
    uint8_t watchId = 0;
    static int8_t currentId;
    
public:
    StopWatch();
    virtual ~StopWatch();

    uint8_t GetId();
};

Timer.h

class Timer : public TimePiece {
private:
    uint8_t timerId = 0;
    static int8_t currentId;
    unsigned long timeout;

public:
    Timer();
    ~Timer();

    void Tick() override;

    void SetTimeoutMs(unsigned long timeout);
    void SetTimeoutSec(unsigned long timeout);

    uint8_t GetId();
    Time GetRemainingTime();
    unsigned long GetTimeout();
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

挽手叙旧 2025-02-13 12:01:35

您不应返回timepiece*,因为两个子类共享timepiece其实现而不是其接口。

一种简单的方法是给出timeManager两个单独的方法:

Timer* AddTimer();
StopWatch* AddStopWatch();

如果需要remove> remove方法:您可以利用多态性行为:

void RemoveTimePiece(TimePiece*);

You should not return TimePiece* because the two subclasses are sharing TimePiece for its implementation, not for its interface.

A simple approach is to give TimeManager two separate methods:

Timer* AddTimer();
StopWatch* AddStopWatch();

You can exploit polymorphic behavior if you need the Remove method:

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