我试图让我的应用程序与PM设计(MVC +演示模型),但我已经坚持如何巧妙地包装模型类在演示模型类。现在,我写了一个简单的代码,其中根据Model类的实例中的值来更改图片和文本。Android演示文稿模型:如何在演示模型中包装模型?
// Disclaimer:
// View and Controller are merged in this sample for clarity's sake.
枚举
MVCEnum AnimalSpecies {
Dog, Cat, Rabbit, Bird,
}
M的+ RM
class Model extends Observable {
// in my actual code Model has 10+ member variables and most of them are Enum
protected AnimalSpecies species;
protected String name;
protected Object update;
public void setSpecies (AnimalSpecies species) {
this.species = species;
notifyUpdate(species);
}
public void setName (String s) {
this.name = s;
notifyUpdate(name);
}
public void notifyUpdate(Object o) {
this.update = o;
this.setChanged();
this.notifyObservers(update);
}
}
MVC + RM的RM
class PresentationModel extends Observable implements Observer {
@Override
public void update(Observable model, Object data) {
// Called when notified by Model
// No idea what to write... but what I want to do is,
// a) determine what text for View to display
// b) determine what pics for View to display,
// based on values of Model.
this.setChanged();
this.notifyObservers(update);
}
}
MVC的VC + RM
class View extends Activity implements Observer {
// This is View + Controller, so it'd implement some interfaces like onClickListener,
// and in events such as onClick(), values of Model class are changed,
// but for clarity's sake, I keep everything in onCreate() event.
TextView header;
TextView footer
ImageView imgview;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
header = (TextView) findViewById(R.id.header);
footer = (TextView) findViewById(R.id.footer);
imgview = (ImageView) findViewById(R.id.imgview);
Model model = new Model();
PresentationModel pm = new PresentationModel();
model.addObserver(pm);
pm.addObserver(this);
model.setSpecies(AnimalSpecies.Cat);
model.setName("Max");
}
@Override
public void update(Observable pm, Object data) {
// Called when notified by PresentationModel
// *** varies based on parameters from PresentationModel
header.setText(***);
footer.setText(***);
imgview.setImageResource(R.drawable.***);
}
}
我的问题:如何编写PresentationModel
类的public void update()
逻辑?我可以从NotifyObserver()
得到的只有一个Object
变量,甚至与嵌套switch
或if
... else
,我不能想出所有代码...
如何将模型绑定到演示模型? RoboBinding将展示模型绑定到视图AFAIK。因此对模型所做的更改会传播到演示文稿模型。 – 2014-12-03 13:42:03