TouchGFX设计模式代码实例说明

一)Model - View - Presenter (MVP) 模式在 TouchGFX 中的应用
1)Model(模型):

模型代表应用程序的数据和业务逻辑。例如,在一个简单的计数器应用中,模型可以是一个包含计数器当前值的类。

cpp 复制代码
class CounterModel 
{
private:
    int count;
public:
    CounterModel() : count(0) {}
    int getCount() const 
    {
        return count;
    }
    void increment() 
    {
        count++;
    }
};

2)View(视图):

在 TouchGFX 中,视图是通过界面设计工具创建的图形用户界面(GUI)元素。例如,有一个文本框用于显示计数器的值,一个按钮用于增加计数器的值。这些 GUI 元素在 TouchGFX Designer 中设计,然后在代码中通过相关的类来引用。

视图类需要能够更新界面显示,以反映模型的状态变化。以下是一个简单的视图类示例,用于显示计数器的值:

cpp 复制代码
class CounterView : public touchgfx::View
 {
private:
    touchgfx::TextAreaWithOneWildcard& countText;
public:
    CounterView(touchgfx::TextAreaWithOneWildcard& textArea) : countText(textArea) {}
    void updateCounterDisplay(int count)
     {
        Unicode::snprintf(countText.getBuffer(), countText.getBufferSize(), "%d", count);
        countText.invalidate();
    }
};

3)Presenter(展示器):

展示器作为模型和视图之间的桥梁,它包含对模型和视图的引用,并处理用户交互。当用户点击增加计数器的按钮时,展示器会调用模型的increment方法来更新数据,然后调用视图的updateCounterDisplay方法来更新界面。

cpp 复制代码
class CounterPresenter 
{
private:
    CounterModel& model;
    CounterView& view;
public:
    CounterPresenter(CounterModel& m, CounterView& v) : model(m), view(v) {}
    void handleIncrementButtonClicked() 
    {
        model.increment();
        view.updateCounterDisplay(model.getCount());
    }
};

使用示例:

在main函数或者应用程序初始化的地方,可以这样使用 MVP 模式构建计数器应用。

// 创建模型、视图和展示器

cpp 复制代码
CounterModel counterModel;
touchgfx::TextAreaWithOneWildcard countTextArea;
CounterView counterView(countTextArea);
CounterPresenter counterPresenter(counterModel, counterView);

// 假设这里有一个按钮点击事件处理器,将其与展示器的方法关联

Button incrementButton;

incrementButton.setClickAction(counterPresenter, &CounterPresenter::handleIncrementButtonClicked);
二)Model - View - Controller (MVC) 模式在 TouchGFX 中的应用示例(与 MVP 类似但有区别)

1)Model(模型):

同样以计数器应用为例,模型类和 MVP 模式中的类似。

cpp 复制代码
class CounterModel
 {
private:
    int count;
public:
    CounterModel() : count(0) {}
    int getCount() const
     {
        return count;
    }
    void increment() {
        count++;
    }
};

2)View(视图):

在 MVC 模式中,视图主要负责显示。它会从模型获取数据来更新自己的显示,但不像 MVP 中的视图那样直接被展示器调用更新方法。

cpp 复制代码
class CounterView : public touchgfx::View 
{
private:
    touchgfx::TextAreaWithOneWildcard& countText;
public:
    CounterView(touchgfx::TextAreaWithOneWildcard& textArea) : countText(textArea) {}
    void updateCounterDisplay(int count)
     {
        Unicode::snprintf(countText.getBuffer(), countText.getBufferSize(), "%d", count);
        countText.invalidate();
    }
};

3)Controller(控制器):

控制器处理用户输入和模型更新,同时也会通知视图更新显示。与 MVP 不同的是,它的职责划分更侧重于控制整个流程。

cpp 复制代码
class CounterController
 {
private:
    CounterModel& model;
    CounterView& view;
public:
    CounterController(CounterModel& m, CounterView& v) : model(m), view(v) {}
    void handleIncrementButtonClicked() 
    {
        model.increment();
        view.updateCounterDisplay(model.getCount());
    }
};

使用示例:

与 MVP 模式类似,在应用程序初始化阶段使用 MVC 模式构建计数器应用。

// 创建模型、视图和控制器

CounterModel counterModel;

touchgfx::TextAreaWithOneWildcard countTextArea;

CounterView counterView(countTextArea);

CounterController counterController(counterModel, counterView);

// 假设这里有一个按钮点击事件处理器,将其与控制器的方法关联

Button incrementButton;

incrementButton.setClickAction(counterController, &CounterController::handleIncrementButtonClicked);

这些设计模式有助于将 TouchGFX 应用程序的不同职责分离,使得代码更加模块化、易于维护和扩展。在实际应用中,可以根据具体的项目需求选择合适的设计模式。

相关推荐
高晶1 分钟前
CANopen 在 BMS 系统中的 C 语言实战指南
设计模式·程序员
博风1 小时前
13、设计模式应用举例:工厂方法模式:创建药品对象
设计模式·工厂方法模式
代钦塔拉3 小时前
【C++/Qt 进阶】设计模式系列(5):适配器模式——给“不兼容的接口“贴一层翻译膜
c++·qt·设计模式
bigbread18 小时前
策略模式 + 工厂方法模式在项目中的实战应用
设计模式
高晶18 小时前
C 语言实现 BMS 系统中 SPI 通信协议实战指南
设计模式·程序员
代钦塔拉18 小时前
【C++/Qt 进阶】设计模式系列(2):工厂模式——从游戏角色到披萨店
c++·算法·设计模式
高晶20 小时前
2A3 单端甲类功放从零制作全攻略:电路设计、焊接调试到煲机维护一站式指南
前端·设计模式
代钦塔拉1 天前
【C++/Qt 进阶】设计模式系列(4):原型模式——用“克隆“代替“重新构造“
c++·qt·设计模式
博风1 天前
11、设计模式应用举例:迭代器模式:使用多个集合存储对象
设计模式·迭代器模式
日和雫2 天前
面向对象与面向过程,谁才是“银弹”?
设计模式