๋ฐ์ํ
Decorator Pattern
SOLID ์ค์์ OCP์ DIP๋ฅผ ๋ฐ๋ฅธ๋ค.
- ๋ฐ์ฝ๋ ์ดํฐ ํจํด์ ๊ธฐ๋ณธ ๊ฐ์ฒด์
์ถ๊ฐ์ ์ธ ๊ธฐ๋ฅ์ ๋์ ์ผ๋ก ์ ์ฐํ๊ฒ ์ฒจ๊ฐํ๋ ํจํด์ด๋ค. - ๊ฐ์ฒด์ ์ถ๊ฐ์ ์ธ ์๊ฑด์ ๋์ ์ผ๋ก ์ฒจ๊ฐํ๋ฉฐ, ๊ธฐ๋ฅ ํ์ฅ์ด ํ์ํ ๋ ์๋ธํด๋์ฑ ๋์ ์ธ ์ ์๋ ์ ์ฐํ ๋์์ด ๋ ์ ์๋ค.
ใ ใ
ใ
- Component
ConcreteComponent
์Decorator
๊ฐ ๊ตฌํํ ์ธํฐํ์ด์ค๋ค.- ๋ ๊ฐ์ฒด๋ฅผ ๋๋ฑํ๊ฒ ๋ค๋ฃจ๊ธฐ ์ํด ์กด์ฌํจ
- ConcreteComponent
- Decorate ๋ฅผ ๋ฐ์ ๊ฐ์ฒด๋ค.
- ์ฆ, ๊ธฐ๋ฅ ์ถ๊ฐ๋ฅผ ๋ฐ์ ๊ธฐ๋ณธ ๊ฐ์ฒด
- Decorator
- Decorate ๋ฅผ ํ ๊ฐ์ฒด์ ์ถ์ ํด๋์ค๋ค.
- ์ฆ, ๊ธฐ๋ฅ ์ถ๊ฐ๋ฅผ ํ ๊ฐ์ฒด๋ ์ด ๊ฐ์ฒด๋ฅผ ์์๋ฐ๋๋ค.
- ConcreteDecorator
Decorator
๋ฅผ ์์๋ฐ์ ๊ตฌํํ ๋ค์ํ ๊ธฐ๋ฅ ๊ฐ์ฒด์ด๋ค.- ์ด ๊ธฐ๋ฅ๋ค์
ConcreteComponent
์ ์ถ๊ฐ๋๊ธฐ ์ํด ๋ง๋ค์ด ์ง๋ค.
ใ
ConcreteComponent
์ ์๋ก์ด ํ๋์ ๋์ ์ผ๋ก ์ถ๊ฐํ ์ ์๋ค.- ๊ฐ ๋ฐ์ฝ๋ ์ดํฐ ์์๋ ๊ตฌ์ฑ์์(Component)์ ๋๋ ๋ ํผ๋ฐ์ค๊ฐ ๋ค์ด์๋ ์ธ์คํด์ค ๋ณ์๊ฐ์๋ค.
Decorator
๋ ์์ ์ด ์ฅ์ํ ๊ตฌ์ฑ์์(Component)์ ๊ฐ์ ์ธํฐํ์ด์ค ๋๋ ์ถ์ ํด๋์ค๋ฅผ ๊ตฌํํ๋ค.ConcreteDecoratorA
,ConcreteDecoratorB
์๋ ๊ทธ ๊ฐ์ฒด๊ฐ ์ฅ์ํ๊ณ ์๋(๋ฐ์ฝ๋ ์ดํฐ๊ฐ ๊ฐ์ธ๊ณ ์๋ Component๊ฐ์ฒด)์ ์ํ ์ธ์คํด์ค ๋ณ์๊ฐ ์๋ค. ๋ฐ๋ผ์ ๋ฐ์ฝ๋ ์ดํฐ๋ Component์ ์ํ๋ฅผ ํ์ฅํ ์ ์๋ค.ConcreteDecoratorA
,ConcreteDecoratorB
๋ฐ์ฝ๋ ์ดํฐ์์ ์๋ก์ด ๋ฉ์๋๋ฅผ ์ถ๊ฐํ ์๋ ์๋ค. ํ์ง๋ง ์ผ๋ฐ์ ์ผ๋ก ์๋ก์ด ๋ฉ์๋๋ฅผ ์ถ๊ฐํ๋ ๋์ Component์ ์๋ ์๋ ๋ฉ์๋๋ฅผ ํธ์ถํ๊ธฐ ์ , ๋๋ ํ์ ๋ณ๋์ ์์ ์ ์ฒ๋ฆฌํ๋ ๋ฐฉ์์ผ๋ก ์๋ก์ด ๊ธฐ๋ฅ์ ์ถ๊ฐํ๋ค.