开发者

How to separate the model from the view?

I have a bunch of model objects.

These objects end up being rendered as views (say forms) in a rich client app.

I started to annotate the fields in the model objects (Java annotations) with things that let me render them as forms on the fly (e.g displayname, group, page, validvalues).

I now realise that the view has crept into the model.

How should I seperate the view logic out of the model objects?

TECH: Java, Java Annotations, Eclipse RCP

EDIT:My question is theoretic, but I would also like some concrete (i开发者_开发百科mplementation) advice.


At the risk of stating the obvious, what you need to do is store the display-related information somewhere else. Don't put the page in the model code - create an object for the interface, have it contain page objects, and make each page know what values it displays. This may require a certain amount of refactoring.

Having said that, not everything you mention is 'view'. Valid values for a field is part of the logic of the field; it should be considered part of the model, not the view. Likewise if 'group' is a logical grouping, rather than about placement in the interface, it might be considered part of the model.


You could replace the annotations:

@DisplayName("My Fancy Name") 
@DisplayGroup("My Fancy Group") 
public String myProperty;

by a separate descriptor class:

Descriptor desc = new Descriptor(MyClass.class, "myProperty");
desc.setDisplayName("My Fancy Name");
desc.setDisplayGroup("My Fancy Group");

You have clean separation of concerns, but you loose compile time safety (in Java, because Java does not have property references).


You could look at the MVC pattern and introduce a controller into the mix to provide the communication between the model and the view.

This prevents the view creeping into the model as the view never talks to the model it only talks to the controller which is responsible for all interaction between the model and the view


If I get you right - you use the model classes as a (static) model to create (part of) the view? Why not - in your case, the model classes (with annotations) are one model, the objects another one.

As long as the annotations just give hints (like @Textfield) I don't see a problem. If the model already contains references to view objects (like a reference to a Textfield), then there's need for refactoring. Easiest would be to move the model classes in a separate plugin and do not add any *.ui type and view plugins as dependencies. Then fix the errors ;)

... and have a look at jface databinding! Very useful in a MVC/MVP architecture!


The view should have a reference to the model, but the model should not have a reference to the view.

The view can write to the model.

The view listens for events on the model, such as a property change, or collection change.

The view then updates itself when the model changes.

You should definitely not have methods on a model that render the model as a view.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