设计模式(21)之模板模式

模板模式(Template Pattern)在一个父抽象类中定义了方法的模板,它的具体实现子类可以按需要重写抽象方法,但实际调用是以父抽象类中定义的非抽象方式进行。

一、需求举例:

在项目团队中,团队人员分为工程师和产品经理,工程师每天签到(sign in),早会(morning meeting),代码开发和bug修复(work),一天工作终结(summary),工产品经理每天签到(sign in),早会(morning meeting),撰写文档(work),一天需求工作终结(summary)。

二、类图:

三、代码实例:

1.员工抽象类:Employee

package com.lance.template;

public abstract class Employee {

    protected String name;


    public Employee(String name) {
        this.name = name;
    }

    public abstract void signIn();

    public abstract void morningMeeting();

    public abstract void work();

    public abstract void summary();

    public void workByDay() {
        signIn();
        morningMeeting();
        work();
        summary();

    }

}

2.工程师类:Engineer

package com.lance.template;

import java.util.Date;

public class Engineer extends Employee {
    public Engineer(String name) {
        super(name);
    }

    @Override
    public void signIn() {
        System.out.println("Engineer " + name + " sign in at:" + new Date());
    }

    @Override
    public void morningMeeting() {
        System.out.println("Engineer " + name + " morning meeting at:" + new Date());
    }

    @Override
    public void work() {
        System.out.println("Engineer " + name + " working......");
    }

    @Override
    public void summary() {
        System.out.println("Engineer " + name + " summary code question!");
    }
}

3.产品经理类:ProductManager

package com.lance.template;

import java.util.Date;

public class ProductManager extends Employee {
    public ProductManager(String name) {
        super(name);
    }

    @Override
    public void signIn() {

        System.out.println("Product Manager " + name + " sign in at:" + new Date());

    }


    @Override
    public void morningMeeting() {
        System.out.println("Product Manager " + name + " morning meeting at:" + new Date());
    }

    @Override
    public void work() {
        System.out.println("Product Manager " + name + " working......");
    }

    @Override
    public void summary() {
        System.out.println("Product Manager " + name + " summary document question!");
    }
}

4.主类:TemplatePattern

package com.lance.template;

public class TemplatePattern {

    public static void main(String[] args) {
        System.out.println("==========start==========");

        Employee engineer = new Engineer("[Lance]");
        engineer.workByDay();

        System.out.println("=======================");

        Employee productManager = new ProductManager("[Mary]");
        productManager.workByDay();

        System.out.println("==========end==========");

    }
}


输出结果:

==========start==========
Engineer [Lance] sign in at:Mon Jul 08 14:04:29 CST 2019
Engineer [Lance] morning meeting at:Mon Jul 08 14:04:29 CST 2019
Engineer [Lance] working......
Engineer [Lance] summary code question!
=======================
Product Manager [Mary] sign in at:Mon Jul 08 14:04:29 CST 2019
Product Manager [Mary] morning meeting at:Mon Jul 08 14:04:29 CST 2019
Product Manager [Mary] working......
Product Manager [Mary] summary document question!
==========end==========

你可能感兴趣的:(设计模式(21)之模板模式)