็ฎๅฝ (7)
[๋์์ธ ํจํด] Decorator ํจํด
Decorator ํจํด์ ๋์์ธ ํจํด์์ ๊ตฌ์กฐ ํจํด์ ํด๋นํ๋ค. ๊ตฌ์กฐ ํจํด์ ํด๋์ค๋ ๊ฐ์ฒด๋ฅผ ์กฐํฉํ์ฌ ๋ ํฌ๊ณ ๋ณต์กํ ๊ตฌ์กฐ๋ฅผ ๋ง๋ค๊ฑฐ๋ ๊ธฐ์กด ๊ตฌ์กฐ๋ฅผ ํ์ฅํ๋๋ฐ ์ฌ์ฉํ๋ค. ๊ทธ๋ฆฌ๊ณ ๋ฐ์ฝ๋ ์ดํฐ ํจํด์ ํด๋์ค์ ๊ธฐ๋ฅ์ ์ถ๊ฐํ๊ธฐ ์ํด์ ๋ค๋ฅธ ๊ฐ์ฒด๋ฅผ ๋ง๋ถ์ด๋ ํจํด์ผ๋ก, ๊ฐ์ฒด์ ๊ฒฐํฉ์ ํตํด ๋์ ์ผ๋ก ์ ์ฐํ๊ฒ ํ์ฅํ ์ ์๋ ํจํด์ด๋ค.
๋ฐ์ฝ๋ ์ดํฐ ํจํด์ ๊ธฐ์กด์ ๊ตฌํ๋์ด์๋ ํด๋์ค๋ฅผ ์์ ํ์ง ์๊ณ , ๊ธฐ๋ฅ์ ์ถ๊ฐํ ์ ์๋ ์ค๊ณ ํจํด(OCP)์ผ๋ก ๊ธฐ๋ฅ ํ์ฅ์ด ํ์ํ ๋ ๋์ ์ธ ๊ธฐ๋ฅ์ด ํ์ํ ๋ subclass๋ก์ ์์ ๋์์ผ๋ก ์ฌ์ฉํ ์ ์๋ ํจํด์ด๋ค.
1. Decorator ํจํด ์ ์ฉ
์ด๋ฒ์ Decorator ํจํด์ ํ ํ์ถ๊ฐ๊ฐ ๊ฐ๋ฅํ๋ฉฐ, ํ ํ๋ณ๋ก ๊ฐ๊ฒฉ์ด ๋ฌ๋ผ์ง๋ ์๊ตฌ์ฌํญ์ ๊ตฌํํ๋ ๊ฒ์ ๋ฐํ์ผ๋ก ์ค๋ช ํ๊ฒ ์ต๋๋ค.
์๊ตฌ ์ฌํญ
์๋ฃ๋ ์์คํ๋ ์, ๋ธ๋ ์ปคํผ, ๋ฐํฌ ์ปคํผ๊ฐ ์๋ค.
ํ ํ์ ๋์ , ์ฐ์ , ํํํฌ๋ฆผ์ด ์๋ค.
ํ ํ ์ถ๊ฐ์ ๊ฐ๊ฒฉ๋ ์ถ๊ฐ์ด ์ถ๊ฐ๋๋ค.
1.1 Decorator ํจํด ์์ด ๊ตฌํ

Beverage Class
public class Beverage { private Milk milk; private Soy soy; ... public String getDescription() {} public Integer cost() {} public void setMilk() {} public Boolean hasMilk() {} public Boolean hasSoy() {} public Boolean hasWhip() {} ...}Beverage ์์๋ฐ๋ ์์คํ๋ ์
public class Espresso extends Beverage { public String getDescription() { String description = "์์คํ๋ ์ ์๋ฃ"; String prefix = ""; if(this.hasMilk()) { prefix = prefix+"์ฐ์ , "; } ... Boolean hasCondiment = prefix.length()>0; description = hasCondiment? prefix + description : description; return description; } public Integer cost() { Integer price = 3000; if(this.hasMilk()) { price += 300; } ... return price }}๋ฐ์ฝ๋ ์ดํฐ ํจํด์์ด ๋จ์ํ ์์์ ํตํ Subclass๋ก ๊ตฌํํ๋ฉด ์์ ๊ฐ์ด ๋์จ๋ค.
์ฅ์
๊ตฌํ์ด ์ฝ๋ค.
๋จ์
ํ ํ์ ์ข ๋ฅ๊ฐ ๋ง์์ง๋ฉด ๊ฐ๊ฐ ์๋ฃ์ ์๋ธํด๋์ค์์ ๊ฐ๊ฒฉ ํจ์ ๋ณ๊ฒฝ ํ์(๋ฌผ๋ก ๊ฐ๊ฒฉ์ field๋ก ์ ์ฅํ์ฌ ์ฐ๋ฉด ํด๊ฒฐ)
ํน์ ์๋ฃ์ ํน์ ํ ํ ์ถ๊ฐ๊ฐ ๋ถ๊ฐํ๋ฉด ๋ชจ๋ ์์ธ ๊ตฌํ ํด์ผํ๋ค.
์ปดํ์ผ์ ํ๋์ด ์์ ํ ๊ฒฐ์ ๋๋ค.
1.2 Decorator ํจํด ์ ์ฉ ๊ตฌํ

