U E D R , A S I H C RSS

Gof/Strategy


1. Strategy

1.1. Intent

๋น„Šท•œ ๋ฌธ œ๋“ค„ •ด๊ฒฐ•  ˆ˜ žˆ๋Š” •Œ๊ณ ๋ฆฌฆ˜˜ ๊ตฐ๋“ค„  •˜•˜๊ณ , ๊ฐ๊ฐ˜ •Œ๊ณ ๋ฆฌฆ˜„ บกА™”•˜๊ณ , ๊ทธ •Œ๊ณ ๋ฆฌฆ˜๋“ค„ ๊ต™˜•  ˆ˜ žˆ๋„๋ก •œ๋‹ค. Strategy๋Š” •Œ๊ณ ๋ฆฌฆ˜๋“ค๋กœ •˜—ฌ๊ธˆ •ด๋‹น •Œ๊ณ ๋ฆฌฆ˜„ ดšฉ•˜๋Š” ด๋ผด–ธŠธ๋กœ๋ถ€„ฐ ๋…๋ฆฝ ผˆ˜ žˆ๋„๋ก •ดค€๋‹ค.

1.2. Also Known As

Policy

1.3. Motivation

…ŠคŠธ ŠคŠธ๋ฆผ„ „ ๋‹จœ„๋กœ ๋‚˜๋ˆ„๋Š” ๋งŽ€ •Œ๊ณ ๋ฆฌฆ˜๋“คด žˆ๋‹ค. (ด•˜ linebreaking algorithm). •ด๋‹น •Œ๊ณ ๋ฆฌฆ˜๋“คด ๊ทธ๊ฒƒ„ •„š”๋กœ •˜๋Š” ด๋ž˜Šค— ๊ธด๋ฐ€•˜๊ฒŒ —ฐ๊ฒฐ๋˜–ดžˆ๋Š” ๊ฒƒ€ —ฌ๋Ÿฌ๊ฐ€ง€ ดœ  ๋ฉด—„œ ๋ฐ”๋žŒง•˜ง€ ๋ชป•˜๋‹ค.
  • linebreakingด •„š”•œ ด๋ผด–ธŠธด ๊ทธ •Œ๊ณ ๋ฆฌฆ˜„ ง ‘ ฌ•จ•˜๊ณ  žˆ๋Š” ๊ฒฝšฐ—๋Š” ด๋ผด–ธŠธ๋“คด ๋” ๋ณตžก•ดˆ ˆ˜ žˆ๋‹ค. ด๋Š” ด๋ผด–ธŠธ๋“ค„ ๋” ปคง€๊ฐ๋‚˜ œ ง€๊ฐ€žˆ ž˜๋“ค๊ฒŒ •œ๋‹ค. Šนžˆ ด๋ผด–ธŠธ๊ฐ€ —ฌ๋Ÿฌ •Œ๊ณ ๋ฆฌฆ˜„  œ๊ณต•ด•ผ •˜๋Š” ๊ฒฝšฐ—๋Š” ๋”๋”š ๊ทธ๋ ‡๋‹ค.
  • ๊ฐ๊ฐ˜ •Œ๊ณ ๋ฆฌฆ˜๋“ค€ ƒ™ฉ— ๋”ฐ๋ผ   ˆ•  ๋•Œ๊ฐ€ กดžฌ•œ๋‹ค. ด๋ผด–ธŠธ๋Š” ‚ฌšฉ• ง€ •ˆ• ง€ ๋ชจ๋ฅด๋Š” •Œ๊ณ ๋ฆฌฆ˜๋“ค๊นŒง€  „๋ถ€ •„š”•˜ง€ •Š๋‹ค.
  • linebreakingด ด๋ผด–ธŠธฝ”๋“œ˜ ผ๋ถ€ธ ๊ฒฝšฐ, ƒˆ •Œ๊ณ ๋ฆฌฆ˜„ ถ”๊ฐ€•˜๊ฐ๋‚˜, ๊ธฐกด ฝ”๋“œ๋ฅผ ™•žฅ•˜๊ธฐ –ด๋ ต๋‹ค.

