个人网站设计首页,重庆市城市建设投资公司网站,网站开发有多少种,在线单页网站制作TouchGFX用户接口遵循Model-View-Presenter#xff08;MVP#xff09;架构模式#xff0c;它是Model-View-Controller#xff08;MVC#xff09;模式的派生模式。 两者都广泛用于构建用户接口应用。
MVP模式的主要优势是#xff1a;
关注点分离#xff1a;将代码分成不…TouchGFX用户接口遵循Model-View-PresenterMVP架构模式它是Model-View-ControllerMVC模式的派生模式。 两者都广泛用于构建用户接口应用。
MVP模式的主要优势是
关注点分离将代码分成不同的部分提供每部分有自己的任务。 这使得代码更简单、可重复使用性更高且更易于维护。单元测试由于UI的逻辑Presenter独立于视图View因此单独测试这些部分会容易很多。
MVP中定义了下列三个类
Model是一种接口用于定义要在用户界面上显示或有其他形式操作的数据。View是一种被动接口用于显示数据来自Model并将用户指令事件传给Presenter以便根据该数据进行操作。Presenter的操作取决于Model和View。 它从存储库Model检索数据并将其格式化以便在视图中显示。 在TouchGFX中从Model类执行与应用非UI部分这里称为后端系统的通信。 后端系统是从UI接收事件和将事件输入UI的软件组件例如采集传感器的新测量值。 后端系统可作为单独的任务在同一MCU、单独的处理器、云模块或其他硬件上运行。 从TouchGFX的角度来看这并不十分重要只要它是能够与之通信的组件。
使用的特定通信协议不受TouchGFX管理。 它只提供一个在每个TouchGFX嘀嗒时间调用一次的函数可以在其中处理需要的通信。 从上节内容已知touchgfx中只有一个Model的实例对象但每一个屏幕都对应一个View和Presenter的实例对象。 从MVP类的代码可以看出
Model实例对象可以绑定一个ModelListener用户端Presenter的基类之一实例对象指针即Model实例对象可以通过该指针查询到Presenter实例对象。
每个View实例对象可以绑定一个Presenter实例对象指针即View实例对象可以通过该指针查询到Presenter实例对象。
每个用户端Presenter实例对象基于ModelListener类可以绑定Model实例对象指针即用户端Presenter实例对象可以通过该指针查询到Model实例对象。
用户端Presenter类中包含View实例对象的引用即用户端Presenter实例对象可以通过该引用查询到View实例对象。
#ifndef MODEL_HPP
#define MODEL_HPPclass ModelListener;/* Model类 */
class Model
{
public:Model();/* 将给定的Presenter对象指针与Model关联起来 */void bind(ModelListener *listener){modelListener listener;}/* 节拍函数 */void tick();protected:ModelListener *modelListener; //当前Presenter对象指针
};#endif#ifndef TOUCHGFX_VIEW_HPP
#define TOUCHGFX_VIEW_HPP#include touchgfx/Screen.hppnamespace touchgfx
{
/* View类 */
template class T
class View : public Screen
{
public:/* 构造函数 */View() : presenter(0){}/* 将给定的Presenter实例与View关联起来 */void bind(T newPresenter){presenter newPresenter;}protected:T *presenter; //指向与此View关联的presenter的指针
};}#endif#ifndef TOUCHGFX_PRESENTER_HPP
#define TOUCHGFX_PRESENTER_HPPnamespace touchgfx
{
/* Presenter类 */
class Presenter
{
public:/* 当Presenter由于屏幕进入而变为活动状态时会自动调用此函数。它通常用于初始化Presenter */virtual void activate(){}/* 当Presenter由于屏幕退出而变为非活动状态时会自动调用此函数。它通常用于清理Presenter */virtual void deactivate(){}/* 析构函数 */virtual ~Presenter(){}protected:/* 构造函数 */Presenter(){}
};}#endif#ifndef MODELLISTENER_HPP
#define MODELLISTENER_HPP#include gui/model/Model.hpp/* ModelListener类 */
class ModelListener
{
public:/* 构造函数 */ModelListener() : model(0) {}/* 析构函数 */virtual ~ModelListener() {}/* 将Model与Presenter实例关联起来 */void bind(Model *m){model m;}protected:Model *model; //Model实例指针
};#endif#ifndef SCREENPRESENTER_HPP
#define SCREENPRESENTER_HPP
#include gui/model/ModelListener.hpp
#include mvp/Presenter.hppusing namespace touchgfx;class screenView;/* 用户端某screen对应的Presenter类 */
class screenPresenter : public touchgfx::Presenter, public ModelListener
{
public:...private:...screenView view;
};#endif通过MVPApplication类的代码可以大概看出引擎是怎么通过MVP切换屏幕的
首先pendingScreenTransitionCallback不为空引擎将调用evaluatePendingScreenTransition函数执行切换回调函数用户需要在切换回调函数中会调用makeTransition来实现屏幕切换
#ifndef TOUCHGFX_MVPAPPLICATION_HPP
#define TOUCHGFX_MVPAPPLICATION_HPP
#include new
#include common/AbstractPartition.hpp
#include mvp/MVPHeap.hpp
#include mvp/Presenter.hpp
#include touchgfx/Application.hpp
#include touchgfx/Callback.hpp
#include touchgfx/Screen.hpp
#include touchgfx/hal/Types.hpp
#include touchgfx/transitions/Transition.hppnamespace touchgfx
{
class Presenter;/* MVP应用类 */
class MVPApplication : public Application
{
public:/* 构造函数 */MVPApplication() : currentPresenter(0), pendingScreenTransitionCallback(0){instance this;}/* 执行挂起的屏幕转换回调 */virtual void handlePendingScreenTransition(){evaluatePendingScreenTransition();}protected:Presenter *currentPresenter; //指向当前活动的Presenter对象的指针GenericCallback* pendingScreenTransitionCallback; //指向挂起的屏幕转换回调指针/* 执行挂起的屏幕转换回调 */void evaluatePendingScreenTransition(){if(pendingScreenTransitionCallback pendingScreenTransitionCallback-isValid()){pendingScreenTransitionCallback-execute();pendingScreenTransitionCallback 0; //调用完后将指针清空等待下一次注册}}
};/* 准备屏幕转换当退出当前屏幕前需要清理正在活动的一些数据 */
FORCE_INLINE_FUNCTION static void prepareTransition(Screen **currentScreen, Presenter **currentPresenter, Transition **currentTrans)
{/* 卸载屏幕中所有定时的控件 */Application::getInstance()-clearAllTimerWidgets();/* 清理当前Transition */if(*currentTrans){(*currentTrans)-tearDown();}/* 析构当前Transition */if(*currentTrans){(*currentTrans)-~Transition();}/* 清理当前View */if(*currentScreen){(*currentScreen)-tearDownScreen();}/* 清理当前Presenter */if(*currentPresenter){(*currentPresenter)-deactivate();}/* 析构当前View */if(*currentScreen){(*currentScreen)-~Screen();}/* 析构当前Presenter */if(*currentPresenter){(*currentPresenter)-~Presenter();}
}/* 完成屏幕转换当进入新屏幕后需要初始化新屏幕的一些数据。然后重绘新屏幕 */
FORCE_INLINE_FUNCTION static void finalizeTransition(Screen *newScreen, Presenter *newPresenter, Transition *newTransition)
{/* 初始化新的View */newScreen-setupScreen();/* 初始化新的Presenter */newPresenter-activate();/* 将新给定的Transition实例与新的View关联起来 */newScreen-bindTransition(*newTransition);/* 初始化新的Transition */newTransition-init();/* 重绘整个屏幕 */newTransition-invalidate();
}/* 用于实现屏幕转换的功能 */
template class ScreenType, class PresenterType, class TransType, class ModelType
PresenterType *makeTransition(Screen **currentScreen, Presenter **currentPresenter, MVPHeap heap, Transition **currentTrans, ModelType *model)
{assert(sizeof(ScreenType) heap.screenStorage.element_size() View allocation error: Check that all views are added to FrontendHeap::ViewTypes);assert(sizeof(PresenterType) heap.presenterStorage.element_size() Presenter allocation error: Check that all presenters are added to FrontendHeap::PresenterTypes);assert(sizeof(TransType) heap.transitionStorage.element_size() Transition allocation error: Check that all transitions are added to FrontendHeap::TransitionTypes);/* 准备屏幕转换当退出当前屏幕前需要清理正在活动的一些数据 */prepareTransition(currentScreen, currentPresenter, currentTrans);/* 在相应的内存分区中构建新的Transition、View、Presenter实例 */TransType *newTransition new (heap.transitionStorage.atTransType(0)) TransType;ScreenType *newScreen new (heap.screenStorage.atScreenType(0)) ScreenType;PresenterType *newPresenter new (heap.presenterStorage.atPresenterType(0)) PresenterType(*newScreen);/* 返回新的Transition、View、Presenter实例对象指针 */*currentTrans newTransition;*currentPresenter newPresenter;*currentScreen newScreen;/* 将新给定的Presenter实例与Model关联起来 */model-bind(newPresenter);/* 将Model与新的Presenter实例关联起来 */newPresenter-bind(model);/* 将新给定的Presenter实例与新的View关联起来 */newScreen-bind(*newPresenter);/* 完成屏幕转换当进入新屏幕后需要初始化新屏幕的一些数据。然后重绘新屏幕 */finalizeTransition((Screen *)newScreen, (Presenter *)newPresenter, (Transition *)newTransition);return newPresenter;
}}#endif在前端应用基类FrontendApplicationBase中实现了对各种屏幕切换函数的封装
#ifndef FRONTENDAPPLICATIONBASE_HPP
#define FRONTENDAPPLICATIONBASE_HPP
#include mvp/MVPApplication.hpp
#include gui/model/Model.hppclass FrontendHeap;/* 前端应用基类 */
class FrontendApplicationBase : public touchgfx::MVPApplication
{
public:/* 构造函数 */FrontendApplicationBase(Model m, FrontendHeap heap);/* 析构函数 */virtual ~FrontendApplicationBase() { }/* 切换到起始屏幕 */virtual void changeToStartScreen(){gotoscreenScreenNoTransition();}/* 不带任何切换动画切换到screen屏幕 */void gotoscreenScreenNoTransition();protected:touchgfx::CallbackFrontendApplicationBase transitionCallback; //切换回调FrontendHeap frontendHeap; //前端内存堆引用Model model; //Model引用void gotoscreenScreenNoTransitionImpl(); //不带任何切换动画切换到screen屏幕回调执行函数
};#endif#include new
#include gui_generated/common/FrontendApplicationBase.hpp
#include gui/common/FrontendHeap.hpp
#include touchgfx/transitions/NoTransition.hpp
#include texts/TextKeysAndLanguages.hpp
#include touchgfx/Texts.hpp
#include touchgfx/hal/HAL.hpp
#include platform/driver/lcd/LCD16bpp.hpp
#include gui/screen_screen/screenView.hpp
#include gui/screen_screen/screenPresenter.hppusing namespace touchgfx;FrontendApplicationBase::FrontendApplicationBase(Model m, FrontendHeap heap): touchgfx::MVPApplication(),transitionCallback(),frontendHeap(heap),model(m)
{touchgfx::HAL::getInstance()-setDisplayOrientation(touchgfx::ORIENTATION_LANDSCAPE);reinterpret_casttouchgfx::LCD16bpp(touchgfx::HAL::lcd()).enableTextureMapperAll();reinterpret_casttouchgfx::LCD16bpp(touchgfx::HAL::lcd()).enableDecompressorL8_All();
}/* 不带任何切换动画切换到screen屏幕 */
void FrontendApplicationBase::gotoscreenScreenNoTransition()
{/* 设置切换屏幕回调 */transitionCallback touchgfx::CallbackFrontendApplicationBase(this, FrontendApplicationBase::gotoscreenScreenNoTransitionImpl);/* 将该切换行为注册到pendingScreenTransitionCallback */pendingScreenTransitionCallback transitionCallback;
}/* 不带任何切换动画切换到screen屏幕回调执行函数 */
void FrontendApplicationBase::gotoscreenScreenNoTransitionImpl()
{/* 实现屏幕转换的功能 */touchgfx::makeTransitionscreenView, screenPresenter, touchgfx::NoTransition, Model (currentScreen, currentPresenter, frontendHeap, currentTransition, model);
}在前端应用类FrontendApplication中调用model.tick()。即为model提供周期节拍用来采样UI所需要的数据
#ifndef FRONTENDAPPLICATION_HPP
#define FRONTENDAPPLICATION_HPP
#include gui_generated/common/FrontendApplicationBase.hppclass FrontendHeap;using namespace touchgfx;class FrontendApplication : public FrontendApplicationBase
{
public:FrontendApplication(Model m, FrontendHeap heap);virtual ~FrontendApplication() { }/* 定时事件 */virtual void handleTickEvent(){model.tick(); //调用model定时事件FrontendApplicationBase::handleTickEvent(); //调用基类定时事件}
private:
};#endif#include gui/common/FrontendApplication.hppFrontendApplication::FrontendApplication(Model m, FrontendHeap heap): FrontendApplicationBase(m, heap)
{}