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