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

相关推荐
怕浪猫7 小时前
一行命令复刻爆款视频,我把 Hypit 从安装跑到了出片
人工智能·设计模式·程序员
Zane19949 小时前
函数式编程里的函数,其实不是你天天写的那个函数——三大编程范式的边界在哪
设计模式
Zane19941 天前
策略模式现在该不该上?一次讲清楚过度设计和设计不足怎么找平衡
设计模式
她说..1 天前
常见设计模式-模板方法模式
java·spring·设计模式·springboot
xiaofeiyang1502 天前
第六章 · 桥接 — 三支毛笔,画出九种颜色
设计模式
Shadow(⊙o⊙)2 天前
OTOL设计模式 One Thread One Loop
服务器·网络·设计模式
sarasuki3 天前
如何让LLM 能在半夜偷偷打开网易云呢?
人工智能·设计模式·agent
小王师傅663 天前
【设计模式】装饰模式(四):框架源码实战——从 Java I/O 到 Spring 到 MyBatis
java·设计模式
执明wa3 天前
Android RecyclerView 多类型, 多种 Item
android·xml·开发语言·设计模式·android studio
sarasuki3 天前
如何让 Agent 获取更多的能力?插件 / 技能系统
人工智能·设计模式·agent