← Back to Advanced Java Course | Chapter 6: Design Patterns | Lesson 6 of 14

Java का MVC Architecture

MVC किसी program को तीन हिस्सों में बांटता है: data, जो आप देखते हैं, और वह हिस्सा जो इन्हें जोड़ता है, जैसे एक restaurant जिसमें kitchen, dining room, और एक waiter हो। हर हिस्से का अपना काम है।

The Model

Model application के data और business logic को represent करता है -- entities, validation rules, और persistence access सब यहीं रहते हैं। इसे जानबूझकर user interface (View) या application कैसे control होता है इसकी कोई जानकारी नहीं है, जो वही है जो एक ही business logic को किसी web UI, desktop UI, या API के पीछे reusable रखता है।

उदाहरण: The Model

java
public class Main {
	static class UserModel {
		String name;
		UserModel(String name) { this.name = name; } // no knowledge of View or Controller
	}
	public static void main(String[] args) {
		UserModel model = new UserModel("Riya");
		System.out.println(model.name);
	}
}

The View

View सिर्फ Model के data को user को render करने के लिए ज़िम्मेदार है -- इसमें presentation logic होनी चाहिए, business rules नहीं। यह Controller से data पाता है और उसे उपयुक्त रूप से दिखाता है, और एक अच्छी तरह अलग किए गए MVC design में आप Model को छुए बिना पूरे View को बदल सकते हैं (जैसे HTML की जगह JSON)।

उदाहरण: The View

java
public class Main {
	static class UserView {
		void display(String name) {
			System.out.println("User: " + name); // presentation only, no business rules
		}
	}
	public static void main(String[] args) {
		new UserView().display("Riya");
	}
}

The Controller

Controller बाकी दोनों layers के बीच intermediary की तरह काम करता है: यह View से user inputs प्राप्त करता है, उन्हें Model पर calls में translate करता है, और फिर जो भी बदला उसे render करने के लिए View को trigger करता है। यहीं request handling और coordination logic होनी चाहिए, जानबूझकर पतली रखी गई ताकि business rules Model में रहें।

उदाहरण: The Controller

java
public class Main {
	static class UserModel { String name; UserModel(String name) { this.name = name; } }
	static class UserView { void display(String name) { System.out.println("User: " + name); } }
	static class UserController {
		UserModel model; UserView view;
		UserController(UserModel model, UserView view) { this.model = model; this.view = view; }
		void updateView() { view.display(model.name); } // coordinates Model and View
	}
	public static void main(String[] args) {
		UserController controller = new UserController(new UserModel("Riya"), new UserView());
		controller.updateView();
	}
}

Flow of MVC

किसी MVC application में आप Model, View, और Controller बनाते हैं और उन्हें आपस में जोड़ते हैं, आमतौर पर किसी main method के अंदर या किसी framework की startup routine में। इस assembly step को समझना मायने रखता है क्योंकि MVC apps में ज़्यादातर bugs flow का सम्मान करने के बजाय गलत layer में पहुंचते Controller से आते हैं।

उदाहरण: Flow of MVC

java
public class Main {
	static class Model { String data = "value"; }
	static class View { void render(String data) { System.out.println("Rendered: " + data); } }
	static class Controller {
		Model model; View view;
		Controller(Model model, View view) { this.model = model; this.view = view; }
		void handle() { view.render(model.data); }
	}
	public static void main(String[] args) {
		Model model = new Model();
		View view = new View();
		Controller controller = new Controller(model, view); // wired together at startup
		controller.handle();
	}
}

Passive vs Active MVC

एक Active MVC design में, View खुद को Model के एक observer के रूप में register करता है, इसलिए जब Model की state बदलती है तो यह Controller को हर update mediate किए बिना सीधे View को notify करता है। यह बार-बार state बदलने वाले UIs के लिए ज़्यादा efficient है, लेकिन इसका मतलब यह भी है कि View अब Model के notification mechanism पर निर्भर करता है, एक trade-off जिसे Passive MVC के बजाय इसे चुनने से पहले समझना ज़रूरी है।

उदाहरण: Passive vs Active MVC

java
import java.util.*;
public class Main {
	interface Observer { void onModelChanged(String data); }
	static class ActiveModel {
		List<Observer> observers = new ArrayList<>();
		void subscribe(Observer o) { observers.add(o); }
		void setData(String data) {
			for (Observer o : observers) o.onModelChanged(data); // View notified directly
		}
	}
	public static void main(String[] args) {
		ActiveModel model = new ActiveModel();
		model.subscribe(data -> System.out.println("View updated: " + data));
		model.setData("new state");
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. View के अंदर business logic डालना, जबकि View को सिर्फ data display करना चाहिए।
  2. Model को View या Controller के बारे में जानने देना, जो layers को आपस में जोड़ देता है।
  3. Controller से data rules और formatting सहित सब कुछ करवाना, जब तक यह एक बड़ी class न बन जाए।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.