裝飾器模式用于動態(tài)地為對象添加新功能,其核心在于通過組合而非繼承實(shí)現(xiàn)靈活擴(kuò)展。當(dāng)你需要以不同方式組合功能(如加密、壓縮消息)時(shí),避免因子類爆炸導(dǎo)致代碼混亂,裝飾器模式通過逐層包裝原始對象實(shí)現(xiàn)功能疊加,同時(shí)保持統(tǒng)一接口。具體步驟為:1. 定義公共接口或基類(如IMessage);2. 創(chuàng)建基本組件(如TextMessage);3. 構(gòu)建抽象裝飾器類,持有組件引用并實(shí)現(xiàn)相同接口;4. 實(shí)現(xiàn)具體裝飾器(如EncryptedMessageDecorator、CompressedMessageDecorator),在調(diào)用組件方法前后添加行為;5. 通過嵌套包裝組合多個裝飾器,實(shí)現(xiàn)功能疊加。例如在Web請求處理中,可依次應(yīng)用身份驗(yàn)證和日志記錄裝飾器,使每個裝飾器專注于單一職責(zé),增強(qiáng)代碼可維護(hù)性與復(fù)用性。
The Decorator pattern is a way to add new features or behaviors to an object dynamically, without changing its class. It’s especially useful when you want to extend functionality in a flexible and reusable way—unlike static inheritance, which can get messy fast.
Why You’d Use the Decorator Pattern
Let’s say you have a basic component, like a text message, and you want to add things like encryption, compression, or logging on top of it. Instead of creating separate subclasses for each possible combination (like EncryptedMessage, CompressedMessage, EncryptedAndCompressedMessage, etc.), the Decorator pattern wraps the original object with one or more decorators that add behavior as needed.
This keeps your code clean and makes it easier to mix and match features without rewriting a ton of classes.
For example:
- You start with a plain
TextMessage
. - Then wrap it with an
EncryptedMessageDecorator
. - Then maybe wrap that with a
CompressedMessageDecorator
.
Each layer adds functionality but still acts like a regular message object.
How It Works in Practice
At its core, the Decorator pattern relies on having a common interface or base class that both the main component and all decorators implement or extend. This allows decorators to freely wrap and enhance components without breaking compatibility.
Here’s a rough breakdown:
-
Component: The base interface or class (e.g.,
IMessage
). -
Concrete Component: The basic implementation (e.g.,
TextMessage
). - Decorator: An abstract class or base decorator that also implements the same interface and holds a reference to a component.
-
Concrete Decorators: Specific enhancements (e.g.,
EncryptedMessageDecorator
,CompressedMessageDecorator
).
Each concrete decorator calls the component's method first (like send()
) and then adds extra behavior before or after.
Real-World Example: Logging + Authentication
Imagine a web request handler. You might have:
- A basic
RequestHandler
that responds to HTTP requests. - A
LoggingDecorator
that logs the request before passing it along. - An
AuthDecorator
that checks if the user is logged in before proceeding.
You could stack them like this:
$handler = new AuthDecorator(new LoggingDecorator(new HomeController()));
Now, every time handle()
is called, it first checks authentication, then logs the request, then runs the actual controller logic.
This kind of chaining is powerful and keeps each piece focused on just one job.
That’s basically how the Decorator pattern works. It’s not hard once you see the structure, but it can be easy to miss the point if you're used to solving everything with inheritance.
? ??? ????? ??? ??????? ?? ?????. ??? ??? PHP ??? ????? ?? ?? ??? ?????!

? AI ??

Undress AI Tool
??? ???? ??

Undresser.AI Undress
???? ?? ??? ??? ?? AI ?? ?

AI Clothes Remover
???? ?? ???? ??? AI ?????.

Clothoff.io
AI ? ???

Video Face Swap
??? ??? AI ?? ?? ??? ???? ?? ???? ??? ?? ????!

?? ??

??? ??

???++7.3.1
???? ?? ?? ?? ???

SublimeText3 ??? ??
??? ??, ???? ?? ????.

???? 13.0.1 ???
??? PHP ?? ?? ??

???? CS6
??? ? ?? ??

SublimeText3 Mac ??
? ??? ?? ?? ?????(SublimeText3)

Java ??????? ??? ??? ???? ??? ???? ??? ??? ???? ??(?: ??? ??) ?? ?? ??? ??? ?? ????? ???? ???? ??? ?? ???? ???? ????? ????. ???? ??? ???? ????? ?? ??? ?? ??? ?? ? ?? ??? ??? ?? ??? ??? ?? ?? ??? ?????.

TDD? ??? PHP ??? ???? ? ?????. ???? ??? ?? ??, ?? ?? ?? ? ?? ???? ?????. ??? ???? ?? ?? ?? ??? ???? ????? ??? ?????. ??? ???? ??? ? ??? ????? ?????? ???, ?? ??? ? ???? ??????.

Guice ?????? ??? ??? ??? ??? ??? ?????. ??? ??: @Singleton ??? ?? ???? ????? ??? ??? ?????. ??? ??? ??: @Provides ??? ?? ??? ???? ???? ??? ?? ?? ?? ????? ????. ?? ??: ????? ??? ?? ???? ????? @Named ??? ?? ?? ??? ?????.

????? ??? ?? ???? ???? ??? ?? ??? ???? ??? ? ?? ??? ??? ?????. ?? ????, ???? ????, ?? ?????, ???? ?????? ??? ?? ????, ???? ??? ?? ??? ??? ???? ??? ? ????. ? ???? ??? ?? ?????? ? $2.29? ???? Espresso? ???? ??? ??? ???? ???? ????? ??? ?? ?????.

SpringMVC ?????? ?? ??? ??? ?????: 1. ??? ??: Spring ????? ?????. 2. Facade ??: ????, ? ? ?? ?? ??? ?????. 3. ?? ??: ??? ???? ?? ???? ?????. : ?????? ???? ???? ?????. ??? ??? ??? SpringMVC? ??? ???? ???? ???? ????? ?? ?? ??? ??????? ?? ? ??? ???.

Java ??????? ??? ??? ???? ??? ?? ???, ?? ??? ? ???? ????? ??? ????. ????? ???, ?? ????, ??? ???? ?? ??? ?? ?? ?? ????. ?? ??: ??? ??? ??? ?? ???? ? ?????. ??? ??? ???? ???? ??? ???? ??? ??????.

PHP ??? ??? ????? ??? ???? ??? ?? ??? ???? ?????. ???? ?? ???? ?? ??(?: ??? ??? ??), ??? ??(?: ????? ??) ? ?? ??(?: ??? ??)? ?????. ??? ??? ???? ??? ????, ?? ???? ????, ???? ??? ? ?? ?????. ????? ????? ??? ??? ????? ?? ?? ?? ?? ????? ??? ? ????. ????? PHP ??? ??? ???? ?? ???? ?? ?? ??? ??????? ??? ? ??? ?????.

TDD ? ??? ??? ?? ??? ?? ???? ??????. TDD? ??? ?? ??? ???? ?? ???? ????? ?? ??? ??????. ??? ??? ??? ?? ? ?? ???? ?? ??? ?? TDD? ???? ???? ?????? ??? ?? ??? ????? ?????. ?? ????, ?? ??? ? ?? ??? ??? ?? ?? ???? ?? ??? ??????.
