Thứ Tư, 24 tháng 4, 2024

MVC

 


Thành phầnMô tả
ModelModel đại diện cho dữ liệu và logic xử lý dữ liệu của ứng dụng. Nó không biết đến bất kỳ phần nào của View hoặc Controller. Cập nhật dữ liệu và thông báo cho các thành phần khác về các thay đổi trong dữ liệu.
ViewView là thành phần hiển thị dữ liệu cho người dùng. Nó hiển thị dữ liệu từ Model và gửi thông tin về sự kiện người dùng (như click chuột, nhập liệu) đến Controller. Không có logic nghiệp vụ trong View.
ControllerController là thành phần trung gian giữa Model và View. Nó chịu trách nhiệm xử lý các sự kiện từ người dùng và thực hiện các thao tác tương ứng trên Model. Controller cập nhật View với dữ liệu mới từ Model và đảm bảo rằng View hiển thị dữ liệu phản ánh trạng thái mới nhất của Model.


Ví dụ :

Model (Model.java):

public class Model { public int add(int num1, int num2) { return num1 + num2; } }

View (View.java):

import javax.swing.*; public class View { private JTextField num1Field; private JTextField num2Field; private JButton addButton; // Thêm trường addButton // Thêm controller để liên kết với Controller private Controller controller; public View() { JFrame frame = new JFrame("Calculator"); JPanel panel = new JPanel(); num1Field = new JTextField(5); num2Field = new JTextField(5); addButton = new JButton("Add"); // Khởi tạo addButton // Thêm ActionListener cho addButton addButton.addActionListener(e -> { // Gọi phương thức xử lý sự kiện từ Controller if (controller != null) { controller.calculateButtonClicked(); } }); panel.add(num1Field); panel.add(num2Field); panel.add(addButton); // Thêm addButton vào panel frame.add(panel); frame.pack(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } // Getter và setter cho controller public void setController(Controller controller) { this.controller = controller; } public String getNum1Text() { return num1Field.getText(); } public String getNum2Text() { return num2Field.getText(); } public void setResult(int result) { JOptionPane.showMessageDialog(null, "Result: " + result); } }

Controller (Controller.java):

import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class Controller { private View view; private Model model; public Controller(View view, Model model) { this.view = view; this.model = model; // Set controller cho view view.setController(this); } public void calculateButtonClicked() { // Lấy dữ liệu từ giao diện người dùng int num1 = Integer.parseInt(view.getNum1Text()); int num2 = Integer.parseInt(view.getNum2Text()); // Gọi phương thức tính toán trong model int result = model.add(num1, num2); // Hiển thị kết quả trong giao diện người dùng view.setResult(result); } public static void main(String[] args) { // Tạo các đối tượng cần thiết View view = new View(); Model model = new Model(); Controller controller = new Controller(view, model); } }

Trong trường hợp này, thay vì sử dụng addActionListener() trực tiếp trong lớp View, chúng ta đã thêm một phương thức setController() để thiết lập controller cho view, sau đó gọi phương thức calculateButtonClicked() từ controller khi nút tính toán được nhấn. Điều này giúp tách biệt logic xử lý sự kiện ra khỏi lớp View, giữ cho mã của chúng ta dễ đọc hơn và dễ bảo trì hơn.

Kết quả :




Ghi chú:

// Thêm ActionListener cho addButton addButton.addActionListener(e -> { // Gọi phương thức xử lý sự kiện từ Controller if (controller != null) { controller.calculateButtonClicked(); } });

Đoạn mã trên sử dụng lambda expression, ta có thể viết lại đoạn mã trên bằng cách sử dụng một lớp inner class. Điều này có thể giúp làm cho mã dễ hiểu hơn cho những người mới bắt đầu. Dưới đây là cách viết lại:

// Thêm ActionListener cho addButton addButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // Gọi phương thức xử lý sự kiện từ Controller if (controller != null) { controller.calculateButtonClicked(); } } });

Trong đoạn mã này, chúng ta tạo một instance mới của lớp inner class
ActionListener và triển khai phương thức actionPerformed. Khi nút được nhấn, phương thức actionPerformed này sẽ được gọi, và trong đó chúng ta kiểm tra xem controller có được khởi tạo hay không trước khi gọi phương thức xử lý sự kiện từ controller.

v

Không có nhận xét nào:

Đăng nhận xét