Skip to main content

MVC - What is behind ? The core of Design Pattern?

Một trong những Pattern gần nhất với MVC là Observer Pattern
xem qua VD sau:

import java.util.Observable;
import java.util.Observer;
import mylib.io.console.*;

public class Main {
public static void main(String[] args) {
DoituongDuocQuanSat obj =
new DoituongDuocQuanSat("Observable obj 1"); // Create observable obj

Observer[] nhomQuanSat = {
new Person("Observer 1",
"So 1 bao cao, obj 1 da hanh dong"),
new Person("Observer 2",
"So 2 bao cao, obj 1 da hanh dong"),
new Person("Observer 3",
"So 3 bao cao, obj 1 da hanh dong"),
new Person("Observer 4",
"So 4 bao cao, obj 1 da hanh dong"),
new Person("Observer 5",
"So 5 bao cao, obj 1 da hanh dong") };

// Add the observers
for (int i = 0; i <>
obj.addObserver(nhomQuanSat[i]);

obj.ThayDoiTrangThai(obj.getName()+ " hanh dong"); // obj 1 da hanh dong
//ngay lap tuc nhom quan sat se thong cao

Console.readString();
}
}

class DoituongDuocQuanSat extends Observable {
public DoituongDuocQuanSat(String s)
{
name = s;
}

public void ThayDoiTrangThai(String s) {
name = s;
setChanged();
notifyObservers();
}

public String getName() {
return name;
}

private String name = "Observable Object";
}

class Person implements Observer {
String name; // Person's identity

String says; // What they say when startled

// Constructor
public Person(String name, String says) {
this.name = name;
this.says = says;
}

// Called when observing an object that changes
public void update(Observable thing, Object o) {
System.out.println(((DoituongDuocQuanSat) thing).getName() + "\n"
+ name + ": " + says);
}
}

như sách đã nói Observer/Oservable là thuộc Observer Pattern. Nó giúp ta cập nhập trạng thái hiển thị của Component
ở đậy đối tượng được quan sát (Observable object) sẽ là dữ liệu của chương trình, nếu có bất kì thay đổi dữ liệu nào từ tác động của User thì tất cả đối tượng đóng vai trò quan sát (Observer obj) sẽ cập nhập trạng thái của mình.

thứ tự như sau:
sau khi khai báo xong.
Nếu
obj.ThayDoiTrangThai(obj.getName()+ " hanh dong"); được gọi thì
Observable obj sẽ
setChanged();

notifyObservers();

tất cả view (Observer) sẽ tự động gọi
public void update(Observable thing, Object o) {

System.out.println(((DoituongDuocQuanSat) thing).getName() + "\n"
+ name + ": " + says);
}


Trở lại câu hỏi

What Is the MVC Pattern?

The MVC pattern divides the application into three layers.

  • The model contains the core of the business logic. It maintains the application state and processes all application-specific data to determine state transitions.

  • The view presents the model to users. In GUI applications, the view typically consists of a set of display screens. It is the outbound interface of the application.

  • The controller links the view to the model. When the user manipulates the view, the controller passes the requests to the model and updates the view after the model responds. It is the inbound interface of the application.

Key benefits of the MVC pattern are as follows.

  • Since the layers are separate from each other, the application is componentized. Each component can be developed and maintained independently by specialists. For example, the view layer can be developed by UI designers, while the model layer can be developed by domain knowledge experts and data modelers. That allows us not only to allocate talent efficiently but also to parallelize the development work and hence shorten the time to market.

  • The compartmented layers are easy to test. Automated unit tests can be used extensively in the model layer to ensure functional correctness. User experience tests can be conducted against the view in usability labs before the model is ready (see Chapter 13 for more on the testing topics).

  • The execution flow in an MVC application is well defined. MVC code is easy to understand, since many developers are already familiar with the pattern. That improves the code maintainability and reduces the cost in the long run.

  • For applications that require several different user interfaces, we can develop several different views while reusing the same model and controller. As we see in Chapter 12, this simplifies our work to optimize the application for multiple devices within a developer platform series or migrate the application to other developer platform series with different UI requirements.

