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.0079 sec