Patterns

There are many ways to write code some are better than others, using Patterns will help you develop better re-usable, maintainable and testable code.

There are many differing types of design patterns, some of the most popular architectural patterns these days are MVC – used for web development in C#,  MVP – the pattern I use the most, MVVP and I could go on….. If you are interested in Patterns I recommend a book called Design Patterns by the GoF for inexperienced developers in my teams it is recommended reading.

For now I will describe the advantages of using MVP or the Model View Presenter pattern.

Model View Presenter (MVP) – in an Android world

Split the development layers into 3 levels.

View – Activity

Model – Access to data i.e. SQL or a store

Presenter – Presents the view data to the model and the model data to the view.

How do these all work together?

Interfaces

One of the advantages of coding in Java is it gives you Interfaces,  an interface is a contract between classes i.e. I guarantee to support a set of methods. Now the good thing about interfaces is you, as a user of the interfaces, do not care how they are implemented. i.e. if I was writing a Morse Code application I could send Morse Code via sound, light or vibration. I could define an interface as below.

package Morse;

/**
 * Created by Lee on 20/01/2015.
 */
public interface IMorse
{
    void play(double duration);
    void cancel();
}

This interface is applicable no matter if I send Morse Code via light, vibration or sound. They all need to play for a duration and they all need to cancel the current activity. Now obviously the code behind each concrete example is different. But, each implementation guarantees to have this functionality if it implements the IMorse interface. So controller code can quite happily be simplified to just call these methods without going through the rigmarole of trying to detect if the methods exist in the class they are using.

Advantage Interfaces

As they say in Tennis, if you haven’t guessed by now by using interfaces you can also substitute a dummy interface for unit testing. A dummy interface can check if “play” has been called with the right parameters or you could make “play” fail to test your error handling etc. If you want to go down the Dependency Injection (DI) route then you NEED to use interfaces to allow this. DI will be a new post entirely.

Back To MVP

Ok, here is a contrived example showing the usage of MVP, this will not compile so don’t even try, the example is meant to be conceptual.

public interface IMyView
{
	ViewData getViewData();
	setViewData(ViewData);
}

class MyView implements IMyView{
	MyViewPresenter mMViewPresenter;

	public ViewData getViewData();
	{
		ViewData vData = new ViewData();
		vData.field1 = (EditText) findViewById(R.id.field1);
		vData.field2 = (EditText) findViewById(R.id.field2);
		return vData;
	}
	public setViewData(ViewData vData)
	{
		field1.setText(vData.field1);
		field2.setTExt(vData.field2);
	}

	@Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sample);
		mMyViewPresenter = new MyViewPresente()this);
		updateButton = (Button) findViewById(R.id.updateButton);
        updateButton.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View arg0)
            {
                mMViewPresenter.updateDatabase();
            }
        });
	}
}

class MyModel{
	Boolean updateDatabase(ViewData vData)
	{
		// perform database update
	}

	ViewData getData()
	{
		// retrieve data from database
		return vData;
	}
}

class MyViewPresenter {

	// declares an instance of the Interface
	IMyView view;

	MyViewPresenter(IMyView view)
	{
		this.view = view;
	}

	public updateDatabase()
	{
		ViewData vData = view.getViewData();
		MyModel mModel = new MyModel();
		mModel.updateDatabase(vData);
		ViewData updatedData = mModel.getData();
		view.setViewData(updatedData);
	}

}

Advantage MVP

By looking at the above code you should be able to see a huge benefit by using the MVP pattern. The Activity needs to know nothing about updating the database. The presenter controls the database updates by getting the data it needs from the View. The presenter then updates the model and then updates the view with new data retrieved from the model, all via the beauty of Interfaces. Again, when unit testing, you don’t need to have an Activity to test the presenter you just need a test class that implements the IMyView interface.