Why Design Patterns Still Matter: Middleware, MVC & DI
Exploring the limits and practical value of software design patterns, this article examines philosophical critiques of scientific induction, then delves into three core web architecture patterns—Middleware, MVC, and Dependency Injection—illustrating their implementation in Node.js and Java, and highlighting how they improve code modularity, communication, and scalability.
Before diving into concrete examples, the article raises two questions: what are the limitations of design patterns, and what practical value do they provide?
Science, as an empirical epistemology, suffers from the inherent flaw of never producing absolute truth because induction is non‑unique.
Using a simple hypothetical world, two contradictory inductive conclusions are presented to illustrate that without additional information, choosing the correct theory is impossible.
Design patterns, like scientific theories, are context‑dependent solutions rather than universal truths.
The article then introduces three architectural patterns commonly used in modern web development.
Middleware Pattern
In a typical Node.js HTTP request‑response cycle, the middleware pattern handles tasks such as logging, authentication, cookie parsing, routing, error handling, and response timing.
Centralized control flow
Decoupled processing modules (reusability)
Declarative, configurable services (separate from code)
The classic Intercepting Filter pattern from J2EE is shown, followed by a Java example:
public class DebuggingFilter implements Processor { private Processor target; public DebuggingFilter(Processor myTarget) { target = myTarget; } public void execute(ServletRequest req, ServletResponse res) throws IOException, ServletException { // preprocess target.execute(req, res); // post‑process } }In dynamic languages like JavaScript, middleware can freely attach data to req and res, making implementations more elegant.
Express Middleware
Express adopts the middleware pattern with a signature function (req, res, next) and supports declarative routing. Example:
var express = require('express'); var app = express();Express also provides a powerful Router for large projects, enabling tree‑like routing decisions.
var express = require('express'); var router = express.Router(); router.get('/', function (req, res) { res.send('Birds home page'); }); module.exports = router;Koa Asynchronous Middleware (Onion Model)
Koa improves on Express by using async functions that return Promise, enabling async/await and a clean onion‑style flow for pre‑ and post‑processing.
MVC Pattern
The Model‑View‑Controller pattern, originally defined in 1979, remains a cornerstone for server‑side web frameworks. It separates data models, presentation, and request handling, improving code reuse and reducing complexity.
When a project grows, MVC helps avoid duplicated view logic and inconsistent data handling across pages and devices.
Dependency Injection (DI) Pattern
DI addresses scalability concerns such as session storage by abstracting service creation behind interfaces. The Inversion of Control (IoC) container creates and injects concrete implementations at runtime.
In JavaScript, frameworks like ThinkJS use extend and adapter to mix in dependencies into controllers, offering flexibility without the reflection overhead required in static languages.
Overall, these three patterns—Middleware, MVC, and DI—demonstrate how abstract, language‑agnostic solutions facilitate communication, modularity, and scalability across diverse web frameworks.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
21CTO
21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
