+ 2
What is MVC?how to implement MVC pattern in Java ?
4 odpowiedzi
+ 5
Why no one is concerned about this tech ?
+ 5
I am learning Design Pattern, I want to implement MVC on my Android App.
+ 2
Part2
Notice that when a Student object is created it is instantiated
by the College object.
Put simply it means that whatever is using a class and its methods
(in this case the College class) should know as little as possible
about the inner workings of that class.
In its simplest terms this is because if something is “encapsulated”
you need to know less things about it to be able to use it.
To add a Student to our system as it is we do the following
College c = new College(“MIT”, “massachusetts”);
c.addStudent(“Steve”, 21);
In our MVC code the three layers should know as little about each other
as possible.
For example, the controller should ideally only know about the top level
model class (in this case the College) and as many of the other classes
within the model should be “hidden” from the controller.
+ 2
part 1
Model View Controller or MVC is a way to model data,When we model data in Java (and other object-oriented languages) we are introducing the concept of a class BEING SOMETHING
rather than just DOING SOMETHING.
when we had alot of primitive variables which were storing related data
(e.g. Rainfall amounts for each month in the last 10 years - 120 int variables!).
● In that scenario “Declaring hundreds or thousands of variables is time
consuming and impractical.”
● As we begin to build a system which is structured into different
layers we will soon come to a point where we don’t want all our
classes in one directory.
● In Java the concept of splitting classes into different directories is
done using packages.
● We create a directory for each package within our Java project
directory
● Packages can be many layers deep (like
directories)
(Within each directory we place a class which we want to belong to that package.
We MUST mark that class as belonging to that package at the top of each class using
the keyword package)
When classes are in separate packages a class can no longer
“see” a class which is not in its immediate package.
● This means that a class needs to import any classes which it
wishes to use from outside its own package
In our MVC code the three layers should know as little about each other
as possible. But what exactly does this mean ?
public class College
{
private String name;
private String address;
private Student[] students;
private int noOfStudentsAdded;
private static final int MAX_STUDENTS = 10;
public College(String name, String address)
{
this.name = name;
this.age = age;
this.numberOfStudentsAdded = 0;
this.students = new Student[MAX_STUDENTS ];
}
}
//adding a student to the college
theCollege.addStudent(“ steve wozniak”, 21);
//method for adding students
public void addStudent(String name, int age)
{
//First create the instance of the Student object
Student s = new Student(name, age);
this.students[noOfStudentsAdded] = s;
}