C++ MVC模式以及类的前置声明

MVC模式在日常软件设计中用得很多很多,本文以自动贩卖机为原型,以MVC模式进行设计。

类的声明:

sellor.h

#include <QString>
#include <QDebug>

//注意这里的前置声明要和后边的实现分开,否则会导致编译不通过
class SellerMOdel;
class SellerView;


//This is control
class SellerControl
{
public:
    static SellerControl* getInstance();
    void SetModle(SellerMOdel*);
    void SetView(SellerView*);

    void SellerControlHandleView(int goods_id);

private:
    SellerControl();
    ~SellerControl();

     static SellerControl* SellerInstance;
     SellerMOdel* model;
     SellerView* View;
};



//Model
class SellerMOdel
{
public:
    SellerMOdel();
    int getprice(int goods_id);
    void update(int goods_id, int goods_num);
private:
    int goods_num;
};

//View
class SellerView
{
public:
    SellerView();
    int SubGoodsID(int goodid);
    void SetControl(SellerControl* ctrl);
    void UpdateInfo(QString str);
private:
    SellerControl* ctrl;
};

类的具体实现:

sellor.cpp

#include "sellor.h"

SellerControl* SellerControl::SellerInstance = NULL;

SellerControl::SellerControl()
{
    qDebug() << "Seller Control init";
}

SellerControl::~SellerControl()
{

}

SellerControl* SellerControl::getInstance()
{
    if(SellerControl::SellerInstance == NULL)
    {
        return new SellerControl();
    } else {
        return SellerControl::SellerInstance;
    }
}

void SellerControl::SetModle(SellerMOdel* model)
{
    if(model)
        this->model = model;
}

void SellerControl::SetView(SellerView *view)
{
    if(view)
        this->View = view;
}


void SellerControl::SellerControlHandleView(int goods_id)
{
    if (model)
    {
        int price = model->getprice(goods_id);
        View->UpdateInfo(QString("goods_id is %1, price is %2").arg(goods_id).arg(price));
    }
}


//Model
SellerMOdel::SellerMOdel()
{
    qDebug()<< "Model init";
}


int SellerMOdel::getprice(int goods_id)
{
    return 10;
}

void SellerMOdel::update(int goods_id, int goods_num)
{
    qDebug()<<"goods_id:"<< goods_id;
    qDebug()<<"     goods_num:"<<goods_num;
}



//View
SellerView::SellerView()
{
    qDebug()<<"View init";
}

int SellerView::SubGoodsID(int goodid)
{
    if(ctrl)
    {
        ctrl->SetView(this);
        ctrl->SellerControlHandleView(goodid);
    }
}


void SellerView::SetControl(SellerControl* ctrl)
{
    this->ctrl = ctrl;
}

void SellerView::UpdateInfo(QString str)
{
    qDebug() << str;
}

测试代码:

int main(int argc, char *argv[])
{
    SellerControl* ctrl = SellerControl::getInstance();
    SellerView* view = new SellerView();
    SellerMOdel* model = new SellerMOdel();

    ctrl->SetModle(model);
    view->SetControl(ctrl);
    view->SubGoodsID(10);
}

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++ 中,可以使用声明前置(forward declaration)来解决互相依赖的之间的编译问题。声明前置可以让编译器知道某个的存在,而无需包含该的头文件。 具体来说,如果一个 A 依赖于另一个 B,而 B 又依赖于 A,那么在定义这两个时,就会产生循环依赖的问题。此时,我们可以使用声明前置来解决这个问题。 例如,假设有两个 `ClassA` 和 `ClassB`,它们互相依赖,可以这样来声明前置: ```cpp // ClassA.h 文件 #pragma once // 前置声明 ClassB class ClassB; class ClassA { public: void func(ClassB* b); }; // ClassB.h 文件 #pragma once // 前置声明 ClassA class ClassA; class ClassB { public: void func(ClassA* a); }; ``` 在上面的代码中,我们使用了 `class ClassB;` 和 `class ClassA;` 来声明前置,而不是包含的头文件。这样,编译器就能够知道这两个的存在,从而解决了循环依赖的问题。 需要注意的是,如果使用了声明前置,那么只能在函数参数、函数返回值或者指针成员变量等地方使用这个。如果需要使用该的具体实现,还需要包含该的头文件。例如,在 `ClassA.cpp` 文件中实现 `ClassA` 的成员函数时,需要包含 `ClassB.h` 头文件: ```cpp // ClassA.cpp 文件 #include "ClassA.h" #include "ClassB.h" void ClassA::func(ClassB* b) { // 使用 ClassB 的具体实现 } ``` 总之,声明前置可以解决互相依赖的之间的编译问题,提高代码的可读性和可维护性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值