博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
singleton pattern & MVC pattern
阅读量:6346 次
发布时间:2019-06-22

本文共 3924 字,大约阅读时间需要 13 分钟。

Singleton Pattern

This pattern involves a single class which is responsible to create an object while making sure that only single object gets created. This class provides a way to access its only object which can be accessed directly without need to instantiate the object of the class.

public class SingleObject {   //create an object of SingleObject   private static SingleObject instance = new SingleObject();   //make the constructor private so that this class cannot be   //instantiated   private SingleObject(){}   //Get the only object available   public static SingleObject getInstance(){      return instance;   }   public void showMessage(){      System.out.println("Hello World!");   }}
public class SingletonPatternDemo {   public static void main(String[] args) {      //illegal construct      //Compile Time Error: The constructor SingleObject() is not visible      //SingleObject object = new SingleObject();      //Get the only object available      SingleObject object = SingleObject.getInstance();      //show the message      object.showMessage();   }}

MVC Pattern

MVC Pattern stands for Model-View-Controller Pattern. This pattern is used to separate application's concerns.

Model - Model represents an object or JAVA POJO carrying data. It can also have logic to update controller if its data changes.

View - View represents the visualization of the data that model

contains.

Controller - Controller acts on both model and view. It controls the data flow into model object and updates the view whenever data changes. It keeps view and model separate.

We are going to create a Student object acting as a model.StudentView will be a view class which can print student details on console and StudentController is the controller class responsible to store data in Student object and update view StudentView accordingly.

Create Model.

Student.java

public class Student {   private String rollNo;   private String 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;   }}

Create View.

StudentView.java

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);   }}

Create Controller.

StudentController.java

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 String getStudentName(){      return model.getName();           }   public void setStudentRollNo(String rollNo){      model.setRollNo(rollNo);           }   public String getStudentRollNo(){      return model.getRollNo();           }   public void updateView(){                      view.printStudentDetails(model.getName(), model.getRollNo());   }    }

Use the StudentController methods to demonstrate MVC design pattern usage.

MVCPatternDemo.java

public class MVCPatternDemo {   public static void main(String[] args) {      //fetch student record based on his roll no from the database      Student model  = retriveStudentFromDatabase();      //Create a view : to write student details on console      StudentView view = new StudentView();      StudentController controller = new StudentController(model, view);      controller.updateView();      //update model data      controller.setStudentName("John");      controller.updateView();   }   private static Student retriveStudentFromDatabase(){      Student student = new Student();      student.setName("Robert");      student.setRollNo("10");      return student;   }}

转载地址:http://nbjla.baihongyu.com/

你可能感兴趣的文章
全新Linux学习路线图及入门教程
查看>>
《企业云桌面实施》-小技巧-03-vSAN6.5中SAS和SSD的使用建议
查看>>
Powershell统计邮箱账户信息
查看>>
工作汇报神技!用Python三步生成带有图表的word报表
查看>>
SFB 项目经验-48-去死吧,天天让人烦的垃圾邮件
查看>>
买《Python从小白到大牛》专题视频课程,送配套纸质图书
查看>>
puppet kick 报错返回值code3 求解答(finished with exit code 3)
查看>>
Windows Server 2012的服务管理自动化
查看>>
薛蛮子入局,EOS超级节点竞争进入白热化
查看>>
微博变种与RSS变种,互联网营销
查看>>
WCF开发框架形成之旅--WCF应用常见问题处理
查看>>
【译】在Asp.Net中操作PDF - iTextSharp - 绘制矢量图
查看>>
什么是代理服务器
查看>>
二叉排序树经典算法速成
查看>>
可判断焦点是否停在输入窗口
查看>>
JRuby-Rack
查看>>
Warbler, A Little Birdie To Introduce Your Rails App To Java
查看>>
C#窗体控件-分组框控件GroupBox
查看>>
生成随机数
查看>>
POJ 2996 Help Me with the Game(水模拟)
查看>>