ด๋Ÿฌ•œ ๋ฌธ œ๋Š”, ๊ฐ๊ฐ˜ ๋‹ค๋ฅธ linebreaking„ บกА™”•œ ด๋ž˜Šค๋ฅผ  •˜•จœผ๋กœ ”ผ•  ˆ˜ žˆ๋‹ค. ด๋Ÿฌ•œ ๋ฐฉ๋ฒ•œผ๋กœ บกА™”•œ •Œ๊ณ ๋ฆฌฆ˜„ stretegy ๋ผ ๋ถ€๋ฅธ๋‹ค.


Composition ด๋ž˜Šค๋Š” text viewer— ‘œ‹œ๋  …Šค‹€ œ ง€•˜๊ณ  ๊ฐ‹ •  …ž„„ ๊ฐ€„๋‹ค๊ณ  ๊ฐ€ ••˜ž. Linebreaking strategy๋“ค€ Composition ด๋ž˜Šค— ๊ตฌ˜„๋˜ง€ •Š๋Š”๋‹ค. ๋Œ€‹ , ๊ฐ๊ฐ˜ Linebreaking strategy๋“ค€ Compositor ถ”ƒด๋ž˜Šค˜ subclass๋กœ„œ ๋”ฐ๋กœ ๊ตฌ˜„๋œ๋‹ค. Compositor subclass๋“ค€ ๋‹ค๋ฅธ streategy๋“ค„ ๊ตฌ˜„•œ๋‹ค.

  • SimpleCompositor - ผ๋ฐ˜ ธ „ ๋‚˜๋ˆ„๊ธฐ •Œ๊ณ ๋ฆฌฆ˜„ ๊ตฌ˜„•œ๋‹ค.
  • TexCompositor - linebreaking — ๋Œ€•ด TeX •Œ๊ณ ๋ฆฌฆ˜„  šฉ, ๊ตฌ˜„•œ๋‹ค. ด ๋ฐฉ๋ฒ•€ •œ๋ฒˆ— ๋ฌธ๋‹จ  „ฒด— ๋Œ€•ด„œ  „๋ฐ˜ œผ๋กœ linebreak๋ฅผ ตœ ™”•˜๋ ค๊ณ  •œ๋‹ค.
  • ArrayCompositor - ๊ฐ๊ฐ˜ „— Šน • ˆ˜˜ ๋‹จ–ด๊ฐ€ ๋“ค–ด๊ฐ€๋„๋ก „๋‚˜๋ˆ”„ •˜๋Š” •Œ๊ณ ๋ฆฌฆ˜„ ๊ฐ€„ ด๋ž˜Šค.
Composition € Compositor ๊ฐฒด˜ ฐธกฐ๋ฅผ ๊ฐ€„๋‹ค. Composition ด …ŠคŠธ๋ฅผ ๋‹ค‹œ ˜•‹™”• ๋•Œ, Composition € Compositor —๊ฒŒ •ด๋‹น …ž„„„˜๊ธด๋‹ค. Composition˜ ด๋ผด–ธŠธ๋Š” ‚ฌšฉ•˜๋ ค๋Š” Compositor˜ ฐธกฐ๋ฅผ ๋ฐ›Œœผ๋กœจ ‚ฌšฉ•ด•ผ •  Compositor„ ๊ตฌฒด™”‹œ‚จ๋‹ค.

1.4. Applicability

