『テンプレートモード』
10910 ワード
テンプレートモード
1.テンプレートパターンの紹介
2.コード実現
public abstract class SoyaBeanMilk {
public final void make(){
materialSelection();
if (supportAddSeasoning())
addSeasoning();
invasion();
grind();
}
protected void materialSelection(){
System.out.println(" ....");
}
// ,
protected abstract void addSeasoning();
protected void invasion(){
System.out.println(" ....");
}
protected void grind(){
System.out.println(" ....");
}
// , addSeasoning
//
protected boolean supportAddSeasoning(){
return true;
}
}
public class RedSoyaBeanMilk extends SoyaBeanMilk{
@Override
protected void addSeasoning() {
System.out.println(" ....");
}
}
public class PureSoyaBeanMilk extends SoyaBeanMilk{
@Override
protected void addSeasoning() {
//
}
@Override
protected boolean supportAddSeasoning() {
return false;
}
}
public class TemplateTest {
public static void main(String[] args) {
//
SoyaBeanMilk redSoyaBeanMilk = new RedSoyaBeanMilk();
redSoyaBeanMilk.make();
System.out.println("============================");
// ,
SoyaBeanMilk pureSoyaBeanMilk = new PureSoyaBeanMilk();
pureSoyaBeanMilk.make();
}
}
....
....
....
....
============================
....
....
....