抽象类体现的就是一种模板模式的设计,抽象类作为多个子类的通用模板,子类在抽象类的基础上进行扩展、改造,但子类总体上会保留抽象类的行为方式。
解决的问题:
当功能内部一部分实现是确定的,一部分实现是不确定的。这时可以把不确定的部分暴露出去,让子类去实现。
换句话说,在软件开发中实现一个算法时,整体步骤很固定、通用,这些步骤已经在父类中写好了。但是某些部分易变,易变部分可以抽象出来,供不同子类实现。这就是一种模板模式。
模板方法设计模式是编程中经常用得到的模式。各个框架、类库中都有他的影子,比如常见的有:
package com.klvchen.java; public class TemplateTest { public static void main(String[] args) { Template s1 = new SubTemplate(); s1.spendTime(); } } abstract class Template{ //计算某段代码执行所需花费的时间 public void spendTime() { long start = System.currentTimeMillis(); code(); //不确定的部分,易变的部分 long end = System.currentTimeMillis(); System.out.println("花费的时间为: " + (end - start)); } public abstract void code(); } class SubTemplate extends Template{ @Override public void code() { for(int i = 2; i<=1000; i++) { boolean isFlag = true; for(int j = 2; j <= Math.sqrt(i); j++) { if(i % j == 0) { isFlag = false; break; } } if(isFlag) { System.out.println(i); } } } }
TemplateMethodTest.java
package com.klvchen.java; public class TemplateMethodTest { public static void main(String[] args) { BankTemplateMethod btm = new DrawMoney(); btm.process(); System.out.println("************************"); BankTemplateMethod btm2 = new ManageMoney(); btm2.process(); } } abstract class BankTemplateMethod{ // 具体方法 public void takeNumer() { System.out.println("取号排队"); } public abstract void transact(); //办理具体的业务 //钩子方法 public void evaluate() { System.out.println("反馈评分"); } //模板方法,把基本操作组合到一起,子类一般不能重写 public final void process() { this.takeNumer(); this.transact(); // 像钩子,具体执行时,挂哪个子类,就执行哪个子类的实现代码 this.evaluate(); } } class DrawMoney extends BankTemplateMethod{ public void transact() { System.out.println("我要取款!"); } } class ManageMoney extends BankTemplateMethod{ @Override public void transact() { System.out.println("我要理财! 我这里有2000万美元"); } }
编写工资系统,实现不同类型员工(多态)的按月发放工资。如果当月出现某个Employee对象的生日,则将该雇员的工资增加100元。
实验说明:
(1) 定义一个Employee类,该类包含:
private 成员变量 name,number,birthday,其中 birthday 为 MyDate 类的对象;
abstract 方法 earnings();
toString()方法输出对象的 name,number和birthday。
(2) MyDate类包含:
private 成员变量 year,month,day ;
toDateString()方法返回日期对应的字符串:xxxx年xx月xx日
(3) 定义 SalariedEmployee 类继承 Employee 类,实现按月计算工资的员工处理。该类包括:private 成员变量monthlySalary;
实现父类的抽象方法 earnings(),该方法返回 monthlySalary 值; toString()方法输出员工类型信息及员工的name,number,birthday。
(4) 参照 SalariedEmbloyee 类定义 HourlyEmployee 类,实现按小时计算工资的员工处理。该类包括:
private成员变量 wage 和 hour;
实现父类的抽象方法 earnings(),该方法返回 wage*hour 值;
toString()方法输出员工类型信息及员工的 name,number,birthday。
(5) 定义 PayrollSystem 类,创建 Employee 变量数组并初始化,该数组存放各类雇员对象的引用。利用循环结构遍历数组元素,输出各个对象的类型,name,number,birthday,以及该对象生日。当键盘输入本月月份值时,如果本月是某个Employee对象的生日,还要输出增加工资信息。
MyDate.java
package com.klvchen.exer2; public class MyDate { private int year; private int month; private int day; public MyDate(int year, int month, int day) { super(); this.year = year; this.month = month; this.day = day; } public int getYear() { return year; } public void setYear(int year) { this.year = year; } public int getMonth() { return month; } public void setMonth(int month) { this.month = month; } public int getDay() { return day; } public void setDay(int day) { this.day = day; } public String toDateString() { return year + "年" + month + "月" + day + "日"; } }
Employee.java
package com.klvchen.exer2; public abstract class Employee { private String name; private int number; private MyDate birthday; public Employee(String name, int number, MyDate birthday) { super(); this.name = name; this.number = number; this.birthday = birthday; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getNumber() { return number; } public void setNumber(int number) { this.number = number; } public MyDate getBirthday() { return birthday; } public void setBirthday(MyDate birthday) { this.birthday = birthday; } public abstract double earnings(); @Override public String toString() { return "name=" + name + ", number=" + number + ", birthday=" + birthday.toDateString() + "]"; } }
SalariedEmployee.java
package com.klvchen.exer2; public class SalariedEmployee extends Employee { private double monthlySalary; //月工资 public SalariedEmployee(String name, int number, MyDate birthday) { super(name, number, birthday); } public SalariedEmployee(String name, int number, MyDate birthday, double monthlySalary) { super(name, number, birthday); this.monthlySalary = monthlySalary; } public double getMonthlySalary() { return monthlySalary; } public void setMonthlySalary(double monthlySalary) { this.monthlySalary = monthlySalary; } @Override public double earnings() { return monthlySalary; } public String toString() { return "SalariedEmployee[" + super.toString() + "]"; } }
HourlyEmployee.java
package com.klvchen.exer2; public class HourlyEmployee extends Employee { private int wage; //每小时的工资 private int hour; //月工资的小时数 public HourlyEmployee(String name, int number, MyDate birthday) { super(name, number, birthday); } public HourlyEmployee(String name, int number, MyDate birthday, int wage, int hour) { super(name, number, birthday); this.wage = wage; this.hour = hour; } public int getWage() { return wage; } public void setWage(int wage) { this.wage = wage; } public int getHour() { return hour; } public void setHour(int hour) { this.hour = hour; } @Override public double earnings() { return wage * hour; } public String toString() { return "HourlyEmployee [" + super.toString() + "]"; } }
PayrollSystem.java
package com.klvchen.exer2; import java.util.Calendar; import java.util.Scanner; public class PayrollSystem { public static void main(String[] args) { //方式一: // Scanner scanner = new Scanner(System.in); // System.out.println("请输入当月的月份:"); // int month = scanner.nextInt(); //方式二 Calendar calendar = Calendar.getInstance(); int month = calendar.get(Calendar.MONTH); //获取当前的月份 Employee[] emps = new Employee[2]; emps[0] = new SalariedEmployee("马森", 1002, new MyDate(1992, 2, 28), 10000); emps[1] = new HourlyEmployee("潘先生", 2001, new MyDate(1991, 3, 15), 60, 240); for(int i = 0; i < emps.length; i++) { System.out.println(emps[i]); double salary = emps[i].earnings(); System.out.println("月工资为: " + salary); if(month+1 == emps[i].getBirthday().getMonth()) { System.out.println("生日快乐! 奖励100元"); } System.out.println("---------------------------"); } } }