StrategyPattern € ๋‹คŒ๊ณผ ๊ฐ™€ ๊ฒฝšฐ— ดšฉ•  ˆ˜ žˆ๋‹ค.
  • ๋งŽ€ ๊ด€๋ จ ด๋ž˜Šค๋“คด ˜คง ๊ทธ๋“ค˜ –‰๋™๋“ค— ˜•ด ๊ตฌ๋ถ„๋œ๋‹ค. Strategy๋“ค€ ๋งŽ€ –‰œ„ค‘— •œ๊ฐ€ง€๋กœ ƒ™ฉ— ๋”ฐ๋ผ ด๋ž˜Šค„ „ ••ดฃผ๋Š” ๋ฐฉ๋ฒ•„  œ๊ณต•ดค€๋‹ค.
  • ๋‹น‹ € •Œ๊ณ ๋ฆฌฆ˜˜ ๋‹ค–‘•จ„ •„š”๋กœ •œ๋‹ค. ˜ˆ๋ฅผ ๋“ค–ด, ๋‹น‹ ด •Œ๊ณ ๋ฆฌฆ˜„  •˜•˜๋Š” ๊ฒƒ€ ‚ฌšฉ๋ฉ”๋ชจ๋ฆฌ/ˆ˜–‰‹œ๊ฐ„— ๋Œ€•œ trade-off (๋ฉ”๋ชจ๋ฆฌ๋ฅผ •„๋ผ๊ธฐ œ„•ด ˆ˜–‰‹œ๊ฐ„„ ฌƒ•ด•ผ •˜๊ฐ๋‚˜, ˆ˜–‰‹œ๊ฐ„„ œ„•ด ๋ฉ”๋ชจ๋ฆฌ๊ณต๊ฐ„„ ๋” ‚ฌšฉ•˜๋Š” ๊ฒƒ ๋“˜ ƒ๊ด€๊ด€๊ณ„)ด๋‹ค. Strategy ๋Š” ด๋Ÿฌ•œ ๋‹ค–‘•œ •Œ๊ณ ๋ฆฌฆ˜˜ ๊ณ„ธต ด๋ž˜Šค๋ฅผ ๊ตฌ˜„• ๋•Œ ดšฉ๋  ˆ˜ žˆ๋‹ค.
  • StrategyPattern„ ดšฉ•จœผ๋กœจ ๋ณตžก•จด ๋…ธถœ๋˜๋Š” ๊ฒƒ๊ณผ •Œ๊ณ ๋ฆฌฆ˜ ๊ตฌฒด ธ ๋ฐด„ฐ ๊ตฌกฐ๋กœ ๊ฐ€๋Š” ๊ฒƒ„ ”ผ•  ˆ˜ žˆ๋‹ค.
  • ด๋ž˜Šค๊ฐ€ ๋งŽ€ –‰œ„๋“ค„  •˜•œ๋‹ค. ด๋Š” ๋‹คค‘กฐ๊ด๋ฌธ๋“ค— ˜•ด„œ ๊ตฌ˜„๋˜๊ณค •œ๋‹ค. ด๋Ÿฌ•œ ๋งŽ€ กฐ๊ด๋ฌธ๋“ค ๋Œ€‹ , ๊ฐ๊ฐ ๊ด€๋ จ๋œ กฐ๊ด๋“ค„ Strategy ด๋ž˜Šค๋“ค—๊ฒŒ๋กœ ด๋™‹œ‚ฌ ˆ˜ žˆ๋‹ค.

1.6. Participants

  • Strategy (Compositor)
    • ๋ชจ๋“   œ๊ณต๋œ •Œ๊ณ ๋ฆฌฆ˜— ๋Œ€•œ ผ๋ฐ˜ ธ ธ„Ž˜ดŠค๋ฅผ „ –ธ•œ๋‹ค. Context๋Š” ConcreteStrategy— ˜•ด ๊ตฌ˜„๋œ •Œ๊ณ ๋ฆฌฆ˜๋“ค„ ˜ธถœ•˜๊ธฐ œ„•ด ด ธ„Ž˜ดŠค๋ฅผ ดšฉ•œ๋‹ค.
  • ConcreteStrategy (SimpleCompositor, TeXCompositor, ArrayCompositor)
    • Strategy ธ„Ž˜ดŠค๋ฅผ ดšฉ•˜—ฌ •Œ๊ณ ๋ฆฌฆ˜„ ๊ตฌ˜„•œ๋‹ค.
  • Context (Composition)
    • ConcreteStrategy ๊ฐฒด๋กœ „ •๋˜–ด„๋‹ค.
    • Strategy ๊ฐฒด˜ ฐธกฐ๋ฅผ ๊ฐ€„๋‹ค.
    • Strategy ๊ฐ€ context˜ ๋ฐด„ฐ๋ฅผ  ‘๊ทผ•  ˆ˜ žˆ๋„๋ก ธ„Ž˜ดŠค๋ฅผ  •˜•  ˆ˜ žˆ๋‹ค.

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๋ฅผ ๋งŒ๋“ ๋‹ค.

----
["Œจ„ด๋ถ„๋ฅ˜"]
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:23:19
Processing time 0.1584 sec