Created
November 10, 2018 03:28
-
-
Save adammb86/109ab9899a537477248363326ffbd3ad to your computer and use it in GitHub Desktop.
Design Pattern - MVC - Sample Case
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(MVC) Langkah pembuatan : | |
1. Student.java | |
2. StudentView.java | |
3. StudentController.java | |
4. MVCPatternDemo.java |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MVCPatternDemo { | |
public static void main(String[] args) { | |
// Fetch student record based on his roll number from the database | |
Student model = retrieveStudentFromDatabase(); | |
StudentView view = new StudentView(); | |
StudentController controller = new StudentController(model, view); | |
controller.updateView(); | |
controller.setStudentName("Suyatna"); | |
controller.updateView(); | |
} | |
private static Student retrieveStudentFromDatabase(){ | |
return new Student("1","Daniyal"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Student { | |
private String rollNo; | |
private String name; | |
public Student(String rollNo, String name) { | |
this.rollNo = rollNo; | |
this.name = name; | |
} | |
public String getRollNo() { | |
return rollNo; | |
} | |
public void setRollNo(String rollNo) { | |
this.rollNo = rollNo; | |
} | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class StudentController { | |
private Student model; | |
private StudentView view; | |
public StudentController(Student model, StudentView view) { | |
this.model = model; | |
this.view = view; | |
} | |
public void setStudentName(String name){ | |
model.setName(name); | |
} | |
public void setStudentRollNo(String rollNo){ | |
model.setRollNo(rollNo); | |
} | |
public String getStudentName(){ | |
return model.getName(); | |
} | |
public String getStudentRollNo(){ | |
return model.getRollNo(); | |
} | |
public void updateView(){ | |
view.printStudentDetails(model.getName(), model.getRollNo()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class StudentView { | |
public void printStudentDetails(String studentName, String studentRollNo){ | |
System.out.println("Student"); | |
System.out.println("Name : "+studentName); | |
System.out.println("Roll No. : "+studentRollNo); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment