Saturday, September 12, 2015

Learn MVC Architecture with Simple Java Example

Model–view–controller (MVC) is a software architectural pattern for implementing user interfaces. It divides a given software application into three interconnected parts, so as to separate internal representations of information from the ways that information is presented to or accepted from the user.

Now I take simple java example to understand MVC Architecture.

Go to Netbeans and create New Java Application Project.

Create New Java Class Called "".I create Apple model inside it and create constructors and two methods to incrementApple and getvalueApple.

So model represents an application’s data and contains the logic for accessing and manipulating that data.

Then create New Java Class Called "".I create simple JFrame and implement JButton and Jlabel inside it.You can see code and Output below.

so the presentation semantics are encapsulated within the view.

Then Controller is most important part.I want to do when i press this button in View Apple value should increment by 1 and display in that logic should implement inside controller.It connect Model and View.

Create ActionListner and with in that, create method LinkBandL( ) to increment apple value and display in label.

so The controller is responsible for intercepting and translating user input into actions to be performed by the model.

then Create Main class to Run this Application

final Output: 

 Click Button Apple value increment by 1 and display in label. 

1 comment: