[Design Pattern] 스트래티지(Strategy) 패턴

스트래티지(Strategy) 패턴

- 서로 밀접한 관계를 가지는 여러 클래스에 대해 필요한 시점에 수행하는 클래스를 골라 사용하고자 할 때 사용하는 패턴. 


- abstract class 를 implement 시켜서 각각의 type 의 클래스를 생성한다. 

- 그리고 하나의 클래스에서 각각의 클래스들을 관리하여준다. 

   새롭게 생성되는 type 가 있을 때 쓰면 좋겠다. 라고 생각된다. 


더이상 설명은 없고 각각의 클래스에 대한 소스코드를 첨부한다. 





Abs_Contents.java


public interface AbsContents {

	void Draw(); // on Android

	void Rendering(); // on OpenGL
}


Content_Card.java


public class Content_Card implements AbsContents {

	@Override
	public void Draw() {
		System.out.println("Content Card's Draw()");
	}

	@Override
	public void Rendering() {
		System.out.println("Content Card's Rendering()");
	}
}


Content_Image.java

public class Content_Image implements AbsContents {

	@Override
	public void Draw() {
		System.out.println("Content Image's Draw()");
	}

	@Override
	public void Rendering() {
		System.out.println("Content Image's Rendering()");
	}
}

ContentsMgr.java

public class ContentsMgr {
	private AbsContents m_Contents;

	ContentsMgr() {
		m_Contents = new Content_Card();
	}

	// setter - Content's type
	void SetContent(AbsContents _contents) {
		m_Contents = _contents;
	}

	void Draw() {
		m_Contents.Draw();
	}

	void Render() {
		m_Contents.Rendering();
	}

}

main.java

public class main {

	public static void main(String[] args) {

		ContentsMgr CtMgr = new ContentsMgr();

		CtMgr.Draw();
		CtMgr.SetContent(new Content_Image());
		CtMgr.Draw();

	}

}





strategy.zip


위와 같이 하면 된다. 




댓글

Designed by JB FACTORY