Tuesday, July 31, 2007

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.ThayDoiTrangThai(obj.getName()+ " hanh dong"); // obj 1 da hanh dong
//ngay lap tuc nhom quan sat se thong cao


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

public void ThayDoiTrangThai(String s) {
name = s;

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.
obj.ThayDoiTrangThai(obj.getName()+ " hanh dong"); được gọi thì
Observable obj sẽ


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ể.

No comments: