Introduction
When I was trying to implement MVC for Android Application,
the concept of “Observer” really attracts me. It really put me in a situation,
where, I have to think, should I go for MVC or MVPC. Because, I think, MVPC would be more
organized and maintainable way for this Android application. Along with this thinking, possibility of
total segregation of event listeners in code, make me think about MVPC for this
project seriously.
[Note:
Observer
is the interface to be implemented by objects that receive notification of
updates on an Observable
object. (Observer
Triggers, when Observable
notifies any
changes on it)]
Why MVPC?
Model-View-Presenter-Controller. In MVC, View
notifies Controller, Controller notifies Model, and Model notifies View. It is
really very compact and smart enough Architecture. But, Presenter is another
useful architecture entity. It will give another level of abstraction to the
project between View and Controller. This will make the view much independent
and make controller more flexible. With
this architecture, we can replace the existing view with a new one, so smartly,
that we may have to do no changes in Controller. Do we really need MVPC in Android Application?
We may not need MVPC for today or tomorrow, but day after tomorrow. As year goes, a View of an application becomes monotonous to the user, or else, we may need to implement different views (Theme) to the project. To do all the above, we really don’t want to hamper any application flow. MVPC will be one of the best fit architecture for this.
Code Example-
In MainActivity.java-
public class MainActivity extends Activity {
public User mUser;
UserController
controller;
UserPresenter
presenter;
@Override
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mUser= new User();
controller= new UserController(this);
mUser.addObserver(controller);
presenter= new UserPresenter(mUser);
Button
test_btn=(Button) findViewById(R.id.btn_Test);
Button
test_btn2=(Button) findViewById(R.id.btn_Test2);
test_btn.setOnClickListener(presenter);
test_btn2.setOnClickListener(presenter);
}
In UserPresenter.java-
public class UserPresenter implements OnClickListener {
private User _model;
public UserPresenter(User
model)
{
_model=model;
}
@Override
public void onClick(View v) {
switch(v.getId())
{
case R.id.btn_Test:
_model.setCount();
break;
case R.id.btn_Test2:
_model.setCount();
_model.setCount();
break;
}
}
}
In UserModel.java-
public class User extends Observable {
public int count=0;
public User(){
}
public void setCount()
{
count++;
setChanged();
notifyObservers();
}
}
In UserController.java-
public class UserController implements Observer {
public User mUser;
private Activity _act;
public
UserController(Activity act )
{
_act=act;
}
@Override
public void update(Observable
observable, Object data) {
TextView
lbl= (TextView)_act.findViewById(R.id.lbl_Text);
lbl.setText(""+((MainActivity)_act).mUser.getCount());
}
}
3 comments:
very nice dude....
i have also found one good example here
Use MVC Pattern To Create Very Basic Shopping Cart - Android Example
very nice dude....
i have also found one good example here
Use MVC Pattern To Create Very Basic Shopping Cart - Android Example
superb big brother........!!!!!!!
Post a Comment