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λ₯Ό λ§λ λ€. ---- ["ν¨ν΄λΆλ₯"]