Hi !!! friends in this tutorial we will learn to implement Model View Controller.
- Model View Controller is a design pattern for android application .
- Model is what the application is.
- View is how the application works.
- Controller is what the application does.
- Communication between the Model and the View happens through the Controller by couple of other patterns called Target/Action and Delegation.
- The overall design pattern is the Model View Controller.
- So let us implement MVC :
- Follow the steps :
- Create a new project and name it as (for example) “ MVC ”
- Your MainActivity.java and activity_main.xml file will be generated.
- Similarly create two new java classes which are Model.Java and ModelDelegate.Java
- Now in your activity_main.xml file in the graphical layout Take the following :
- EdiText
- LargeText
- Button
- Set the text and onClick property of the button as stated :
- Also set the text property of the TextView as stated :
- Thus now your graphical layout will look like as shown :
- Thus ,this is the View.
- Now go to your MainActivity.java extend the class with the Activity and implementing the ModelDelegate class .Also declare Model as private class instantiating as mModel to a new Model the code for which is shown below.
- Now in the onCreate( ) method add a statement as
mModel.setDelegate(this); - The model has a delegate property and we are setting that delegate property to this instance of the
MainActivity . “ this “ is the object that represents the class MainActivity in our running program.
It will allow the model to talk to this MainActivity which is the controller of our application and the Model
is the Model.Java - So the Code for this is given below :
- Your window will Look like as shown below :
- Now go to your Model.Java class you have created.
- Take an integer Array of 5 integers , an integer called Index and set it to 0.
- delegate is of the type MainActivity , here it points to the MainActivity’s actual object.
- Now set the Delegate to obtain the object of the MainActivity and just calling it “ d ”.
“ d ” stands for delegate. - which is done by the code :
- Now write the code for calculating the Average of all the elements of the array :
- Now we don’t want to get the average until there are all five elements in the array.So we are going to collect integers in a interface in our View Passing through the controller through the Model in addToArray.
- So now go to your MainActivity.java file.when each integer is entered we are going to click on the
Button and which takes the number out of the EditText and Parses it out as a number and sets the EditText = null
And then calls addToArray ( ) on the Model. - The code on the button is :
- Now go to your Model.java class :
Write the code in the addToArray ( ) method Which adds the uder provided integers to the Array which is as follows : - Now ModelDelegate is the interface so set it .Go to your ModelDelegate.Java class and write the
following code : - Now when the index of the array is equal to 5 It will call the arrayIsFull( ) method so write the Code for the same method in the MainActivity that is the controller of our application.
- So the model gets the average and sets it to the TextView.
- So your MainActivity.Java file is shown below :
- Your Model.Java class is shown below :
- Your ModelDelegate.java class is shown below :
- Now run your MainActivity.Java file you will get :
- Now enter any one number and click on ENTER button ,Similarly enter four more numbers and you will get the Average of the five numbers you have entered.
So let’s enter 5 for five times and the average is :
[code language=”xml”]android : text=”ENTER”
android : onClick=”btnClicked”’
[/code]
[code language=”xml”]android : text = “ ” [/code]
[code language=”java”]public class MainActivity extends Activity implements ModelDelegate
{
private Model mModel = new Model();
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mModel.setDelegate(this);
}
}
[/code]
[code language=”java”]public int[] intArray = new int[5];
public int index = 0;
public MainActivity delegate = null;
[/code]
[code language=”java”]public void setDelegate(MainActivity d)
{
delegate = d;
}
[/code]
[code language=”java”]public float getAverage()
{
float average = 0;
int count =0;
for(int num : intArray)
{
average +=num;
count ++;
}
return average/count;
}
[/code]
[code language=”java”]public void btnClicked(View v)
{
EditText et= (EditText)findViewById(R.id.editText1);
int num=Integer.parseInt(et.getText( ).toString( ) );
et.setText("");
mModel.addToArray(num);
}
[/code]
[code language=”java”]public void addToArray(int element)
{
//go to the index and set the element to Array index and increment the index.
intArray[index]=element;
index++;
//check whether the index is equal to 5
if(index==5)
{
delegate.arrayIsFull();
index = 0;
}
}
[/code]
[code language=”java”]public interface ModelDelegate
{
public void arrayIsFull();
}
[/code]
[code language=”java”]public void arrayIsFull()
{
// TODO Auto-generated method stub
float avg = mModel.getAverage();
TextView tv = (TextView)findViewById(R.id.textView1);
tv.setText(String.format("%f", avg));
}
[/code]