![èŠåºãç»å](https://assets.st-note.com/production/uploads/images/143298432/rectangle_large_type_2_a09db1591eff87188c8c43bdee31457a.png?width=1200)
ð Decoratorãã¿ãŒã³
Decoratorãã¿ãŒã³ã¯æãçããŠããã€ã¡ãŒãžãDecoratorãã¿ãŒã³ãšã¹ãã©ããžãšã®éãã¯ã¬ãã(è)ãšã¹ãã³(ç®è)ã®éããååž°çãªãã¿ãŒã³çµå
ãªããžã§ã¯ãæåããã°ã©ãã³ã°ã«ãããŠããã³ã¬ãŒã¿ãŒã»ãã¿ãŒã³ã¯ãåãã¯ã©ã¹ã®ä»ã®ã€ã³ã¹ã¿ã³ã¹ã®æ¯ãèãã«åœ±é¿ãäžããããšãªããåã
ã®ãªããžã§ã¯ãã«åçã«æ¯ãèããè¿œå ã§ããããã«ãããã¶ã€ã³ã»ãã¿ãŒã³ã§ãã
ãã³ã¬ãŒã¿ãŒã»ãã¿ãŒã³ã䜿ããšããããªããžã§ã¯ãã®æ©èœãéçã«ããããã¯å Žåã«ãã£ãŠã¯å®è¡æã«ãåãã¯ã©ã¹ã®ä»ã®ã€ã³ã¹ã¿ã³ã¹ãšã¯ç¬ç«ããŠæ¡åŒµïŒè£ 食ïŒããããšãã§ããã
ããã¯ãå ã®ã¯ã©ã¹ãã©ããããæ°ããDecoratorã¯ã©ã¹ãèšèšããããšã§å®çŸã§ããŸãããã®ã©ããã³ã°ã¯ã次ã®ãããªäžé£ã®æé ã§å®çŸã§ããŸãïŒ
1.å ã®Componentã¯ã©ã¹ãDecoratorã¯ã©ã¹ã«ãµãã¯ã©ã¹åããïŒUMLãã€ã¢ã°ã©ã ãåç §ïŒïŒ
2.Decorator ã¯ã©ã¹ã«ããã£ãŒã«ããšã㊠Component ãã€ã³ã¿ãŒãè¿œå ããŸãïŒ
3.Decoratorã¯ã©ã¹ã§ãDecoratorã³ã³ã¹ãã©ã¯ã¿ã«Componentãæž¡ããŠComponentãã€ã³ã¿ãåæåããïŒ
4.Decoratorã¯ã©ã¹ã§ããã¹ãŠã®Componentã¡ãœãããComponentãã€ã³ã¿ã«è»¢éããã
5.ConcreteDecoratorã¯ã©ã¹ã§ãåäœãå€æŽããå¿ èŠãããComponentã¡ãœããããªãŒããŒã©ã€ãããã
ãã®ãã¿ãŒã³ã¯ãè€æ°ã®ãã³ã¬ãŒã¿ãŒãéããããšã§ããªãŒããŒã©ã€ããããã¡ãœããã«æ°ããæ©èœãè¿œå ã§ããããã«èšèšãããŠããŸãã
Javaãš.NET Frameworkã®I/O Streamså®è£ ã¯ããã³ã¬ãŒã¿ãŒã»ãã¿ãŒã³ãåãå ¥ããŠããã
Adapterã¯ã€ã³ã¿ãŒãã§ãŒã¹ã®äºææ§ããDecoratorã¯åçãªè²¬åè¿œå ããStrategyã¯ã¢ã«ãŽãªãºã ã®æè»ãªåãæ¿ãããããããµããŒãããŸãã
ããã¯åããããã
ãããªããããããµã³ãã«ããã²èŠãã
function run() {
var user = new User("Kelly");
user.say();
var decorated = new DecoratedUser(user, "Broadway", "New York");
decorated.say();
log.show();
}
ãœãŒã¹ãèŠãã ãã§äœãããŠãããã®ãã¯ããããããããã³ã¬ãŒã¿ãŒãšããååã被ããŠãæããéåžžã«åãããããã
æã«ã¯ãã¯ã©ã¹å šäœã§ã¯ãªããåã ã®ãªããžã§ã¯ãã«è²¬ä»»ãè¿œå ãããããšããããŸããäŸãã°ãã°ã©ãã£ã«ã«ã»ãŠãŒã¶ãŒã»ã€ã³ã¿ãŒãã§ãŒã¹ã»ããŒã«ãããã§ã¯ãå¢çç·ã®ãããªããããã£ãã¹ã¯ããŒã«ã®ãããªåäœããŠãŒã¶ãŒã»ã€ã³ã¿ãŒãã§ãŒã¹ã»ã³ã³ããŒãã³ãã«è¿œå ããããšãã§ããŸãã
GoFã§ã®ãã³ã¬ãŒã¿ãŒã®åœ¹å²ã¯ãã³ã¬ãŒãããã¬ã€ã€ãŒã®ãããªæ©èœã«æ³šç®ããããŸãã§ãã©ãŠããã®ããã«éãçãããŠãã³ã¬ãŒã·ã§ã³ããããã ããéæãªãã¬ãŒããšããŠã€ã¡ãŒãžããŠããããã ãGUIã ãã§ãªããã¹ããªãŒã ã®äŸã説æããŠãããJavaã®ããšããç¥ããªããããªããããããã¡ãŒãã¯ãã³ã¬ãŒã¿ã®å¥œäŸãšããããšã«ãªãã
ãã£ã¡ã®ã»ãããª
ãã³ã¬ãŒã¿ãŒã¯ãªããžã§ã¯ãã®è¡šå±€ãå€ãããã¹ãã©ããžã¯å èããããã
A decorator lets you change the skin of an object; a strategy lets you change the guts. These are two alternative ways ofãchanging an object.ãã³ã¬ãŒã¿ãŒã¯ãªããžã§ã¯ãã®è¡šå±€ãå€ãããã¹ãã©ããžã¯å èããããã
ã¬ããïŒèç©ïŒãå€ããããããã¹ãã©ããžã䜿ããšãªã
Decorator ãã¿ãŒã³ã¯ãæ¢åã®ãªããžã§ã¯ãã«åçã«æ°ãã責任ãæ©èœãè¿œå ããããã®ãã¶ã€ã³ãã¿ãŒã³ã§ãã
ãã®ãã¿ãŒã³ã¯ããµãã¯ã©ã¹åã䜿çšããããšãªãããªããžã§ã¯ãã®æ©èœãæ¡åŒµããããã«äœ¿çšãããŸããDecorator ãã¿ãŒã³ã®éµãšãªãç¹ã¯ãåãåºæ¬ã€ã³ã¿ãŒãã§ãŒã¹ãŸãã¯æœè±¡ã¯ã©ã¹ãç¶æ¿ãŸãã¯å®è£
ããããšã«ãã£ãŠããªããžã§ã¯ããã©ããããããã³ã¬ãŒã¿ããšããŠåäœããã¯ã©ã¹ãå®çŸ©ããããšã§ãã
ååž°çãªçµåãšã¯ãDecorator ãã¿ãŒã³ã®ç¹åŸŽçãªæ§é ã§ããããã³ã¬ãŒã¿ãå¥ã®ãã³ã¬ãŒã¿ãåºæ¬ã®ã³ã³ããŒãã³ããã©ããã§ããããšãæå³ããŸããããã«ãããè€æ°ã®ãã³ã¬ãŒã¿ãé£éçã«çµã¿åãããããšã§ãåçã«ãªããžã§ã¯ãã®æ¯ãèãã責任ãã«ã¹ã¿ãã€ãºããããšãã§ããŸãã
äŸïŒ
èããŠã¿ãŠãã ããïŒãããããªã¹ããªãŒãã³ã°ãµãŒãã¹ã«ãŠãåºæ¬çãªãããªåçæ©èœããããŸãããããåºæ¬çãªã³ã³ããŒãã³ãã§ãã
ãããã以äžã®ãããªè¿œå æ©èœãæã€ãã³ã¬ãŒã¿ããããŸãïŒ
AdDecorator: ãããªã®åã«åºåãè¿œå ããŸãã
SubtitleDecorator: ãããªã«åå¹ãè¿œå ããŸãã
WatermarkDecorator: ãããªã«éãããè¿œå ããŸãã
ãããã®ãã³ã¬ãŒã¿ã䜿çšããŠãåºæ¬ã®ãããªåçæ©èœã«äžèšã®è¿œå æ©èœãåçã«çµã¿èŸŒãããšãã§ããŸããããã«ããããã®ãã³ã¬ãŒã¿ãååž°çã«çµã¿åãããããšã§ãè€æ°ã®æ©èœãçµã¿åããããããªåçäœéšãäœæããããšãã§ããŸãã
äŸãã°ãåºåä»ãã§åå¹ã衚瀺ããããããªãäœæããå ŽåãAdDecorator ã§ãããªãã©ãããã次ã«ãã®çµæã SubtitleDecorator ã§ã©ããããŸãããã®ããã«ãååž°çãªçµåã䜿çšããŠãå¿
èŠãªæ©èœã®çµã¿åãããç°¡åã«æ§ç¯ããããšãã§ããŸãã
ãã®ã¢ãããŒãã®ã¡ãªããã¯ãç°ãªãæ©èœã®çµã¿åãããæè»ã«äœæã§ããããšããŸãæ°ããæ©èœãè¿œå ããéã«æ¢åã®ã³ãŒããå€æŽããããšãªããã³ã¬ãŒã¿ãšããŠè¿œå ã§ããããšã§ãã
![](https://assets.st-note.com/img/1694825378944-igF0zNmO9T.png?width=1200)
Dot
digraph DecoratorPattern {
node [shape=record]
Component [label="{Component|+operation(): void}"]
ConcreteComponent [label="{ConcreteComponent|+operation(): void}"]
Decorator [label="{Decorator|+operation(): void\n+addedBehavior(): void}"]
ConcreteDecoratorA [label="{ConcreteDecoratorA|+operation(): void\n+addedBehavior(): void}"]
ConcreteDecoratorB [label="{ConcreteDecoratorB|+operation(): void\n+addedBehavior(): void}"]
Component -> ConcreteComponent [arrowhead="none"]
Component -> Decorator [arrowhead="none"]
Decorator -> ConcreteDecoratorA [arrowhead="none"]
Decorator -> ConcreteDecoratorB [arrowhead="none"]
Decorator -> Component [label="decorates" dir="back"]
{rank=same; ConcreteDecoratorA ConcreteDecoratorB}
}
![](https://assets.st-note.com/img/1715345161881-FQQs4NZKzX.png?width=1200)
![](https://assets.st-note.com/img/1717751205470-wtLHYzW8he.png?width=1200)
ãé¡ãèŽããŸã