  • As the application gets more complex over time, it may be unavoidable for us to evolve the data model to accommodate the new needs. A separate model layer clearly indicates which classes we need to extend, and the changes are not propagated to other parts of the application. That again promotes code reuse in an object-oriented system.

Figure 2: The end-to-end MVC pattern
Hình phía trên mô tả kiến trúc (architecture) của Ruby 2.0 Multimedia Noting chạy trên Mobile + Web của tôi.
Như vậy có n View, 1 component Controller, 1 Model.
Đây chỉ là nguyên tắc chung, việc thực thi kiến trúc thật ở 1 project sẽ tùy vào app cụ thể.




Comments

Popular posts from this blog

Netty Cookbook - free ebook for Java Developer

Introduction Netty.io is a popular open source library that greatly simplifies the development of network applications on top of the JVM.  It abstracts the burden to deal with tedious low level details and allows you to concentrate on your business logic instead.  It is used by high profile companies like Red Hat, Twitter or Facebook and designed from the ground up to handle high throughput at low latency, even with thousands of connections at the same time. In this book, you are going to build both client and server using netty best practices, which are communicating with each other in a completely asynchronous fashion.  We are going to explore the netty pipeline, how you can reuse existing protocol handlers and how to write your own. Source code for book  https://github.com/trieu/netty-cookbook Table of Contents Chapter 1: Communicating in Asynchronous World with Netty (15 pages) Introduction Recipe 1.1 Building an asynchronous TCP server and client Recipe 1.

Vì sao chúng ta cần ứng dụng Dataism cho đời sống

Dataism là một triết lý cho rằng dữ liệu là nền tảng của thực tại.  Theo triết lý này, dữ liệu là thứ tạo ra mọi thứ, từ các sự kiện trong thế giới thực đến suy nghĩ và cảm xúc của con người. Trong bối cảnh cuộc cách mạng công nghiệp 4.0 và sự phát triển mạnh mẽ của công nghệ thông tin. Tên gọi "Dataism" được đặt ra để mô tả một quan điểm cho rằng dữ liệu, đặc biệt là dữ liệu số và thông tin, chính là một nguyên tắc lớn trong tự nhiên và xã hội. Nguyên gốc của Dataism không thể được liên kết chặt chẽ với một cá nhân hay tổ chức cụ thể, nhưng một số tác giả và nhà nghiên cứu nhất định đã đóng góp vào việc phát triển và mô tả triết lý này. Một số tác giả nổi tiếng trong lĩnh vực này bao gồm: Yuval Noah Harari: Tác giả của cuốn sách nổi tiếng "Sapiens: Lược sử loài người" và "Homo Deus: Lược sử tương lai", Harari đã đề cập đến khái niệm Dataism trong việc mô tả sự tiến hóa của con người và xã hội.  https://dataethics.eu/humanism-dataism-future-scenario/ Wiki

Các lý thuyết tâm lý học cổ điển về động lực sống của con người

Hôm nay, mình viết 1 bài post tóm tắt theo kiến thức cơ bản về tâm lý học, theo những gì mình đã đọc + kinh nghiệm sống của cá nhân mình.  sự thật thú vị là Sigmund Freud là thầy của Carl Jung và Adler. Điểm chung:  cả 3 Bác đều tập trung nghiên cứu về các nguyên lý cơ bản mà tâm trí, ý thức của con người hoạt động.  Bản chất là các dữ liệu trong bộ não, ở trạng thái vô thức ảnh hưởng trực tiếp đến cách con người nhận thức về thế giới xung quanh họ, bao gồm cả đạo đức, cách suy nghĩ, cảm xúc và ý chí.  Một người không thể hành động mà không có 1 động lực tác động trong suy nghĩ của họ, nó phải có nguyên nhân. Hầu hết các nguyên nhân nằm trong mode "vô thức", bạn rất ít khi tự nhận ra chúng  Điểm khác biệt và đặc trưng: 1) #SidmundFreud  mô hình tâm lý của Freud tập trung vào  1.1 cõi vô thức con người,   1.2 tình dục và động lực sinh sản là năng lượng chính trong cõi vô thức 1.3 các chấn thương tâm lý nhưng bị ý thức kiềm chế lâu ngày 1.4 các ham muốn (như ham muốn tình dục)