1.1. Intent ¶
request ๋ฅผ ๊ฐ์ฒด๋ก ์บก์ํ ์ํด์ผ๋ก์ ๋ค๋ฅธ request๋ค์ ๊ฐ์ง ํด๋ผ์ด์ธํธ๋ฅผ ์ธ์ํ์ํค๊ฑฐ๋, request๋ฅผ queueํ๊ฑฐ๋ ๋๊ธฐ์ํค๋ฉฐ, undo๊ฐ ๊ฐ๋ฅํ ๋ช
๋ น์ ์ง์ํ๋ค.
1.3. Motivation ¶
๋๋๋ก ์์ฒญ๋ฐ์ ๋ช
๋ น์ด๋ request๋ฅผ ๋ฐ๋ ๊ฐ์ฒด์ ๋ํ ์ ๋ณด์์ด ๊ฐ์ฒด๋ค์๊ฒ request๋ฅผ ๋๊ฒจ์ค ๋๊ฐ ์๋ค. ์๋ฅผ ๋ค์ด user interface tookit์ button์ด๋ menu์ฒ๋ผ ์ฌ์ฉ์ ์
๋ ฅ์ ๋ํด ์๋ตํ๊ธฐ ์ํด ์์ฒญ์ ์ฒ๋ฆฌํ๋ ๊ฐ์ฒด๋ค์ ํฌํจํ๋ค. ํ์ง๋ง, ์ค์ง toolkit์ ์ฌ์ฉํ๋ ์ดํ๋ฆฌ์ผ์ด์
๋ง์ด ์ด๋ค ๊ฐ์ฒด๊ฐ ์ด๋ค์ผ์ ํด์ผ ํ ์ง ์๊ณ ์์ผ๋ฏ๋ก, toolkit์ button์ด๋ menu์ ๋ํด์ ์์ฒญ์ ๋ํด ๋ช
์์ ์ผ๋ก ๊ตฌํ์ ํ ์ ์๋ค. toolkit ๋์์ด๋๋ก์ ์ฐ๋ฆฌ๋ request๋ฅผ ๋ฐ๋ ๊ฐ์ฒด๋ request๋ฅผ ์ฒ๋ฆฌํ operations์ ๋ํด ์์ง ๋ชปํ๋ค.
Command Pattern์ request ๋ฅผ ๊ฐ์ฒดํํจ์ผ๋ก์ toolkit ๊ฐ์ฒด๋ก ํ์ฌ๊ธ ๋ถํน์ ํ ์ดํ๋ฆฌ์ผ์ด์
๊ฐ์ฒด์ ๋ํ request๋ฅผ ๋ง๋ค๊ฒ ํ๋ค. ์ด ๊ฐ์ฒด๋ ๋ค๋ฅธ ๊ฐ์ฒด์ฒ๋ผ ์ ์ฅ๋ ์ ์์ผ๋ฉฐ pass around ๊ฐ๋ฅํ๋ค. ์ด pattern์ key๋ ์ํํ ๋ช
๋ น์ด์ ๋ํ ์ธํฐํ์ด์ค๋ฅผ ์ ์ธํ๋ ์ถ์ Command class์ ์๋ค. ์ด ์ธํฐํ์ด์ค์ ๊ฐ์ฅ ๋จ์ํ ํํ์์๋ ์ถ์์ ์ธ Execute operation์ ํฌํจํ๋ค. ๊ตฌ์ฒดํ๋ Command subclass๋ค์ request์ ๋ํ receiver๋ฅผ instance ๋ณ์๋ก ์ ์ฅํ๊ณ request๋ฅผ invokeํ๊ธฐ ์ํ Execute operation์ ๊ตฌํํจ์ผ๋ก์ receiver-action ์ง์ ๊ตฌ์ฒดํ์ํจ๋ค. The receiver has the knowledge required to carry out the request.
Menu๋ ์ฝ๊ฒ Command Object๋ก ๊ตฌํ๋ ์ ์๋ค. Menu ์ ๊ฐ๊ฐ์ ์ ํ์ ๊ฐ๊ฐ MenuItem ํด๋์ค์ ์ธ์คํด์ค์ด๋ค. Application ํด๋์ค๋ ์ด ๋ฉ๋ด๋ค๊ณผ ๋๋จธ์ง ์ ์ ์ธํฐํ์ด์ค์ ๋ฐ๋ผ์ ๋ฉ๋ด์์ดํ
์ ๊ตฌ์ฑํ๋ค. Application ํด๋์ค๋ ์ ์ ๊ฐ ์ด Document ๊ฐ์ฒด์ track์ ์ ์งํ๋ค.
์ดํ๋ฆฌ์ผ์ด์
์ ๊ฐ๊ฐ์ ๊ตฌ์ฒด์ ์ธ Command ์ subclass๋ค๋ก ๊ฐ๊ฐ๊ฐMenuItem ๊ฐ์ฒด๋ฅผ ์ค์ ํ๋ค. ์ฌ์ฉ์๊ฐ MenuItem์ ์ ํํ์๋ MenuItem์ ๋ฉ๋ด์์ดํ
์ ํด๋น ๋ช
๋ น์ผ๋ก์ Execute oeration์ ํธ์ถํ๊ณ , Execute๋ ์ค์ ์ ๋ช
๋ น์ ์ํํ๋ค. MenuItem๊ฐ์ฒด๋ค์ ์์ ๋ค์ด ์ฌ์ฉํ Command์ subclass์ ๋ํ ์ ๋ณด๋ฅผ ๊ฐ์ง๊ณ ์์ง ์๋ค. Command subclass๋ ํด๋น request์ ๋ํ receiver๋ฅผ ์ ์ฅํ๊ณ , receiver์ ํ๋๋ ๊ทธ ์ด์์ ๋ช
๋ น์ด๋ค์ invokeํ๋ค.
์๋ฅผ ๋ค์ด PasteCommand๋ clipboard์ ์๋ text๋ฅผ Document์ ๋ถ์ด๋ ๊ธฐ๋ฅ์ ์ง์ํ๋ค. PasteCommand ์ receiver๋ ์ธ์คํด์คํํ ๋ ์ค์ ๋์ด์๋ Docuemnt๊ฐ์ฒด์ด๋ค. Execute ๋ช
๋ น์ ํด๋น ๋ช
๋ น์ receiver์ธ Document์ Paste operation ์ invoke ํ๋ค.
OpenCommand์ Execute operation์ ๋ค๋ฅด๋ค. OpenCommand๋ ์ฌ์ฉ์์๊ฒ ๋ฌธ์ ์ด๋ฆ์ ๋ฌผ์๋ค, ๋์ํ๋ Document ๊ฐ์ฒด๋ฅผ ๋ง๋ค๊ณ , ํด๋น ๋ฌธ์๋ฅผ ์ฌ๋ ์ดํ๋ฆฌ์ผ์ด์
์ ๋ฌธ์๋ฅผ ์ถ๊ฐํ ๋ค (MDI๋ฅผ ์๊ฐํ ๊ฒ) ๋ฌธ์๋ฅผ ์ฐ๋ค.
๋๋๋ก MenuItem์ ์ฐ์๋ ๋ช
๋ น์ด๋ค์ ์ผ๊ด์ํ์ ํ์๋ก ํ๋ค. ์๋ฅผ ๋ค์ด์ ํด๋น ํ์ด์ง๋ฅผ ์ค์์ ๋๊ณ ์ผ๋ฐํฌ๊ธฐํ ์ํค๋ MenuItem์ CenterDocumentCommand ๊ฐ์ฒด์ NormalSizeCommand ๊ฐ์ฒด๋ก ๋ง๋ค ์ ์๋ค. ์ด๋ฌํ ๋ฐฉ์์ผ๋ก ๋ช
๋ น์ด๋ค์ ์ด์ด์ง๊ฒ ํ๋ ๊ฒ์ ์ผ๋ฐ์ ์ด๋ฏ๋ก, ์ฐ๋ฆฌ๋ ๋ณต์๋ช
๋ น์ ์ํํ๊ธฐ ์ํ MenuItem์ ํ์ฉํ๊ธฐ ์ํด MacroCommand๋ฅผ ์ ์ํ ์ ์๋ค. MacroCommand๋ ๋จ์ํ ๋ช
๋ น์ด๋ค์ sequence๋ฅผ ์ํํ๋ Command subclass์ ๊ตฌ์ฒดํ์ด๋ค. MacroCommand๋ MacroCommand๋ฅผ ์ด๋ฃจ๊ณ ์๋ command๋ค์ด ๊ทธ๋ค์ receiver๋ฅผ ์ ์ํ๋ฏ๋ก ๋ช
์์ ์ธ receiver๋ฅผ ๊ฐ์ง์ง ์๋๋ค.
์ด๋ฌํ ์๋ค์์, ์ด๋ป๊ฒ Command pattern์ด ํด๋น ๋ช
๋ น์ invokeํ๋ ๊ฐ์ฒด์ ๋ช
๋ น์ ์ํํ๋ ์ ๋ณด๋ฅผ ๊ฐ์ง ๊ฐ์ฒด๋ฅผ ๋ถ๋ฆฌํ๋์ง ์ฃผ๋ชฉํ๋ผ. ์ด๋ฌํจ์ ์ ์ ์ธํฐํ์ด์ค๋ฅผ ๋์์ธํจ์ ์์ด์ ๋ง์ ์ ์ฐ์ฑ์ ์ ๊ณตํ๋ค. ์ดํ๋ฆฌ์ผ์ด์
์ ๋จ์ง menu์ push button์ด ๊ฐ์ ๊ตฌ์ฒด์ ์ธ Command subclass์ ์ธ์คํด์ค๋ฅผ ๊ณต์ ํจ์ผ๋ก์ menu ์ push button ์ธํฐํ์ด์ค ์ ๊ณตํ ์ ์๋ค. ์ฐ๋ฆฌ๋ ๋์ ์ผ๋ก command๋ฅผ ๋ฐ๊ฟ ์ ์์ผ๋ฉฐ, ์ด๋ฌํจ์ context-sensitive menu ๋ฅผ ๊ตฌํํ๋๋ฐ ์ ์ฉํ๋ค. ๋ํ ์ฐ๋ฆฌ๋ ๋ช
๋ น์ด๋ค์ ์ปค๋ค๋ ๋ช
๋ น์ด์ ํ๋๋ก ์กฐํฉํจ์ผ๋ก์ command scripting์ ์ง์ํ ์ ์๋ค. ์ด๋ฌํ ๋ชจ๋ ๊ฒ์ request๋ฅผ issueํ๋ ๊ฐ์ฒด๊ฐ ์ค์ง ์ด๋ป๊ฒ issueํ ํ๋์ง๋ง ์๊ณ ์์ผ๋ฉด ๋๊ธฐ๋๋ฌธ์ ๊ฐ๋ฅํ๋ค. request๋ฅผ ๋ํ๋ด๋ ๊ฐ์ฒด๋ ์ด๋ป๊ฒ request๊ฐ ์ํ๋์ด์ผ ํ ์ง ์ ํ์๊ฐ ์๋ค.
1.4. Applicability ¶
๋ค์๊ณผ ๊ฐ์ ๊ฒฝ์ฐ์ CommandPattern์ ์ด์ฉํ๋ผ.
- MenuItem ๊ฐ์ฒด๊ฐ ํ๋ ค๋ ์ผ์ ๋์ด์ ์ํํ๋ ค๋ action์ ์ํด ๊ฐ์ฒด๋ฅผ์ ์ธ์ํ์ํฌ๋. ํ๋ก๊ทธ๋๋จธ๋ procedural language์์์ callback ํจ์์ฒ๋ผ ์ธ์ํ์ํฌ ์ ์๋ค. Command๋ callbackํจ์์ ๋ํ ๊ฐ์ฒด์งํฅ์ ์ธ ๋์์ด๋ค.
- ๋ค๋ฅธ ์๊ฐ๋์ request๋ฅผ ๊ตฌ์ฒดํํ๊ฑฐ๋ queueํ๊ฑฐ๋ ์ํํ๊ธฐ ์ํ ๋. Command ๊ฐ์ฒด๋ request์ ๋
๋ฆฝ์ ์ธ lifetime์ ๊ฐ์ง ์ ์๋ค. ๋ง์ผ request์ receiver๊ฐ ๊ณต๊ฐ ๋
๋ฆฝ์ ์ธ ๋ฐฉ๋ฒ์ผ๋ก (๋คํธ์ํฌ ๋ฑ) ์ฃผ์๋ฅผ ํํํ ์ ์๋ค๋ฉด ํ๋ก๊ทธ๋๋จธ๋ request์ ๋ํ Command ๊ฐ์ฒด๋ฅผ ๋ค๋ฅธ ํ๋ก์ธ์ค์๊ฒ ์ ๋ฌํ์ฌ ์ฒ๋ฆฌํ ์ ์๋ค.
- undo ๊ธฐ๋ฅ์ ์ง์ํ๊ธฐ ์ํ ๋. Command์ Execute operation์ ํด๋น Command์ ํจ๊ณผ๋ฅผ ๋๋๋ฆฌ๊ธฐ ์ํ state๋ฅผ ์ ์ฅํ ์ ์๋ค. Command ๋ Execute ์ํ์ ํจ๊ณผ๋ฅผ ๋๋๋ฆฌ๊ธฐ ์ํ Unexecute operation์ ์ธํฐํ์ด์ค๋ก์ ์ถ๊ฐํด์ผ ํ๋ค. ์ํ๋ command๋ history list์ ์ ์ฅ๋๋ค. history list๋ฅผ ์ ๋ค๋ก ๊ฒ์ํ๋ฉด์ Unexecute์ Execute๋ฅผ ๋ถ๋ฆ์ผ๋ก์ ๋ฌด์ ํ์ undo๊ธฐ๋ฅ๊ณผ redo๊ธฐ๋ฅ์ ์ง์ํ ์ ์๊ฒ ๋๋ค.
- logging change๋ฅผ ์ง์ํ๊ธฐ ์ํ ๋. logging change ๋ฅผ ์ง์ํจ์ผ๋ก์ ์์คํ
์ถฉ๋์ด ๋ ๊ฒฝ์ฐ์ ๋ํด ํด๋น command๋ฅผ ์ฌ์๋ ํ ์ ์๋ค. Command ๊ฐ์ฒด์ load ์ store operation์ ์ถ๊ฐํจ์ผ๋ก์ change์ log๋ฅผ ์ ์งํ ์ ์๋ค. crash๋ก๋ถํฐ ๋ณต๊ตฌํ๋ ๊ฒ์ ๋์คํฌ๋ก๋ถํฐ logged command๋ฅผ ์ฝ์ด๋ค์ด๊ณ Execute operation์ ์ฌ์คํํ๋ ๊ฒ์ ์ค์ํ ๋ถ๋ถ์ด๋ค.
- ๊ธฐ๋ณธ๋ช
๋ น์ด๋ค๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ์ด์ฉํ ํ์ด๋ ๋ฒจ์ ๋ช
๋ น๋ค๋ก ์์คํ
์ ์กฐ์งํ ๋. ๊ทธ๋ฌํจ ์กฐ์ง์ transaction์ ์ง์ํ๋ ์ ๋ณด์์คํ
์์ ๋ณดํธํ๋ ๋ฐฉ์์ด๋ค. transaction์ ๋ฐ์ดํฐ์ ๋ณํ์ ์งํฉ์ ์บก์ํํ๋ค. CommandPattern์ transaction์ ๋์์ธํ๋ ํ๋์ ๋ฐฉ๋ฒ์ ์ ๊ณตํ๋ค. Command๋ค์ ๊ณตํต๋ ์ธํฐํ์ด์ค๋ฅผ ๊ฐ์ง๋ฉฐ, ๋ชจ๋ transaction๋ฅผ ๊ฐ์ ๋ฐฉ๋ฒ์ผ๋ก invokeํ ์ ์๋๋ก ํ๋ค. CommandPattern์ ๋ํ ์๋ก์ด transaction๋ค์ ์์คํ
์ ํ์ฅ์ํค๊ธฐ ์ฝ๊ฒ ํ๋ค.
1.6. Participants ¶
- Command
- ์ํํ operation์ ์ํ ์ธํฐํ์ด์ค๋ฅผ ์ ์ธํ๋ค.
- ConcreteCommand (PasteCommand, OpenCommand)
- Receiver ๊ฐ์ฒด์ action ๋ฌถ์์ ์ ์ํ๋ค.
- Client (Application)
- ConcreteCommand ๊ฐ์ฒด๋ฅผ ๋ง๋ค๊ณ receiver๋ก ์ ํ๋ค.
- Invoker (MenuItem)
- command ์๊ฒ request๋ฅผ ์ํํ๋๋ก ์์ฒญํ๋ค.
- Receiver (Document, Application)
- ์ฒ๋ฆฌํ request์ ๋ํด ๋ช ๋ น์ด๋ค์ ์ด๋ป๊ฒ ์ํํด์ผ ํ ์ง ์๊ณ ์๋ค. ์ด๋ ํ ํด๋์ค๋ ์ง Receiver๋ก์ ํ๋๊ฐ๋ฅํ๋ค.
1.7. Collaborations ¶
- client๋ ConcreteCommand ๊ฐ์ฒด๋ฅผ ๋ง๋ค๊ณ , receiver๋ฅผ ์ ํ๋ค.
- Invoker ๊ฐ์ฒด๋ ConcreteCommand๊ฐ์ฒด๋ฅผ ์ ์ฅํ๋ค.
- invoker๋ command์์ Execute๋ฅผ ํธ์ถํจ์ผ๋ก์ request๋ฅผ issueํ๋ค. ๋ช
๋ น์ด๊ฐ undo๊ฐ๋ฅํ ๋, ConcreteCommand๋ ๋ช
๋ น์ด๋ฅผ undoํ๊ธฐ ์ํ state๋ฅผ ์ ์ฅํ๋ค.
- ConcreteCommand ๊ฐ์ฒด๋ request๋ฅผ ์ฒ๋ฆฌํ๊ธฐ ์ํด receiver์์ operation์ invokeํ๋ค.
1.8. Consequences ¶
CommandPattern์ ๋ค์๊ณผ ๊ฐ์ ๊ฒฐ๊ณผ๋ฅผ ๊ฐ์ ธ์จ๋ค.
- Command๋ ํด๋น ๊ฐ์ฒด์ ๋ช
๋ น์ invokeํ๋ ๊ฐ์ฒด์ ์ด๋ป๊ฒ ์ํํด์ผ ํ ์ง ์๊ณ ์๋ ๊ฐ์ฒด์์ ๊ฒฐํฉ์ ํด์ ํ๋ค.
- Command๋ ์ฒซ๋ฒ์งธ๋จ๊ณ์ ํด๋์ค ๊ฐ์ฒด์ด๋ค. Command๋ ์กฐ์๋์ด์ง ์ ์์ผ๋ฉด์ ๋ค๋ฅธ ๊ฐ์ฒด๋ค๊ณผ ๋ง์ฐฌ๊ฐ์ง๋ก ํ์ฅ๊ฐ๋ฅํ๋ค.
- Command๋ฅผ ๋ฌถ์ ์ ์๋ค. ์์ ์ค๋ช
ํ MacroCommand๊ฐ ๊ทธ ์๊ฐ ๋๋ค. ์ผ๋ฐ์ ์ผ๋ก composite command๋ค์ CompositePattern์ ์ธ์คํด์ค์ด๋ค.
- ์๋ก์ด Command๋ฅผ ์ถ๊ฐํ๊ธฐ๊ฐ ์ฝ๋ค. ์๋ํ๋ฉด ์ด๋ฏธ ์กด์ฌํ๊ณ ์๋ ํด๋์ค๋ค์ ๊ณ ์น ํ์๊ฐ ์๊ธฐ ๋๋ฌธ์ด๋ค.
1.10. Sample Code ¶
์ฌ๊ธฐ ๋ณด์ฌ์ง๋ C++ code๋ Motivation ์น์
์ Command ํฌ๋์ค์ ๋ํ ๋๊ฐ์ ๊ตฌํ์ด๋ค. ์ฐ๋ฆฌ๋ OpenCommand, PasteCommand ์ MacroCommand๋ฅผ ์ ์ํ ๊ฒ์ด๋ค. ๋จผ์ ์ถ์ Commmand class ๋ ์ด๋ ๋ค.
~cpp class Command { public: virtual ~Command (); virtual void Execute () = 0; protected: Command (); };
OpenCommand๋ ์ ์ ๋ก๋ถํฐ ์ ๊ณต๋ ์ด๋ฆ์ ๋ฌธ์๋ฅผ ์ฐ๋ค. OpenCommand๋ ๋ฐ๋์ Constructor์ Application ๊ฐ์ฒด๋ฅผ ๋๊ฒจ๋ฐ์์ผ ํ๋ค. AskUser ๋ ์ ์ ์๊ฒ ์ด์ด์ผ ํ ๋ฌธ์์ ์ด๋ฆ์ ๋ฌป๋ ๋ฃจํด์ ๊ตฌํํ๋ค.
~cpp class OpenCommand : public Command { public: OpenCommand (Application*); virtual void Execute (); protected: virtual const char* AskUser (); private: Application* _application; char* _response; }; OpenCommand::OpenCommand (Application* a) { _application = a; } void OpenCommand::Execute () { const char* name = AskUser (); if (name != 0) { Document* document = new Document (name); _application->Add (document); document->Open (); } }
PasteCommand ๋ receiver๋ก์ Document๊ฐ์ฒด๋ฅผ ๋๊ฒจ๋ฐ์์ผ ํ๋ค. receiver๋ PasteCommand์ constructor์ parameter๋ก์ ๋ฐ๋๋ค.
~cpp class PasteCommand : public Command { public: PasteCommand (Document*); virtual void Execute (); private: Document* _document; }; PasteCommand::PasteCommand (Document* doc) { _document = doc; } void PasteCommand::Execute () { _document->Paste (); }
undo ํ ํ์๊ฐ ์๊ณ , ์ธ์๋ฅผ ์๊ตฌํ์ง ์๋ ๋จ์ํ ๋ช
๋ น์ด์ ๋ํด์ ์ฐ๋ฆฌ๋ command์ receiver๋ฅผ parameterizeํ๊ธฐ ์ํด class template๋ฅผ ์ฌ์ฉํ ์ ์๋ค. ์ฐ๋ฆฌ๋ ๊ทธ๋ฌํ ๋ช
๋ น๋ค์ ์ํด template subclass์ธ SimpleCommand๋ฅผ ์ ์ํ ๊ฒ์ด๋ค. SimpleCommand๋ Receiver type์ ์ํด parameterize ๋๊ณ
~cpp template <class Receiver> class SimpleCommand : public Command { public: typedef void (Receiver::* Action) (); SimpleCommand (Receiver* r, Action a) : _receiver (r), _action (a) { } vitual void Execute (); private: Action _action; Receiver* _receiver; };
constructor๋ receiver์ instance ๋ณ์์ ๋์๋๋ action์ ์ ์ฅํ๋ค. Execute๋ ๋จ์ํ action์ receiver์ ์ ์ฉํ๋ค.
~cpp template <class Receiver> void Simplecommand<Receiver>::Execute () { (_receiver->*_action) ();
MyClass์ instance๋ก ์๋ Action์ ํธ์ถํ command๋ฅผ ๋ง๋ค๊ธฐ ์ํด์, ํด๋ผ์ด์ธํธ๋ ๋จ์ํ ์ด๋ ๊ฒ ์ฝ๋ฉํ๋ค.
~cpp MyClass* receiver = new MyClass; // ... Command* aCommand = new SimpleCommand<MyClass> (receiver, &MyClass::Action); // ... aCommand->Execute ();
์ด ๋ฐฉ๋ฒ์ ๋จ์ง ๋จ์ํ ๋ช
๋ น์ด์๋ํ ํด๊ฒฐ์ฑ
์ผ ๋ฟ์์ ๋ช
์ฌํ๋ผ. track์ ์ ์งํ๊ฑฐ๋, receiver์ undo state๋ฅผ argument ๋ก ํ์๋ก ํ๋ ์ข๋ ๋ณต์กํ ๋ช
๋ น๋ค์ Command์ subclass๋ฅผ ์๊ตฌํ๋ค.
MacroCommand๋ ๋ถ๋ช
๋ น์ด๋ค์ sequence๋ฅผ ๊ด๋ฆฌํ๊ณ ๋ถ๋ช
๋ น์ด๋ค์ ์ถ๊ฐํ๊ฑฐ๋ ์ญ์ ํ๋ operation์ ์ ๊ณตํ๋ค. subcommand๋ค์ ์ด๋ฏธ ๊ทธ๋ค์ receiver๋ฅผ ์ ์ํ๋ฏ๋ก MacroCommand๋ ๋ช
์์ ์ธ receiver๋ฅผ ์๊ตฌํ์ง ์๋๋ค.
~cpp class MacroCommand : public Command { public: MacroCommand (); virtual ~MacroCommand (); virtual void Add (Command*); virtual void Remove (Command*); virtual void Execute (); private: List<Command*>* _cmds; };
MacroCommand์ ์ด์ ๋ Execute ๋งด๋ฒํจ์์ ์๋ค. ์ด๊ฒ์ ๋ชจ๋ ๋ถ๋ช
๋ น์ด๋ค์ ํ์ํ๋ฉด์ ๊ทธ๋ค ๊ฐ๊ฐ์ Execute operation๋ฅผ ์ํํ๋ค.
~cpp void MacroCommand::Execute () { ListIterator<Command*> i (_cmds); for (i.First (); !i.IsDone (); i.Next()) { Command* c = i.CurrentItem (); c->Execute (); } }
MacroCommand ๊ฐ Unexecute operation์ ๊ตฌํํ๊ธฐ ์ํด์ MacroCommand์ ๋ถ๋ช
๋ น์ด๋ค์ Execute operation์์ ๊ตฌํ๋ ์์์ ์ญ์์ผ๋ก ์ฐธ์กฐ๋๋ฉด์ unexecuteํด์ผ ํจ์ ์์งํ์.
์ต์ข
์ ์ผ๋ก MacroCommand ๋ ๋ถ๋ช
๋ น์ด๋ค์ ๊ด๋ฆฌํ operation์ ์ ๊ณตํด์ผํ๋ค. MacroCommand๋ ๋ํ MacroCommand๋ฅผ ์ด๋ฃจ๋ ๋ถ๋ช
๋ น์ด๋ค์ ์ญ์ ํ ์ฑ
์์ ์ง๋ค.
~cpp void MacroCommand::Add (Command* c) { _cmds->Append (c); } void MacroCommand::Remove (Command* c) { _cmds->Remove (c); }
1.11. Known Uses ¶
์๋ง๋ CommandPattern์ ๋ํ ์ฒซ๋ฒ์งธ ์์ ๋ Lieberman ์ ๋
ผ๋ฌธ(Lie85)์์ ๋ํ๋ฌ์ ๊ฒ์ด๋ค. MacApp App89 ๋ undo๊ฐ๋ฅํ ๋ช
๋ น์ ๊ตฌํ์ ์ํ command์ ํ๊ธฐ๋ฅผ ๋์คํ์์ผฐ๋ค. ET++WGM88, InterViews LCI+92, UnidrawVL90 ์ญ์ CommandPatter์ ๋ฐ๋ผ ํด๋์ค๋ค์ ์ ์ํ๋ค. InterViews๋ ๊ฐ ๊ธฐ๋ฅ๋ณ ๋ช
๋ น์ ๋ํ Action ์ถ์ ํด๋์ค๋ฅผ ์ ์ํ๋ค. ๊ทธ๋ฆฌ๊ณ action ๋ฉ์๋์ ์ํด ์ธ์ํ๋จ์ผ๋ก์ ์๋์ ์ผ๋ก command subclass๋ค์ ์ธ์คํด์คํ ์ํค๋ ActionCallback ํ
ํ๋ฆฟ๋ ์ ์ํ์๋ค.
THINK ํด๋์ค ๋ผ์ด๋ธ๋ฌ๋ฆฌ Sym93b ๋ํ undo ๊ฐ๋ฅํ ๋ช
๋ น์ ์ง์ํ๊ธฐ ์ํด CommandPattern์ ์ฌ์ฉํ๋ค. THINK ์์์ Command๋ค์ "Tasks" ๋ก ๋ถ๋ฆฐ๋ค. Task ๊ฐ์ฒด๋ค์ ChainOfResponsibilityPattern์ ์
๊ฐํ์ฌ ๋๊ฒจ์ง๊ณ ์๋น๋์ด์ง๋ค.
AddMe)