In the last part I have simply described the four Patterns:Autonomous View, MVC, MCP, and Presentation model and the principle of choosing them. Since the article is written in Chinese, readers who can not read Chinese please refer to the homepage of Martin Fowler for the detail information of these patterns. I just repeat here the most important principle of choice that I made in the last article. The principle is for the choice of the two patterns: MVP and Presentation Model. Martin Fowler says “it really comes down to how easy it is to do the pattern in your GUI environment and on your own personal tastes.” I think, it is not 100% correct. We should choose the Presentation Model if we have a data binding solution, for example Jgoodies binding. Otherwise MVP is the first choice.
As I use the Presentation Model, I find there is one problem with this pattern – where should the view be initialized and How to initialize the View? When we use the MVC and MVP, we can get the Controller or the Presenter and let them initialize the view. But how can we do this with the Presentation Model, since the presentation model has no reference of the view? As I think about it, I find there are two possible solutions:
The first one is using the Dependency Injection and Business Delegate Patterns. Using Dependence Injection, an object, for example a presentation chooser, can be injected into the presentation model. When we want to create/get view through presentation model, the invocation will be delegated to the injected object.
public class PresentationModel {
private PresentationChooser chooser;
public PresentationModel(PresentationChooser chooser) {
this.chooser = chooser;
}
public View getView() {
chooser.getView(this);
}
}
From this sample code we can see that the PresentationModel does not need any reference of any View.
The Second one is combining the two patterns, i.e. Presentation Mode and MVP. In the normal MVP, the Presenter has references of the View and the Model, most of the functionalities are implemented in the Presenter including some function like Synchronization. As extension, we can add a reference of the model into the View, just like the Presentation Model pattern does. Now we can use some data binding framework, for example Jgoodies binding, to synchronize the view and the model, and let the Presenter do the rest work.
The UML looks like:
It looks a little like the MVC, but actually, it is NOT MVC, because the view has not a reference of the Presenter, and all events handling will be done by the Presenter not the view itself.
Since I have not found that such a pattern has been described by someone else, I call it Presentation Model Presenter(PMP). This solution can also solve the one known problem of using Presentation Model pattern: When the presentation logic goes very complex, the presentation model class goes very big, the maintenance of the code such class is very difficult. Some logics, i.e. the logics for managing the model, should be move to a new class, which is normally called XXXModelManager. Using this new pattern, we can move these logics into the Presenter. And the result is – we got the benefit of both patterns and avert some drawbacks of them. Thanks for the power of the combination!
References:
Dependency Injection http://www.martinfowler.com/articles/injection.html
Presentation Chooser
http://www.martinfowler.com/eaaDev/PresentationChooser.html
This article is written originally by Jing Ge, anyone who want to copy&paste or reference this article, please adds my name "Jing Ge" and my blog: http://blog.csdn.net/schnell
.
分享到:
相关推荐
本文将深入探讨两种流行的UI设计模式——Model View Presenter(MVP)和Model View Controller(MVC)。这两种模式在Java、C#等编程语言的Web应用和桌面应用中广泛应用,帮助开发者实现清晰的代码组织和职责分离。 ...
**设计模式学习:Model View Presenter (MVP)与MVC** 在软件开发中,设计模式是一种通用解决方案,用于解决常见的设计问题,以提高代码的可读性、可维护性和复用性。MVP(Model View Presenter)和MVC(Model View ...
2. **绑定 Presenter**:在你的 Model 类中,通过 `app/Providers/AppServiceProvider` 的 `boot` 方法,将 Model 和对应的 Presenter 进行关联。这样,当你实例化 Model 时,就可以自动获取到关联的 Presenter 对象...
- Presenter是介于Model和View之间的一个组件,它的主要任务是处理模型数据的展示逻辑,如格式化日期、货币等,确保视图层只负责渲染,不涉及复杂的业务处理。 2. **为什么要使用Presenter**: - 提高代码可读性...
CodeProject Model View Presenter with ASP_NET_ Free source code and programming help
在Laravel框架中,Eloquent ORM(对象关系映射)是用于数据库操作的强大工具,而Eloquent Presenter则是进一步提升模型数据展示的一种设计模式。本文将深入探讨Eloquent Presenter的概念、用法及其在Laravel开发中的...
"PRESENTER" 是一款可能与演示文稿或演讲展示相关的软件或工具,但具体的细节并未在提供的信息中明确说明。然而,我们可以基于"字体"这个标签来探讨一些与IT行业,尤其是演示文稿制作中字体相关的知识点。 字体是...
在Laravel框架中,"Laravel开发-laravel-auto-presenter"是一个用于模型数据增强的工具,它通过使用演示者(Presenter)对象来自动地装饰模型的属性,使得开发者可以更优雅地处理视图层的数据展示。这个系统的核心...
为了解决这个问题,"presenter"或"view model"的概念被引入,它作为一个中间层,处理从模型到视图的数据转换。 在"Laravel开发-laravel-presenter"项目中,这个包可能是提供了一套工具或者结构,用于创建和使用演示...
【Nucleus:Android开发中的Model-View-Presenter框架】 Nucleus是一个针对Android平台的开源库,它的设计目标是简化应用开发,特别是在处理后台任务和界面更新时。该库的核心理念是采用Model-View-Presenter(MVP...
C#RTP_Presenter例子和类库C#RTP_Presenter例子和类库C#RTP_Presenter例子和类库C#RTP_Presenter例子和类库C#RTP_Presenter例子和类库C#RTP_Presenter例子和类库
iSpring Presenter 将ppt转换为swf
《ActivePresenter:强大的屏幕录制与编辑工具》 ActivePresenter,正如其名,是一款专为屏幕录制和演示制作而设计的专业软件。它以其丰富的功能、易用的界面和对个人及非商业用户的免费政策,赢得了“善用佳软”的...
MVP(Model-View-Presenter)设计模式是Android应用开发中常用的一种架构模式,旨在分离业务逻辑、界面显示和数据处理,提高代码的可测试性和可维护性。MVP模式在Android开发中的应用,能够帮助开发者更好地组织代码...