์ ๊ทธ๋ฆผ์ Decorator ํจํด์ ์ ์ฉํ์ ๋ ๊ทธ๋ฆผ์ด๋ค. ์๋ฃ ์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํ๋ ์ฒจ๊ฐ๋ฌผ ํด๋์ค๋ฅผ ๋ง๋ค๊ณ , ๋์ , ์ฐ์ , ํํํฌ๋ฆผ์ ์ด ์ฒจ๊ฐ๋ฌผ ํด๋์ค๋ฅผ ๊ตฌํํ๋๋ฐ, ์๋ฃ ํ๋๋ฅผ ๋ณ๋๋ก ๊ฐ์ ธ์ผํ๋ค.
๊ฐ ๊ตฌ์ ์ฒจ๊ฐ๋ฌผ๋ค์ ๋ชจ๋ ์๋ฃ ์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํ ์ฒจ๊ฐ๋ฌผ ์ธํฐํ์ด์ค(์ถ์ ํด๋์ค, ํด๋์ค)๋ฅผ ์์๋ฐ๊ณ ์๋ค. ์ด๋ ํ๋์ ์์์ ์ํ ๊ฒ์ด ์๋๋ผ ํ์์ ๋ง์ถ๊ธฐ ์ํ ์์์์ ๋ถ๋ช ํ ํด์ผํ๋ค. ๊ทธ๋ฆฌ๊ณ ์๋ฃ ์ธํฐํ์ด์ค์ ๊ฐ์ฒด๋ฅผ ๊ตฌ์ ์ฒจ๊ฐ๋ฌผ์์ ๊ตฌ์ฑ์ผ๋ก ๊ฐ์ ธ ํ์๋ฅผ ์ฐ๊ฒฐํ์๋ค.
CondimentDecorator (์ฒจ๊ฐ๋ฌผ)
abstract class CondimentDecorator implements Beverage { protected Beverage decoratedBeverage; // ๊พธ๋ฉฐ์ง๋ ์๋ฃ public CondimentDecorator (Beverage decoratedBeverage) { this.decoratedBeverage = decoratedBeverage; }}ํํ ํฌ๋ฆผ (์ฒจ๊ฐ๋ฌผ ๊ตฌํ)
class Whip extends CondimentDecorator { private discount = 100; public Whip (Beverage decoratedBeverage) { super(decoratedBeverage); } public Integer cost() { return this.decoratedBeverage.cost() + calculateCost(); } public Integer calculateCost() { return 500 - getDiscount(); } public String getDescription() { return this.decoratedBeverage.getDescription() + "ํํ"; } private Integer getDiscount() { return discount; }}์ค๋ช ์ ๋ ฌ (์ฒจ๊ฐ๋ฌผ ๊ตฌํ)
class PrettyDescription extends CondimentDecorator { public PrettyDescription (Beverage decoratedBeverage) { super(decoratedBeverage); } public Integer cost() { return this.decoratedBeverage.cost(); } public String getDescription() { return sortDescription(this.decoratedBeverage.getDescription()); } private String sortDescription(String description) { String prettyDescription = ์ ๋ ฌ(description); return prettyDescription; }}์์คํ๋ ์ (์๋ฃ ๊ตฌํ)
public class Espresso implements Beverage { public String getDescription() { return "์์คํ๋ ์"; } public Integer cost() { return 3000; }}public class CoffeeStore { public static void main(String args[]) { Beverage beverage = new Espresso(); beverage = new Whip(beverage); // ํํํฌ๋ฆผ ์ถ๊ฐ, espresso.cost() + whip.cost() = 3000+ 400 = 3400; beverage = new Whip(beverage); // ํํํฌ๋ฆผ ์ถ๊ฐ, 3400+ 400 = 3800;, description = "์์คํ๋ ์ ํํ ํํ" beverage = new Whip(beverage); // ํํํฌ๋ฆผ ์ถ๊ฐ, 3800+ 400 = 4200;, description = "์์คํ๋ ์ ํํ ํํ ํํ" beverage = new PrettyDescription(beverage); // ์ค๋ช
์ ๊น๋ํ๊ฒ ์ ๋ ฌ, description = "ํธ๋ฆฌํ ํํ ์์คํ๋ ์"; }}์ ์ฝ๋๊ฐ ๋ฐ์ฝ๋ ์ดํฐ ํจํด์ ์ ์ฉํ์ ๋ ์ฌ์ฉํ๋ ์ฝ๋๋ค. ๊ธฐ์กด ์๋ฃ ์ฝ๋๋ ๋ฌด์ํ ๋ง์ if๋ฌธ์ ํตํด ๋ถ๊ธฐํ์ฌ ๊ฐ๊ฒฉ๊ณผ ์ค๋ช ์ ๊ณ์ฐํ์ง๋ง, ์ด์ ๋ ๋ ๊ฐ๋จํ๊ฒ ๋ด๋ถ ๊ตฌํ์ ์์กดํ์ง ์๊ณ ๊ธฐ๋ฅ์ ํ์ฅํ ์ ์๋ค. ๊ทธ๋์ OCP๋ฅผ ๋ ์์งํค๊ฒ ๋ ์ ์ด๋ค. ๊ทธ๋ฐ๋ฐ ๋ฐ์ฝ๋ ์ดํฐ ํจํด์ ์ด๋ ๊ฒ ๋จ๋ ์ผ๋ก ์ฐ๊ธฐ ํ๋ค๋ค. ์๋ํ๋ฉด ์ค์ ๋น์ง๋์ค์์๋ ์์์ ์์กด์ด ์๊ณ ๋ ๋ก์ง์ด ๋ณต์กํ ๊ฒ์ด๊ธฐ ๋๋ฌธ์ด๋ค. ์๋ฅผ ๋ค๋ฉด ์ ์ฝ๋์์ PrettyDescription์ด๋ผ๋ ํด๋์ค๋ฅผ ๋ง๋ค์๋๋ฐ ์ด ์์๊ฐ ํญ์ ๋งจ ๋ค์ ์์ด์ผ ํ๋ ๊ฒ์ฒ๋ผ, ํํํฌ๋ฆผ์ ํญ์ ๋ง์ง๋ง ๋ถ๋ถ์์ ์ถ๊ฐ๋๋๋ก ๊ท์น์ ์ ํ๋ค๋ฉด ๊ทธ ๊ท์น์ ๋ฐ๋ผ์ผ ํ๋ค.
1.3 Decorator ํจํด ํ๊ณ๊ณผ ๋ณด์
๋ฐ์ฝ๋ ์ดํฐ ํจํด์ ๋จ๋ ์ผ๋ก ์ฐ์ด๊ธฐ ๋ณด๋ค๋, ์ด๋ฅผ ๋ณด์กฐํ๋ ์๋จ์ผ๋ก ํฉํ ๋ฆฌ, ๋น๋ ํจํด๋ฑ์ ํจ๊ป ์ฌ์ฉํ๋ค. ์๋ํ๋ฉด ์์ ๊ฐ์ด ์์์ ๋ฐ๋ผ ์๋์ด ๋ฌ๋ผ์ง๊ฑฐ๋, ํ ํ์ ์ถ๊ฐ๋ฅผ ์ทจ์ํด์ผ ํ๋ ๋ฌธ์ ๊ฐ ์๊ธฐ ๋๋ฌธ์ด๋ค.
์๋๋ ๊ฐ๋จํ ๋น๋ ํจํด์ผ๋ก ํ ํ ์ถ๊ฐ,์ทจ์๊ฐ ๊ฐ๋ฅํ๋๋ก ๋ง๋ ๊ฒ์ด๋ค. ์ฌ๊ธฐ์ ์ฐ์ ์์๋ฅผ ์ถ๊ฐํ์ฌ ์ ๋ ฌํ๋๋ก ํ๋ค๋๊ฐ ํ๋ ๋ฑ์ ๊ธฐ๋ฅ์ ์ถ๊ฐํ๋ฉด ๋๋ค.
public class CoffeeBuilder { private List<Function<Coffee, Coffee>> steps = new ArrayList<>(); public void addMilk() { steps.add((c) => new WithMilk(c)); } public void addWhip() { steps.add((c) => new WithWhip(c)); } public void undo() { steps.pop(); } public Coffee build(){ Coffee coffee = new BasicCoffee(); for (Function<Coffee, Coffee> step : steps) { coffee = step.apply(coffee); } return coffee; }}2. ํ๋ก ํธ์๋์ Decorator ํจํด (๊ณ ์ฐจ ์ปดํฌ๋ํธ, Hoc)
ํ๋ก ํธ์๋์์๋ ๋ฐ์ฝ๋ ์ดํฐ ํจํด์ HOC(๊ณ ์ฐจ ์ปดํฌ๋ํธ) ๋ง๋ค๋ ์ฌ์ฉํ๋ค. ์๋ฅผ ๋ค๋ฉด ํน์ ์ปดํฌ๋ํธ๊ฐ ๋์ด์ง ๋ ๋ก๊ทธ๋ฅผ ์ฐ๋๋ค๋๊ฐ, ์ด๋ค ๊ถํ์ ์ฒดํฌํ์ฌ ์ ๊ทผ์ ์ ํํ๋ ๊ธฐ๋ฅ์ ๋ง๋ค๋ ์ ์ฉํ๋ค.
// withAuthorization.tsxfunction withAuthorization<P extends object>(WrappedComponent: React.ComponentType<P>) { return function (wrappedComponentProps: P) { const {userRole} = useAuth(); const isAuthorized = userRole === "ADMIN"; if (!isAuthorized) return <div>์ ๊ทผ ๊ถํ์ด ์์ต๋๋ค.</div>; return <WrappedComponent {...wrappedComponentProps} />; };}// withErrorBoundary.tsxfunction withErrorBoundary<P>(WrappedComponent: React.ComponentType<P>) { return class ErrorBoundary extends Component<P, State> { constructor(props: P) { super(props); this.state = {hasError: false}; } static getDerivedStateFromError() { return {hasError: true}; } componentDidCatch(error: Error, errorInfo: React.ErrorInfo) { console.error("ErrorBoundary caught an error", error, errorInfo); } render() { if (this.state.hasError) { return <FallbackComponent />; } return <WrappedComponent {...this.props} />; } };}// SecretDashBoard.tsxconst SecretDashBoard = () => { return <div>๋์ฌ๋ณด๋</div>;};export default withErrorBoundary(withAuthorization(SecretDashBoard));// App.tsxfunction App() { return <SecretDashBoard />; // ๊ถํ ์์ผ๋ฉด ๋ค๋ฅธ ํ๋ฉด ๋ธ. ์๋ฌ๊ฐ ๋จ๋ฉด ์๋ฌ ์ปดํฌ๋ํธ ๋ธ.}์ ์ฝ๋๊ฐ ๊ถํSecretDashBoard์ ErrorBoundary์ Auth Boundary๊ฐ ํจ๊ผ ์ ์ฉ๋ ์ฝ๋์ด๋ค. ์ด๋ค ์ปดํฌ๋ํธ๋ผ๋ ์ฝ๊ฒ ๊ถํ ์ค์ ์ด ๊ฐ๋ฅํ๋ค. ๋ฌผ๋ก ๋งํ๋๋ฐ๋ก ์ ์ฉ ์์์ ๋ฐ๋ผ ๊ฒฐ๊ณผ๊ฐ ์์ ํ ๋ฌ๋ผ์ง ์ ์๋ค. (์๋ฌ์ ๊ถํ์ด ๋ชจ๋ ์ถฉ์กฑ๋์ง ๋ชปํ ๊ฒฝ์ฐ)
๋ง๋ฌด๋ฆฌ
์ด๋ ๊ฒ ๋์์ธ ํจํด์ ํ๋ก ํธ์ ๋ฐฑ์๋ ๋ชจ๋์์ ์๊ฒ ๋ชจ๋ฅด๊ฒ ์ ์ฉ๋๋ค. (๋ฌผ๋ก ํ๋ก ํธ๋ ์ฝ๊ฐ ์ต์ง์ค๋ฌ์ด ๋ฉด์ด ์๋ค) ํ์ง๋ง ๊ทธ์ ๊ด๊ณ์์ด ๋์์ธ ํจํด์ ๊ณต๋ถํด์ผ ๋ ์ข์ ์ฝ๋์ ์ค๊ณ๊ฐ ๊ฐ๋ฅํ ๊ฒ ๊ฐ๋ค. ์์ ํ ๋ง์๋๋ก ์ํฉ์ ๋ฐ๋ผ ์์ฉ์ด ๊ฐ๋ฅํ๋๋ก ๋ ธ๋ ฅํด์ผ๊ฒ ๋ค.
์ฌ๋ฌ๋ถ๋ค๋ ํ์ดํ !!
