【发布时间】:2021-05-01 15:16:40
【问题描述】:
我正在尝试实现工厂设计模式。我使用“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'糖果有一些不同于超类的方法,所以我不知道如果我特别想要一个 PowerUp 实例,我为什么要将 Factory::getCandy() 的返回值存储在 Candy* 中*.
注意:它适用于使用以下行创建“Candy”实例:
Candy *c = Factory::getCandy(CandyType::Normal)
我知道我的实现可能并不完美,这是我第一次尝试将工厂设计添加到我的代码中,但它似乎在我的程序中非常有用。非常感谢任何指针。提前谢谢!
【问题讨论】: