ervinne

MVC with IoC Sample

Mar 6th, 2015
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.62 KB | None | 0 0
  1. /**
  2.  * Sample only for ate charms ^_^
  3.  */
  4. public class SomeController implements SomeViewActionListener, SomeModelEventListener {
  5.    
  6.     private SomeModel someModel;
  7.     private SomeView someView;
  8.  
  9.     public SomeController() {
  10.         someModel = new SomeModel(this);
  11.         someView = new SomeView (this);
  12.     }
  13.  
  14.     @Override
  15.     public void onDownloadSomethingCommand() {
  16.         someModel.downloadSomething();
  17.     }
  18.  
  19.     @Override
  20.     public void onDownloadDone(Content content) {
  21.         someView.displaySomeContent(content);
  22.     }
  23.  
  24. }
  25.  
  26. public class SomeModel {
  27.  
  28.     private SomeModelEventListener eventListener;
  29.  
  30.     public SomeModel(SomeModelEventListener eventListener) {
  31.         this.eventListener = eventListener;
  32.     }
  33.  
  34.     public void downloadSomething() {
  35.         // ... download on a different thread, since this is a different thread, a return is not possible
  36.         // or else the UI will be blocked
  37.  
  38.         // .. when done: where content is the content downloaded
  39.         eventListener.onDownloadDone(content);
  40.     }
  41.  
  42. }
  43.  
  44. public class SomeView() {
  45.  
  46.     private Button bDownloadSomethingButton;
  47.     private SomeViewActionListener actionListener;
  48.  
  49.     public SomeView(SomeViewActionListener actionListener) {
  50.         this.actionListener = actionListener;
  51.  
  52.         // This is inversion of control as well
  53.         bDownloadSomethingButton.setOnClickListener(new OnCLickListener() {
  54.             actionListener.onDownloadSomethingCommand();
  55.         });
  56.  
  57.     }
  58.  
  59.     public void displaySomeContent(Content content) {
  60.         // display content here
  61.     }
  62.  
  63. }
  64.  
  65. public interface SomeViewActionListener {
  66.     public void onDownloadSomethingCommand();
  67. }
  68.  
  69. public interface SomeModelEventListener {
  70.     public void onDownloadDone(Content content);
  71. }
Advertisement
Add Comment
Please, Sign In to add comment