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