Skip to content

Instantly share code, notes, and snippets.

Created May 11, 2011 07:02
Show Gist options
  • Save anonymous/966026 to your computer and use it in GitHub Desktop.
Save anonymous/966026 to your computer and use it in GitHub Desktop.
StrategyパターンによるTemplate Methodパターンの書き換え
// ■Template Method Pattern
public abstract class Template {
public final void templateMethod() {
...
method1();
...
method2();
...
method3();
...
}
protected void method1();
protected void method2();
protected void method3();
}
public class Concrete1 extends Template {
@Override
protected void method1() {
...
}
@Override
protected void method2() {
...
}
@Override
protected void method3() {
...
}
}
// ■Strategy Pattern
public interface Strategy {
void method1();
void method2();
void method3();
}
public class Client {
public void templateMethod(final Strategy strategy) {
...
strategy.method1();
...
strategy.method2();
...
strategy.method3();
...
}
}
public class ConcreteStrategy implements Strategy {
public void method1() {
...
}
public void method2() {
...
}
public void method3() {
...
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment