mercredi 8 avril 2015

Can a model return its own View?

I'm developing a application where a list has to display different implementations of the same interface (for example, TextImplementation, VideoImplementation, AudioImplementation e.t.c). In this case, would it be good design to have the model return its view?


Here is an example of some code that demonstrates what I have in mind. Note that the Model doesn't actually design the view, it merely returns an instance of it.



public interface Impl{
public Object getQuestion();
public String getAnswer();
public View getListCellView();
}

public class AudioImpl implements Impl{

private File audioFile;
private String answer;

public Audio getQuestion(){

Audio a = Audio.fromFile(audioFile);
return a;

}

public String getAnswer(){

return answer;

}

public View getListCellView(){

return new AudioListCellView(audioFile);
}
}

public class AudioListCellView extends View{

private File file;

public AudioListCellView(File audioFile){

this.file = audioFile;

}

public View createView(){

AudioPlayer ap = new AudioPlayer();
ap.addOnClickListener(new OnClickListener(){

public void onClick(ClickEvent e){

play(this.file);

}

});

this.addView(ap,Layout.CENTER);

}

}


The reason I'm considering this design is because when rendering the list items, I can simply write something like this:



class QuestionsListView extends ListView{

private Collection<Impl> list;

public View getCellView(int position){

Impl anImpl = list.get(position);
return anImpl.getListCellView();
}

}


Are there downsides to this design? How can it be improved?


Aucun commentaire:

Enregistrer un commentaire