Monday 27 May 2013

mvc in android example

mvc in android example : I like very much from Josh 's example. So, I summary in this blog to easier to understand.

1) MVC General Concepts:
To quickly review, MVC is a compound pattern meaning several patterns comprise this architectural pattern.
The Model represents our application’s state. Remember, objects have state (properties) and behaviors (methods). And so applications have state (the model). In essence, models are just big name-value objects that dispatch events when its state changes.
Views are the things you see and interact with. They bind to the model by registering themselves as observers.When the model changes, the  view is notified and it updates itself.

When a user interacts with the view, it sends events to a Controller. The controller is responsible for handling the input logic. It interprets the user gestures, updates the model, and may send messages back to the view. Below is a typical MVC diagram showing this relationship.



2) Organize packages:


activities – Should be should be obvious, we’ll put all our activities here.
controllers – Controllers that are the brain for our Views here.
daos – We’ll put our persistence logic here.
lists – This one I’m not incredibly found of, but I put all my custom list adapters here. I’m still experimenting.
models – These are the models are Views will bind to.
utils – All of my static classes and utilities go in here.
vos – I put all of my Value Objects also know as POJOs or Data Transfer Objects here.
widgets – This is borrowed from the Android convention. I put custom widgets controls in here.


3) Explain code

 
CODE: http://www.therealjoshua.com/wp-content/uploads/2011/11/TapCounter.zip 

MODEL:

CounterVo.java

SimpleObservable.java

So notice that our model really only does two things:
  1. It holds state. The properties id, label, count, and locked represent the object. It’s just a big name-value object: a Plain Old Java Object (POJO).
  2. It notifies its observers when properties have changed. The workhorse of this done in the subclass SimpleObservable. (Side note: Instead of using Java’s Observable, class which doesn’t have an interface and forces you to subclasses, I made my own Observable, which has an interface. This creates the option to subclass instead of being forced. Favor composition over inheritance. Bad Java, bad.)

VIEW: is activity package:
1) binding to the model.
2) sending messages to the controller.
3) handling messages from the controller.
Role 1, Data Binding:
Let’s look at TapActivity and let’s bind some data.
@Override
    public void onCreate(Bundle savedInstanceState) {


  // MODEL (Vos package)
        counter = new CounterVo();
        counter.addListener(this);
       
        // CONTROLLER
        controller = new TapController(counter);

/*
     * when MODEL change  onChange will be called.
     * */
    @Override
    public void onChange(CounterVo counter) {

        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                updateView();
            }
        });
    }



Our Activity is registered as an observer and whenever our model is updated we know our UI is going to stay right in-sync.

Role 2, Sending Messages:
 TapActivity
 plusBtn.setOnClickListener(new View.OnClickListener() {
 minusBtn.setOnClickListener(new View.OnClickListener() { 

when PlusBtn is clicked.

controller.handleMessage(TapController.MESSAGE_INCREMENT_COUNT);
 
then MODEL CounterVo change ===> onChange will be called in TapActivity.

to update UI.
Role 3: Handling Messages
 check out TapListActivity

http://www.therealjoshua.com/2011/11/android-architecture-part-6-putting-it-together-2/

 






3 comments:

  1. Hi,

    Could you explain the Goal of this application ?
    Why an Counter table is CREATED and where this table is physically stored ?

    Regards

    ReplyDelete