1.1. Intent ¶
๋น์ทํ ๋ฌธ์ ๋ค์ ํด๊ฒฐํ ์ ์๋ ์๊ณ ๋ฆฌ์ฆ์ ๊ตฐ๋ค์ ์ ์ํ๊ณ , ๊ฐ๊ฐ์ ์๊ณ ๋ฆฌ์ฆ์ ์บก์ํํ๊ณ , ๊ทธ ์๊ณ ๋ฆฌ์ฆ๋ค์ ๊ตํํ ์ ์๋๋ก ํ๋ค. Strategy๋ ์๊ณ ๋ฆฌ์ฆ๋ค๋ก ํ์ฌ๊ธ ํด๋น ์๊ณ ๋ฆฌ์ฆ์ ์ด์ฉํ๋ ํด๋ผ์ด์ธํธ๋ก๋ถํฐ ๋
๋ฆฝ์ ์ผ์ ์๋๋ก ํด์ค๋ค.
1.3. Motivation ¶
ํ
์คํธ ์คํธ๋ฆผ์ ์ค ๋จ์๋ก ๋๋๋ ๋ง์ ์๊ณ ๋ฆฌ์ฆ๋ค์ด ์๋ค. (์ดํ linebreaking algorithm). ํด๋น ์๊ณ ๋ฆฌ์ฆ๋ค์ด ๊ทธ๊ฒ์ ํ์๋ก ํ๋ ํด๋์ค์ ๊ธด๋ฐํ๊ฒ ์ฐ๊ฒฐ๋์ด์๋ ๊ฒ์ ์ฌ๋ฌ๊ฐ์ง ์ด์ ๋ฉด์์ ๋ฐ๋์งํ์ง ๋ชปํ๋ค.
- linebreaking์ด ํ์ํ ํด๋ผ์ด์ธํธ์ด ๊ทธ ์๊ณ ๋ฆฌ์ฆ์ ์ง์ ํฌํจํ๊ณ ์๋ ๊ฒฝ์ฐ์๋ ํด๋ผ์ด์ธํธ๋ค์ด ๋ ๋ณต์กํด์ง ์ ์๋ค. ์ด๋ ํด๋ผ์ด์ธํธ๋ค์ ๋ ์ปค์ง๊ฑฐ๋ ์ ์ง๊ฐํ ํ๋ค๊ฒ ํ๋ค. ํนํ ํด๋ผ์ด์ธํธ๊ฐ ์ฌ๋ฌ ์๊ณ ๋ฆฌ์ฆ์ ์ ๊ณตํด์ผ ํ๋ ๊ฒฝ์ฐ์๋ ๋๋์ฑ ๊ทธ๋ ๋ค.
- ๊ฐ๊ฐ์ ์๊ณ ๋ฆฌ์ฆ๋ค์ ์ํฉ์ ๋ฐ๋ผ ์ ์ ํ ๋๊ฐ ์กด์ฌํ๋ค. ํด๋ผ์ด์ธํธ๋ ์ฌ์ฉํ ์ง ์ํ ์ง ๋ชจ๋ฅด๋ ์๊ณ ๋ฆฌ์ฆ๋ค๊น์ง ์ ๋ถ ํ์ํ์ง ์๋ค.
- linebreaking์ด ํด๋ผ์ด์ธํธ์ฝ๋์ ์ผ๋ถ์ธ ๊ฒฝ์ฐ, ์ ์๊ณ ๋ฆฌ์ฆ์ ์ถ๊ฐํ๊ฑฐ๋, ๊ธฐ์กด ์ฝ๋๋ฅผ ํ์ฅํ๊ธฐ ์ด๋ ต๋ค.
Composition ํด๋์ค๋ text viewer์ ํ์๋ ํ
์คํ ์ ์งํ๊ณ ๊ฐฑ์ ํ ์ฑ
์์ ๊ฐ์ง๋ค๊ณ ๊ฐ์ ํ์. Linebreaking strategy๋ค์ Composition ํด๋์ค์ ๊ตฌํ๋์ง ์๋๋ค. ๋์ , ๊ฐ๊ฐ์ Linebreaking strategy๋ค์ Compositor ์ถ์ํด๋์ค์ subclass๋ก์ ๋ฐ๋ก ๊ตฌํ๋๋ค. Compositor subclass๋ค์ ๋ค๋ฅธ streategy๋ค์ ๊ตฌํํ๋ค.
- SimpleCompositor - ์ผ๋ฐ์ ์ธ ์ค ๋๋๊ธฐ ์๊ณ ๋ฆฌ์ฆ์ ๊ตฌํํ๋ค.
- TexCompositor - linebreaking ์ ๋ํด TeX ์๊ณ ๋ฆฌ์ฆ์ ์ ์ฉ, ๊ตฌํํ๋ค. ์ด ๋ฐฉ๋ฒ์ ํ๋ฒ์ ๋ฌธ๋จ ์ ์ฒด์ ๋ํด์ ์ ๋ฐ์ ์ผ๋ก linebreak๋ฅผ ์ต์ ํํ๋ ค๊ณ ํ๋ค.
- ArrayCompositor - ๊ฐ๊ฐ์ ์ค์ ํน์ ์์ ๋จ์ด๊ฐ ๋ค์ด๊ฐ๋๋ก ์ค๋๋์ ํ๋ ์๊ณ ๋ฆฌ์ฆ์ ๊ฐ์ง ํด๋์ค.
1.4. Applicability ¶
StrategyPattern ์ ๋ค์๊ณผ ๊ฐ์ ๊ฒฝ์ฐ์ ์ด์ฉํ ์ ์๋ค.
- ๋ง์ ๊ด๋ จ ํด๋์ค๋ค์ด ์ค์ง ๊ทธ๋ค์ ํ๋๋ค์ ์ํด ๊ตฌ๋ถ๋๋ค. Strategy๋ค์ ๋ง์ ํ์์ค์ ํ๊ฐ์ง๋ก ์ํฉ์ ๋ฐ๋ผ ํด๋์ค์ ์ค์ ํด์ฃผ๋ ๋ฐฉ๋ฒ์ ์ ๊ณตํด์ค๋ค.
- ๋น์ ์ ์๊ณ ๋ฆฌ์ฆ์ ๋ค์ํจ์ ํ์๋ก ํ๋ค. ์๋ฅผ ๋ค์ด, ๋น์ ์ด ์๊ณ ๋ฆฌ์ฆ์ ์ ์ํ๋ ๊ฒ์ ์ฌ์ฉ๋ฉ๋ชจ๋ฆฌ/์ํ์๊ฐ์ ๋ํ trade-off (๋ฉ๋ชจ๋ฆฌ๋ฅผ ์๋ผ๊ธฐ ์ํด ์ํ์๊ฐ์ ํฌ์ํด์ผ ํ๊ฑฐ๋, ์ํ์๊ฐ์ ์ํด ๋ฉ๋ชจ๋ฆฌ๊ณต๊ฐ์ ๋ ์ฌ์ฉํ๋ ๊ฒ ๋ฑ์ ์๊ด๊ด๊ณ)์ด๋ค. Strategy ๋ ์ด๋ฌํ ๋ค์ํ ์๊ณ ๋ฆฌ์ฆ์ ๊ณ์ธต ํด๋์ค๋ฅผ ๊ตฌํํ ๋ ์ด์ฉ๋ ์ ์๋ค.
- StrategyPattern์ ์ด์ฉํจ์ผ๋ก์จ ๋ณต์กํจ์ด ๋
ธ์ถ๋๋ ๊ฒ๊ณผ ์๊ณ ๋ฆฌ์ฆ ๊ตฌ์ฒด์ ์ธ ๋ฐ์ดํฐ ๊ตฌ์กฐ๋ก ๊ฐ๋ ๊ฒ์ ํผํ ์ ์๋ค.
- ํด๋์ค๊ฐ ๋ง์ ํ์๋ค์ ์ ์ํ๋ค. ์ด๋ ๋ค์ค์กฐ๊ฑด๋ฌธ๋ค์ ์ํด์ ๊ตฌํ๋๊ณค ํ๋ค. ์ด๋ฌํ ๋ง์ ์กฐ๊ฑด๋ฌธ๋ค ๋์ , ๊ฐ๊ฐ ๊ด๋ จ๋ ์กฐ๊ฑด๋ค์ Strategy ํด๋์ค๋ค์๊ฒ๋ก ์ด๋์ํฌ ์ ์๋ค.
1.6. Participants ¶
- Strategy (Compositor)
- ๋ชจ๋ ์ ๊ณต๋ ์๊ณ ๋ฆฌ์ฆ์ ๋ํ ์ผ๋ฐ์ ์ธ ์ธํฐํ์ด์ค๋ฅผ ์ ์ธํ๋ค. Context๋ ConcreteStrategy์ ์ํด ๊ตฌํ๋ ์๊ณ ๋ฆฌ์ฆ๋ค์ ํธ์ถํ๊ธฐ ์ํด ์ด ์ธํฐํ์ด์ค๋ฅผ ์ด์ฉํ๋ค.
- ๋ชจ๋ ์ ๊ณต๋ ์๊ณ ๋ฆฌ์ฆ์ ๋ํ ์ผ๋ฐ์ ์ธ ์ธํฐํ์ด์ค๋ฅผ ์ ์ธํ๋ค. Context๋ ConcreteStrategy์ ์ํด ๊ตฌํ๋ ์๊ณ ๋ฆฌ์ฆ๋ค์ ํธ์ถํ๊ธฐ ์ํด ์ด ์ธํฐํ์ด์ค๋ฅผ ์ด์ฉํ๋ค.
- ConcreteStrategy (SimpleCompositor, TeXCompositor, ArrayCompositor)
- Strategy ์ธํฐํ์ด์ค๋ฅผ ์ด์ฉํ์ฌ ์๊ณ ๋ฆฌ์ฆ์ ๊ตฌํํ๋ค.
- Strategy ์ธํฐํ์ด์ค๋ฅผ ์ด์ฉํ์ฌ ์๊ณ ๋ฆฌ์ฆ์ ๊ตฌํํ๋ค.
- Context (Composition)
- ConcreteStrategy ๊ฐ์ฒด๋ก ์ค์ ๋์ด์ง๋ค.
- Strategy ๊ฐ์ฒด์ ์ฐธ์กฐ๋ฅผ ๊ฐ์ง๋ค.
- Strategy ๊ฐ context์ ๋ฐ์ดํฐ๋ฅผ ์ ๊ทผํ ์ ์๋๋ก ์ธํฐํ์ด์ค๋ฅผ ์ ์ํ ์ ์๋ค.
- ConcreteStrategy ๊ฐ์ฒด๋ก ์ค์ ๋์ด์ง๋ค.
1.7. Consequences ¶
StrategyPattern ์ ๋ค์๊ณผ ๊ฐ์ ์ฅ์ ๊ณผ ๋จ์ ์ ๊ฐ์ง๋ค.
- ๊ด๋ จ๋ ์๊ณ ๋ฆฌ์ฆ์ ๋ฌถ์
- subclassing ์ ๋์
- ์กฐ๊ฑด๋ฌธ์ ์ ๊ฑฐํ๊ธฐ ์ํ Strategy
- ํด๋น ๊ตฌํ๋ถ์ ์ ํ
- ํด๋ผ์ด์ธํธ๋ ๋ฐ๋์ ๊ฐ๊ฐ์ Strategy๋ค์ ๋ํด ์์์ผ ํ๋ค.
- Strategy ์ Context ์ฌ์ด์ ๋ํ์ค overhead ๊ฐ ๋ฐ์ํ๋ค.
- ๊ฐ์ฒด์ ์๊ฐ ์ฆ๊ฐํ๋ค.
1.8. Sample Code ¶
~cpp class Composition { public: Composition (Compositor *); void Repair (); private: Compositor* _compositor; Component* _components; int _componentCount; int _lineWidth; int* _lineBreaks; int _lineCount; };
~cpp class Compositor { public: virtual int Compose ( Coord natural[], Coord stretch[], Coord shrink[], int componentCount, int lineWidth, int breaks[] ) = 0; protected: Compositor (); };
~cpp void Composition::Repair () { Coord* natural; Coord* stretchability; Coord* shrinkability; int componentCount; int* breaks; // prepare the arrays with the desired component sizes // ... // determine where the breaks are: int breakCount; breakCount = _compositor->Compose ( natural, stretchability, shrinkability, componentCount, _lineWidth, breaks ); // lay out components according to breaks // ... }
~cpp class SimpleCompositor : public Compositor { public: SimpleCompositor (); virtual int Compose ( Coord natural[], Coord stretch[], Coord shrink[], int componentCount, int lineWidth, int breaks[] ); // ... };
~cpp class TeXCompositor : public Compositor { public: TeXCompositor (); virtual int Compose ( Coord natural[], Coord stretch[], Coord shrink[], int componmentCount, int lineWidth, int breaks[] ); // ... };
{{{~cpp class ArrayCompositor : public Compositor { public: ArrayCompositor (int interval); virtual int Compose ( Coord natural[], Coord stretch[], Coord shrink[], int componentCount, int lineWidth, int breaks[] ); // ... }; {{{~cpp Composition* quick = new Composition (new SimpleCompositor); Composition* slick = new Composition (new TeXCompositor); Composition* iconic = new Composition (new ArrayCompositior (100)); }}} == Known Uses == * ET++, InterViews - line breaking algorithms as we've described. * RTL System for compiler code optimization - Register allocation, Instruction set Scheduling. * ET++ SwapsManager cacluation engine framework. * The Booch components - template arguments. - memory allocation strategies. * RApp (system for integrated circult layout) - Router Algorithms. * Borland's ObjectWindows - dialog box. validation streategies. == Releated Patterns == FlyWeightPattern : Strategy ๊ฐ์ฒด๋ ์ข ์ข ํ๋ฅญํ flyweight๋ฅผ ๋ง๋ ๋ค. ---- ["ํจํด๋ถ๋ฅ"]