Skip to content

williamniemiec/MVC-in-Java

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

32 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MVC in Java

This project aims to provide an MVC Java framework for you to use in your projects. If you want to see an example there is a simple and complete example about a scheduler in example folder.


What is MVC?

Briefly, MVC (Model View Controller) is a design pattern widely used in projects because it leaves the project structured in order to facilitate the identification of application modules, understand how it is structured, in addition to facilitating maintenance. It structures the project in three modules:

Name Funcion
Models Responsible for business logic
View Responsible for the visual part
Controllers Responsible for the behavior of the visual part

This project is based on book Head First design patterns. Below are some illustrations from this book about how MVC works. generalView1 generalView2

Nomenclature

This project adopted the following naming pattern:

Type Name
Controller <ControllerName>Controller
View <ViewName>View

Note: This is just an adopted standard; if you do not like it, you can use another one.

How to use this structure in my project?

In src folder is all what you need to apply this pattern in your project.

How to create new models, views and controllers?

Models

Models should implement Model interface. This interface is like an observable and has three methods that must be implemented: "attach", "detach" and "notifyViews".

Views

Views should implement View interface. This interface just have one method that must be implemented: "update".

Controllers

Controllers should extend Controller class. This class has one method that must be implemented: "run".

Application execution

To execute the application just run Main class. It will execute HomeController (that will execute HomeView).

Classes and interfaces

core

Name Type Description
Controller Abstract class Contains the main frame of the application. All controllers must extend this
Model Interface Has behavior of a Observator. All model classes should implement this interface
View Interface Has behavior of a Observer. All view classes should implement this interface

controllers

Name Type Description
HomeController Class It will be responsible for HomeView behavior.

models

Name Type Description
Model1 Class Example model

views

Name Type Description
HomeView Class Responsible for the look of application's main screen

Project organization

The MVC structure is in src folder. In it, there are three folders and one files.

/

Name Type Function
bin Directory Binary files (.class files)
example Directory Example of a project using mvc
media Directory Visual information
src Directory MVC framework
.classpath File IDE generated file
.project File IDE generated file

/src

Name Type Function
assets Directory Contains all application content files
controllers Directory Contains all application controller classes
core Directory Contains classes and interfaces essential for the functioning of the MVC structure
models Directory Contains all application model classes
views Directory Contains all application view classes
Main.java File Class responsible for starting the application

Example

To be clear how the framework works I made a small application that consists of a scheduler, being possible to create new events and see the ones already created. It is located in example folder.

References

  • Freeman, Eric, Elisabeth Robson, Kathy Sierra, and Bert Bates. 2004. Head First design patterns. Sebastopol, CA: O'Reilly.