摘要:原文鏈接譯者個人翻譯,水平有限,希望有所幫助設計模式模式設計模式是模式的代表。代表可視化模型所包含的數據。它控制數據流向對象,并且在數據發(fā)生改變的時候更新視圖,它保持者和的分離。
設計模式-MVC模式原文鏈接
譯者:smallclover
個人翻譯,水平有限,希望有所幫助
MVC設計模式 是Model-View-Controller 模式的代表(stand for)。該設計模式主要是用來分離(separate)應用的關注點(concerns)
注:
Model:模型 View:視圖 Controller:控制器
? Model - Model 代表一個對象(object)或者java普通對象(POJO)裝載的數據,如果它的數據發(fā)生變化,它也可以有邏輯到update controller。
? View – View代表可視化模型所包含的數據。
? Controller - Controller 作用于model和view。它控制數據流向model對象,并且在數據發(fā)生改變的時候更新視圖,它保持者view和model的分離。
實現首先我們會創(chuàng)建一個Student對象來扮演model,StudentView將作為一個view類,它能在控制臺輸出學生的詳細信息,StudentController 作為一個controller 負責把數據存儲到student對象并且相應的更新view StudentView
MVCPatternDemo,我們的demo類,將使用StudentController來展示如何使用MVC模式
第一步創(chuàng)建 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; } }第二步
創(chuàng)建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); } }第三步
創(chuàng)建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()); } }第四步
使用StudentController的方法展示MVC設計模式的的使用
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; } }第五步
校驗輸出
Student: Name: Robert Roll No: 10 Student: Name: John Roll No: 10
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規(guī)行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://m.hztianpu.com/yun/66042.html
摘要:創(chuàng)建的對象使構造函數私有,外界將無法實例化該類獲得唯一可用的對象第二步從單例類獲得唯一的對象。非法構造編譯錯誤,構造函數不可見。獲得唯一可用對象展示信息第三步校驗輸出。 原文鏈接譯者:smallclover個人翻譯,水平有限,如有錯誤歡迎指出,謝謝! 設計模式-單例模式 單例模式是Java中最簡單的設計模式之一。這種類型的設計模式,是創(chuàng)建型模式下創(chuàng)建對象的最好方式之一。這個模式涉及到一...
摘要:原文鏈接譯者個人翻譯,水平有限,如有錯誤歡迎指出,謝謝設計模式工廠模式工廠模式是中最常用的設計模式之一。這種類型的設計模式屬于創(chuàng)建型模式下,創(chuàng)建一個對象最好的方式之一。調用圓的方法獲得矩形的一個對象并調用它的方法。 原文鏈接譯者:smallclover個人翻譯,水平有限,如有錯誤歡迎指出,謝謝! 設計模式-工廠模式 工廠模式是Java中最常用的設計模式之一。這種類型的設計模式屬于創(chuàng)建型...
摘要:設計模式是軟件開發(fā)人員在整個軟件開發(fā)的過程中面臨普遍問題的解決方案。這些作者被統稱為四人幫。根據這些作者的觀念,設計模式主要是基于一下幾種面向對象的設計原則。例如,單例模式表示使用單一對象。我們還將討論另外一個類別的設計模式。 原文鏈接譯者:smallclover個人翻譯,水平有限,如有錯誤歡迎指出,謝謝! 設計模式-概述 設計模式體現了經驗豐富的面向對象軟件開發(fā)人員的最佳實踐。設計模...
摘要:調用者對象會尋找合適的并且能夠處理該命令的對象,然后把該命令傳遞給相應的對象處理。緊接著創(chuàng)建一個類代表請求。創(chuàng)建具體的命令類和實現接口,它們將會作為具體的命令被處理。代表調用者,它能獲得并且發(fā)出命令。 原文連接譯者 smallclover希望對大家有所幫助。謝謝!(●?●) 設計模式-命令模式 命令模式是一種數據驅動的設計模式,屬于行為型模式這一類。命令模式會將一個請求包裝成一個對象并...
摘要:在代理模式中,我們將創(chuàng)建一個對象,該對象在在接口中持有原始對象,以對外部提供它的功能。實現我們將創(chuàng)建一個接口并且創(chuàng)建具體類實現接口。 原文鏈接譯者:smallclover希望對你們有所幫助,謝謝閱讀! 設計模式-代理模式 在代理模式中,我們使用一個類來代表另一個類的功能。這種類型的設計模式屬于結構型設計模式的一種。在代理模式中,我們將創(chuàng)建一個對象,該對象在在接口中持有原始對象,以對外部...
閱讀 2042·2021-11-25 09:43
閱讀 717·2021-10-11 10:58
閱讀 1815·2019-08-30 15:55
閱讀 1790·2019-08-30 13:13
閱讀 800·2019-08-29 17:01
閱讀 1904·2019-08-29 15:30
閱讀 905·2019-08-29 13:49
閱讀 2234·2019-08-29 12:13