抽象类体现的就是一种模板模式,抽象类作为多个子类的通用模板,子类在抽象类的基础上进行扩展、改造,但子类总体上会保留抽象类的行为方式。
解决的问题:
案例1:计算代码运行时间
public class TemplateTest {
public static void main(String[] args) {
PrintStar ps = new PrintStar();
ps.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 PrintStar extends Template {
@Override
public void code() {
System.out.println("*");
System.out.println("**");
System.out.println("***");
System.out.println("****");
System.out.println("*****");
}
}
案例2:银行流程抽象
public class TemplateMethodTest {
public static void main(String[] args) {
BankTemplateMethod btm = new DramMoney();
btm.process();
BankTemplateMethod btm2 = new ManageMoney();
btm.process();
}
}
abstract class BankTemplateMethod {
public void takeNumber() {
System.out.println("排队取号");
}
//办理具体的业务
public abstract void transact();
public void evaluate() {
System.out.println("反馈评分");
}
//模板方法
public final void process() {
takeNumber();
transact();
evaluate();
}
}
class DramMoney extends BankTemplateMethod {
@Override
public void transact() {
System.out.println("我要取款!!!");
}
}
class ManageMoney extends BankTemplateMethod {
@Override
public void transact() {
System.out.println("我要理财!!!我这里有200W");
}
}
案例三:抽象员工系统,分全职工和小时工,输出他们的工资(生日当月工资加二百)
员工抽象类 Employee.java:
@Data
@AllArgsConstructor
@NoArgsConstructor
public abstract class Employee {
private String name;
private Integer number;
private MyDate birthday;
public abstract double calculateSalary();
public String toString() {
return "name = " + name + ",number = " + number + ", birthday = " + birthday.toDateString();
}
//模板方法:返回工资
public final double earnings() {
return calculateSalary() + isBirthday();
}
public double isBirthday() {
Calendar now = Calendar.getInstance();
return birthday.getMonth() - 1 == now.get(Calendar.MONTH) ? 200 : 0;
}
}
@Data
@AllArgsConstructor
@NoArgsConstructor
class MyDate {
private Integer year;
private Integer month;
private Integer day;
public String toDateString() {
return year + "年" + month + "月" + day + "日";
}
}
全职工类 SalariedEmployee.java
@Data
@NoArgsConstructor
public class SalariedEmployee extends Employee {
private double monthlySalary;
public SalariedEmployee(String name, Integer number, MyDate birthday, double monthlySalary) {
super(name, number, birthday);
this.monthlySalary = monthlySalary;
}
@Override
public double calculateSalary() {
return monthlySalary;
}
public String toString() {
return "SalariedEmployee[" + super.toString() + "]";
}
}
小时工类 HourlyEmployee.java
@Data
@NoArgsConstructor
public class HourlyEmployee extends Employee {
private double wage;//小时工资
private int hour;//月工作小时
@Override
public double calculateSalary() {
return wage * hour;
}
public HourlyEmployee(String name, Integer number, MyDate birthday, double wage, int hour) {
super(name, number, birthday);
this.wage = wage;
this.hour = hour;
}
public String toString() {
return "HourlyEmployee[" + super.toString() + "]";
}
}
测试类 PayrollSystem.java
public class PayrollSystem {
public static void main(String[] args) {
Employee[] emps = new Employee[2];
emps[0] = new SalariedEmployee("张小亮", 1001, new MyDate(1992, 1, 21), 18000);
emps[1] = new HourlyEmployee("刘大地", 1002, new MyDate(1998, 4, 10), 21, 18);
for (Employee emp : emps) {
System.out.println(emp.toString());
System.out.println("工资为:" + emp.earnings());
}
}
}