码迷,mamicode.com
首页 > 编程语言 > 详细

java中23种设计模式之8-模板方法模式(template method pattern)

时间:2015-03-30 20:39:29      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:

 

技术分享

 

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)

标签:

原文地址:http://www.cnblogs.com/wudymand/p/4379001.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!