MVC Architecture/Pattern
Hello friends, I am Pruthwiraj Ghadge and welcome back to my blog once again within Java Course In Pune. As times passes the technology changes and these days it has been a rapid changeover. When we talk about changes we must talk about efficiency, accuracy, and reliability of the code. Moreover, its been a very tough job to rewrite the whole thing again and again and hence reusability is one of the most important concerns today. Looking at the code from a developer’s point of view and minimizing the code and apparently, the efforts to write the code is the key to success today and learn it from Java Course In Pune.
Java is a vast Software Platform and most popular in development fraternity, so enhancement in this technology is at a quicker pace and much better as compared to other technologies. Since the evolvement of this language, there have been quite a lot of innovations in it. One of the most widespread components of technology is Design Patterns. Design patterns have taken a big leap in the technical domain in the last few years. As technology grows it becomes tedious to manage all aspects of it. Design patterns help maintain these parts easily and make technology more manageable. Although design patterns are pretty confusing, they make our tasks more flexible, reusable and manageable. Java Classes In Pune are going to talk about one of the design patterns today, MVC architecture.
MVC stands for Model-View-Controller architecture. This is the most used and popular design pattern nowadays. It is a three-tier design pattern. Initially when the software applications were designed all the things used to be on the same machine. That means a single machine used to have frontend, backend, and processing code. Gradually these things changed and a tiered approach was introduced where a certain part of code is separated and written independently. Rather the code is divided on to different machines or maybe different layers. The approach till then was a 1-tier approach where everything used to be on the same layer.
A 1-tier architecture comprises all the elements of application including visual end application, database application, the logic that modifies or interacts with visual and database applications. These kind of applications are generally the stand-alone applications, where they have no internet or network connectivity and machines which are needed to do all the processing, storing and displaying of the data. However such machines have a very high working and processing load which could lead to degrading performance.
In another approach, the database and the application layer are separated from each other. This is known as the 2-tier approach. However, this approach seems to be a better solution to the previous 1-tier approach, we still face architectural and functional intricacies. As all the processing and visual elements are still in one place, the machine has a high processing overhead. As a result of which, even though the database has moved to another machine, still there is too much load on the machine.
Apart from this, there are many other things we face when we use a 1-tier or 2-tier approach. As visual, processing and modeling parts are together it’s difficult to debug the code. Also separating these individual elements is tough. If we need any of these elements in other applications we have to make hard efforts to separate them.
For Free, Demo classes Call: 8237077325
Registration Link: Click Here!
These problems can be solved by the 3-tier architecture i.e. MVC architecture. We part all the units of the application and keep them separate so that they can be used anytime, anywhere. Also, they can be modified individually and debugging becomes easy too.
Now let’s take a look at the MVC architecture in brief;
MVC stands for Model View Controller. Each of these represents one aspect of the application. Model – represents the POJO bean or the database units, View represents the HTML or JSP, the visual units and Controller represents the Servlets or the controller units.
When we write the application in a 1-tier or 2-tier type, all these units are generally written in one single place together. However, in a 3-tier approach, these units are separated. It makes the application more flexible, reusable and manageable.
Following diagram shows how MVC pattern is implemented;
The architecture defines each part separately as shown above. The database is kept in a different machine and the client is accessing the application from a different machine. The application is stored in a different machine where all the components are separated and can be used individually to perform different activities.
When we use this pattern in Java we make use of different classes for different purposes. A POJO bean class is created as a Model, a plain Java class can be created as View and one more class can be written as Controller which is responsible for calling the methods.
Let us take an example, how MVC can be used in Java.
Let’s take a class Account that contains some data members as accountNumber, balance, accountType. The POJO bean class will contain the getter and setter methods along with the constructors of the bean class. The view class will contain the method to display the contents of the Account class. And the controller class will decide on how to perform the operations.
Class Account.java
public class Account
{
private int accountNumber;
private double balance;
private String accountType;
public Account(){
}
public int getAccountNumber(){
return this.accountNumber;
}
public int getBalance(){
return this.balance;
}
public int getAccountType(){
return this.accountType;
}
public void setAccountNumber(int accountNumber){
this.accountNumber = accountNumber;
}
public void setBalance(double balance){
this.balance = balance;
}
public void getAccountType(String accountType){
this.accountType = accountType;
}
}
For Free, Demo classes Call: 8237077325
Registration Link: Click Here!
Class AccountView.java
public class AccountView{
public void displayAccount(int accountNumber, double balance, String accountType){
System.out.println(“*=*=*=*=*=*=*Account Details*=*=*=*=*=*=*”);
System.out.println(“Account Number : ” + accountNumber);
System.out.println(“Account Balance : ” + balance);
System.out.println(“Account Type : ” + accountType);
}
}
Class AccountController.java
public class AccountController{
Account model;
AccountView view;
public AccountController(Account model, AccountView view){
this.model = model;
this.view = view;
}
public void setModelAccountNumber(int accountNumber){
model.setAccountNumber(accountNumber);
}
public void setModelBalance(double balance){
model.setBalance(balance);
}
public void setModelAccountType(String accountType){
model.setAccountType(accountType);
}
public int getModelAccountNumber(){
return model.getAccountNumber();
}
public double getModelBalance(){
return model.getBalance();
}
public String getModelAccountType(){
return model.getAccountType();
}
public void updateView(int accountNumber, double balance, String accountType) {
view.displayAccount(accountNumber, balance, accountType);
}
}
Class MVCPatternDemo.java
public class MVCPatternDemo{
public static void main(String args[]){
Student model = retrieveAccountDetailsFromDatabase();
AccountView view = new AccountView();
AccountController controller = new AccountController(model, view);
controller.updateView();
controller.setModelBalance(35000.0);
controller.updateView();
}
public static Account retrieveAccountDetailsFromDatabase(){
Account account = new Account();
account.setAccountNumber(101);
account.setBalance(25000.0);
account.setAccountType(“Savings”);
return account;
}
}
Verify the Output
*=*=*=*=*=*=*Account Details*=*=*=*=*=*=*
Account Number : 101
Account Balance : 25000.0
Account Type : Savings
*=*=*=*=*=*=*Account Details*=*=*=*=*=*=*
Account Number : 101
Account Balance : 35000.0
Account Type : Savings
The above code when executed gives the output as shown.
The MVC pattern simplifies the coding and makes it easier to manage. As we can see the entire code has become more manageable and flexible. It can also be plugged into any other code easily. Now, let’s talk about web application and MVC architecture.
The way we separated the logic, view, and model in the stand-alone Java application, we separate them in the web application too. In a web application, these are known as components and they all reside inside a server – Web Server. The diagram shown above depicts the MVC pattern in Web Application. In a web application, a client requests a resource to a server and the server responds to the client with the specified resource if available. Here the resources are nothing but HTML/JSP pages, servlets or other resources, which are termed as components in Web Application. The way separated the data, logic, and view in java application we separate them here also. We create models using POJO bean classes, views using HTML or JSP pages and Servlets act as controllers.
In a web application generally, the client and server are distant and they communicate with the help of request and response. The client requests a resource and the server responds to the request with the specified resource if available. Mostly the communication happens with HTTP protocol. The client normally has a web browser and the server contains the resources in a web container. The resources could be servlets, HTML/JSP pages, filters or any other things that can be used as a resource. When the client requests a resource and server checks for the availability of it. If it is available, it serves the client with the resource. In the web application, HTML/JSP pages work as views and they are sent as responses to clients. The servlets are the controllers and they also create dynamic contents based on the requirement and send them to webpages. Java beans/POJOs are treated as models and they basically manage all modifications and transactions. Controllers are the mediators between the models and views which ultimately modify the database based on user inputs and the actions being performed. As the data which is being stored in POJOs, the visual units and the processing code is separated it becomes very easy to maintain the code and also manage the entire application easily.
So that’s all from my side today. Meet you soon with another blog and another topic at Java Training In Pune. Till then keep coding.
Happy coding…!!!
Author:
Ghadge, Pruthwiraj | SevenMentor Pvt. Ltd.
For Free, Demo classes Call: 8237077325
Registration Link: Click Here!
Call the Trainer and Book your free demo class for JAVA now!!!
© Copyright 2019 | Sevenmentor Pvt Ltd.