2012-08-14 111 views
0

我试图让我的应用程序与PM设计(MVC +演示模型),但我已经坚持如何巧妙地包装模型类在演示模型类。现在,我写了一个简单的代码,其中根据Model类的实例中的值来更改图片和文本。Android演示文稿模型:如何在演示模型中包装模型?

// Disclaimer: 
// View and Controller are merged in this sample for clarity's sake. 

枚举

MVC
Enum 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变量,甚至与嵌套switchif ... else,我不能想出所有代码...

回答

1

嗯,你可能想告诉你的听众有什么改变。例如,如果模型中的名称字段已更改,请致电notifyObservers(update, PROPERTY_NAME)。然后,表示模型只需要逻辑来处理名称更改。

也就是说,我不推荐在没有框架的情况下使用Presentation Model。发布事件和正确移动数据需要太多的复杂性和代码。实际上,即使有框架,也有一个重要的学习曲线 - 但我认为这对大型项目来说是一个很好的架构。

JGoodies Binding是表现模型框架的一个很好的例子。但是,它的目标是Swing应用程序。它可以适应Android的一些努力,但我会看看是否存在一个好的Android特定的框架。

2

正如Peter指出的,没有框架,在android应用程序中应用演示模型模式将会有很多工作。 JGoodies Binding是Java Swing的框架。我知道我迟到了。但对于其他人或您未来的项目,您可能会感兴趣。我们的开源项目Robobinding是Android平台的数据绑定演示模型框架。当我们将MVC/MVVM/Presentation Model应用到android应用程序时,我们真正想要的是拥有一个清晰的结构化项目,更重要的是单元测试更容易。目前,如果没有第三方框架,通常会有很多代码(如addXXListener(),findViewById()...),它不会增加任何业务价值。更重要的是,你必须运行android单元测试,而不是正常的JUnit测试,这需要很长时间才能运行,并使单元测试有点不切实际。由于这些原因,几年前我们开始了RoboBinding。 RoboBinding可帮助您编写易于阅读,测试和维护的UI代码。 RoboBinding删除的像addXXListener左右不必要代码的需要,并转移UI逻辑来呈现模型,这是一个POJO并且可以经由正常JUnit测试进行测试。 RoboBinding本身带有超过300个JUnit测试来确保其质量。其他选择:Android绑定,Bindroid和MvvmCross。

+0

如何将模型绑定到演示模型? RoboBinding将展示模型绑定到视图AFAIK。因此对模型所做的更改会传播到演示文稿模型。 – 2014-12-03 13:42:03