1CatalogConfig.productCatalog() ghép ba lớp như búp bê Nga: new JdbcProductCatalog(jdbc), bọc bằng new CachingProductCatalog(db), rồi bọc tiếp bằng timed(cached). Cả ba đều là ProductCatalog nên nơi dùng bean không biết có bao nhiêu lớp vỏ.CatalogConfig.productCatalog() stacks three layers like Russian dolls: new JdbcProductCatalog(jdbc), wrapped by new CachingProductCatalog(db), wrapped again by timed(cached). All three are ProductCatalog, so code using the bean cannot tell how many layers there are.
2Lời gọi findById("SP1001") đầu tiên vào lớp ngoài cùng: lambda của timed() ghi System.nanoTime() rồi gọi inner.findById(). Cache chưa có SP1001 nên báo trượt.The first findById("SP1001") call enters the outermost layer: the timed() lambda records System.nanoTime() and calls inner.findById(). The cache does not have SP1001 yet, so it misses.
3CachingProductCatalog chuyển lời gọi tới delegate là JdbcProductCatalog, vì vẫn cần nạp từ DB. Thêm cache không làm đổi câu truy vấn của lớp gốc.CachingProductCatalog forwards the call to its delegate, JdbcProductCatalog, because the product still has to be loaded from the database. Adding the cache leaves the core query unchanged.
4JDBC trả Optional có sản phẩm; cache put() vào Map (kết quả rỗng không được lưu). Khối finally của timed() ghi thời gian, gồm cả tra cache và đọc DB. Bản Go WithCache(next) làm đúng việc này với sync.RWMutex.JDBC returns an Optional holding the product; the cache put()s it into the Map (empty results are not stored). timed()'s finally block logs the elapsed time, including the cache lookup and DB read. Go's WithCache(next) does the same with a sync.RWMutex.
5Lần gọi findById("SP1001") thứ hai đi qua cùng chuỗi. Cache trả ngay bản đã lưu, JDBC không bị gọi, và timed() vẫn ghi lại lần gọi nhanh này.The second findById("SP1001") call goes through the same chain. The cache returns the stored product at once, JDBC is not called, and timed() still records this fast call.
6Spring cũng dùng decorator: bean DailyReportService có @Transactional nên được bọc trong proxy. Gọi closeDay(day) sẽ mở transaction, chạy markSettled() và archive(), rồi commit hoặc rollback.Spring uses decorators too: the DailyReportService bean has @Transactional, so it is wrapped in a proxy. Calling closeDay(day) opens a transaction, runs markSettled() and archive(), then commits or rolls back.
7exportCsv() xếp decorator I/O của JDK: GZIPOutputStream bọc BufferedOutputStream bọc luồng file thô. Mỗi out.write() đi từ ngoài vào trong: nén, gom bộ đệm, rồi mới ghi xuống đĩa.exportCsv() stacks JDK I/O decorators: GZIPOutputStream around BufferedOutputStream around the raw file stream. Each out.write() travels outside-in: compress, buffer, then write to disk.
8Bản Go: main gọi httpx.Chain(mux, Logging(logger), RequireAPIKey(key)). Chain áp từ cuối lên nên Logging là lớp ngoài cùng. Request thiếu X-API-Key bị RequireAPIKey chặn với 401, mux không chạy, nhưng Logging vẫn ghi log.The Go version: main calls httpx.Chain(mux, Logging(logger), RequireAPIKey(key)). Chain applies from the last one, so Logging is outermost. A request without X-API-Key is stopped by RequireAPIKey with 401, mux never runs, yet Logging still logs it.
9Có đúng key thì RequireAPIKey gọi next.ServeHTTP() và mux trả về sản phẩm. Mỗi middleware nhận và trả về http.Handler, nên thêm lớp mới không phải sửa handler gốc.With the right key, RequireAPIKey calls next.ServeHTTP() and the mux answers. Each middleware takes and returns an http.Handler, so adding a layer never touches the core handler.