微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

工厂设计 C++/QtCreator

如何解决工厂设计 C++/QtCreator

我正在尝试实现工厂设计模式。我使用“Factory”类的静态成员函数,它创建并返回“Candy”的实例,但对用户隐藏了类模块的详细信息。

我的头文件粘贴如下:

#ifndef CANDY_H
#define CANDY_H

#include <QColor>
#include <QGraphicsItem>
#include <QGraphicsSceneMouseEvent>
#include <QDebug>
#include <QLayoutItem>
#include <QIcon>
#include <QFont>

enum class CandyType {normal,Powerup};
enum class PowerUpType {None,Wrapped,Striped,Sour};

class Candy : public QObject,public QGraphicsItem
{
    // this makes it so that we can emit signals
    Q_OBJECT

public:
    //static std::vector<CandyType> enum_vec;

    //Candy(QColor color,const int i,const int j);

    Candy();

    int get_i() const { return j_; }  // inline member function
    int get_j() const { return i_; }  // inline member function
    QColor get_color() const { return color_; }
    //CandyType get_candy_type() const { return candy_type_; }

    std::vector<Candy*> get_neighbors() { return neighbors_; }

    QRectF boundingRect() const override;
    QPainterPath shape() const override;

    void SwapCandy(Candy *c);

    void StoreNeighbors(std::vector<Candy*> neighbors_vec);

    void Poof();

    void setIJ(int i,int j);

    void paint(QPainter *painter,const qstyleOptionGraphicsItem *item,QWidget *widget) override;

signals:
    void StoreSwapCandySelected(Candy *c);
    void SwapCandySelected(Candy *c);

protected:
    void mousepressEvent(QGraphicsSceneMouseEvent *event) override;

private:
  int i_;
  int j_;

  QColor color_;
  QFont myFont;
  bool next_state;
  static const int width_ = 35;
  std::vector<Candy*> neighbors_;
};  // class RegCandy
 // CANDY_H

/*INHERITED CLASS -- the functions that shoudl be different from parent
 * are the constructor and the mousepressevent. In addition some feilds
 * and getters are unnecessary and some added functions are used*/

class PowerUp : public Candy
{
    Q_OBJECT

public:
    static std::vector<PowerUpType> enum_vec;
    PowerUp() : Candy() {}

    std::string CandyTypetoString() {
        if (powerup_type_ == PowerUpType::Wrapped) {
            return "Wrapped";
        } else if (powerup_type_ == PowerUpType::Striped) {
            return "Striped";
        } else {
            return "Sour";
        }
    }

    PowerUpType get_candy_type() const { return powerup_type_; }

    void setPUType(PowerUpType p_type) {
        this->powerup_type_ = p_type;
    }

    void setColor(QColor color) {
        this->color_ = color;
    }

    void setEmpty() {
        empty_ = true;
    }

    void setFull() {
        empty_ = false;
    }

    bool isEmpty() { return empty_; }

signals:
    void StorePowerUpSelected(PowerUp *p);

protected:
    virtual void mousepressEvent(QGraphicsSceneMouseEvent *event) override {
        if (event->button() == Qt::LeftButton) {
            //qDebug() << QString::fromStdString(this->CandyTypetoString()) << ": " << this->get_i() << "," << this->get_j();
            emit StorePowerUpSelected(this);
        }
        update();
    }

private:
  int i_;
  int j_;
  PowerUpType powerup_type_;
  QColor color_;
  bool empty_ = true;;
  //bool next_state;
  static const int width_ = 35;

}; //class PowerUp

class Factory {
public:

    /*~Factory() {
        if (c_type) {
            delete[] c_type;
            c_type = NULL;
        }
    }*/
    static Candy* getCandy(CandyType type)  {
        if (type == CandyType::normal)
            return new Candy();
        else if (type == CandyType::Powerup)
            return new PowerUp();
        else return NULL;
    }

};

#endif

目前我的 PowerUp 类继承自我的 Candy 类,我想使用 Factory::getCandy() 创建每个对象的实例,但是当我尝试像这样调用静态方法 getCandy() 时:

PowerUp *p = Factory::getCandy(CandyType::Powerup);

我收到以下错误,“错误:无法用 Candy * 类型的右值初始化 PowerUp * 类型的变量”

我认为通过使 'PowerUp' 类继承自 'Candy' 类,我可以将返回值 Candy* 存储在 PowerUP* 中,但是在对继承进行更多思考之后,似乎逻辑如下,'每个 PowerUp 都是 Candy,但并非每个 Candy 都是 PowerUp',所以也许应该写下这一行:

Candy *c = Factory::getCandy(CandyType::Powerup);

但是我需要“PowerUp”糖果有一些与超类不同的方法,所以我不知道为什么我会在 Candy* 中存储 Factory::getCandy() 的返回值,如果我特别想要一个 PowerUp 的实例*.

注意:它适用于使用以下行创建“Candy”实例: Candy *c = Factory::getCandy(CandyType::normal)

我知道我的实现可能并不完美,这是我第一次尝试将工厂设计添加到我的代码中,但它似乎在我的程序中非常有用。任何指针都非常感谢。提前致谢!

解决方法

你说:

但是我需要 PowerUp 糖果有一些不同的方法 比超类,所以我不知道为什么我会存储返回 Factory::getCandy()Candy* 的值,如果我特别想要一个 PowerUp* 的实例。

如果这是真的,工厂就没有意义了,使用简单的构造函数。工厂的全部意义在于通过共享一个通用接口来抽象出细节(例如您的 Candy 实际上是一个 PowerUp)。在您的情况下,如果应用程序知道它使用的特定类型并仅调用该类型具有的方法,则意味着没有抽象。

你可以:

  1. 移除工厂并使用经典构造函数。
  2. 检查您的 Candy 抽象,使 PowerUp 成为真正的实现细节。最后,在调用代码的眼中,使用 PowerUp 和任何其他类似 Candy 的类型应该没有区别。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。