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 应用程序的不同职责分离,使得代码更加模块化、易于维护和扩展。在实际应用中,可以根据具体的项目需求选择合适的设计模式。

相关推荐
Carl_奕然28 分钟前
【智能体】Agent的四种设计模式之:ReAct
人工智能·设计模式·语言模型
二哈赛车手2 小时前
新人笔记---多策略搭建策略执行链实现RAG检索后过滤
java·笔记·spring·设计模式·ai·策略模式
楼田莉子3 小时前
仿Muduo的高并发服务器:Channel模块与Poller模块
linux·服务器·c++·学习·设计模式
geovindu18 小时前
go: Strategy Pattern
开发语言·设计模式·golang·策略模式
嵌入式学习_force1 天前
02_state
设计模式·蓝牙
qcx231 天前
Warp源码深度解析(七):Token预算策略——双轨计费、上下文溢出与摘要压缩
人工智能·设计模式·rust·wrap
Cosolar2 天前
提示词工程面试题系列 - Zero-Shot Prompting 和 Few-Shot Prompting 的核心区别是什么?
人工智能·设计模式·架构
geovindu2 天前
go:Template Method Pattern
开发语言·后端·设计模式·golang·模板方法模式
钝挫力PROGRAMER2 天前
贫血模型的改进
java·开发语言·设计模式·架构
qcx232 天前
Warp源码深度解析(二):自研GPU UI框架——WarpUI的ECH模式与渲染管线
人工智能·ui·设计模式·rust