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 .