標籤:
abstract class PhoneModel6Inch
{
public void printPhoneInfo()
{
screenSize();
systemType();
colorType();
}
protected final void screenSize()
{
System.out.print("6 inch,");
}
abstract protected void systemType();
abstract void colorType();
}
class Phone1 extends PhoneModel6Inch
{
public void systemType()
{
System.out.print("2G,");
}
public void colorType()
{
System.out.println("Black");
}
}
class Phone2 extends PhoneModel6Inch
{
public void systemType()
{
System.out.print("3G,");
}
public void colorType()
{
System.out.println("Red");
}
}
public class TemplateMethodPattern
{
public static void main(String[] args)
{
System.out.print("number 1 phone is: ");
Phone1 aPhone1=new Phone1();
aPhone1.printPhoneInfo();
System.out.print("number 2 phone is: ");
Phone2 aPhone2=new Phone2();
aPhone2.printPhoneInfo();
}
}
/////////////////////////////////////////////////
輸出結果:
number 1 phone is: 6 inch,2G,Black
number 2 phone is: 6 inch,3G,Red
java中23種設計模式之8-模板方法模式(template method pattern)