Presentation Model Presenter (PMP)

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 .

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值