The template method pattern shows a programmer's day

Please indicate the source of Reprint: http://blog.csdn.net/lmj623565791/article/details/26276093

Continue design pattern ~ template method pattern

For the old routine, first look at the definition: it defines the skeleton of an algorithm and delays some steps to subclasses. The template method enables subclasses to redefine the steps of the algorithm without changing the structure of the algorithm.

Simply look at the definition. The template method defines the steps of an algorithm and allows subclasses to provide implementation for one or more steps. If you come to work, please have a clear definition of entertainment. A brief description: the company has procedures, testing, HR, project managers, etc. the following uses the template method mode to record the working conditions of all personnel:

First, a superclass is defined in which a workOneDay method is set as the skeleton of the algorithm:

package com.zhy.pattern.template;
 
public abstract class Worker
{
    protected String name;
 
    public Worker(String name)
    {
        this.name = name;
    }
 
    /**
     * Record the day's work
     */
    public final void workOneDay()
    {
 
        System.out.println("-----------------work start ---------------");
        enterCompany();
        computerOn();
        work();
        computerOff();
        exitCompany();
        System.out.println("-----------------work end ---------------");
 
    }
 
    /**
     * work
     */
    public abstract void work();
 
    /**
     * Turn off the computer
     */
    private void computerOff()
    {
        System.out.println(name + "Turn off the computer");
    }
 
    /**
     * Turn on the computer
     */
    private void computerOn()
    {
        System.out.println(name + "Turn on the computer");
    }
 
    /**
     * Enter the company
     */
    public void enterCompany()
    {
        System.out.println(name + "Enter the company");
    }
 
    /**
     * Leave the company
     */
    public void exitCompany()
    {
        System.out.println(name + "Leave the company");
    }
 
}

The skeleton defines one of the following steps:

a. Enter the company

b. Turn on the computer

c. Work situation

d. Turn off the computer

e. Leave the company

We can see that a, b, d and e have been implemented in the superclass, and the subclass only implements the abstract method of work to record the daily work situation. The following types of losers are admitted:

Procedure:

package com.zhy.pattern.template;
 
public class ITWorker extends Worker
{
 
    public ITWorker(String name)
    {
        super(name);
    }
 
    @Override
    public void work()
    {
        System.out.println(name + "Write program-measure bug-fix bug");
    }
 
}
package com.zhy.pattern.template;
 
public class HRWorker extends Worker
{
 
    public HRWorker(String name)
    {
        super(name);
    }
 
    @Override
    public void work()
    {
        System.out.println(name + "Look at your resume-phone-answer the phone");
    }
 
}
package com.zhy.pattern.template;
 
public class QAWorker extends Worker
{
 
    public QAWorker(String name)
    {
        super(name);
    }
 
    @Override
    public void work()
    {
        System.out.println(name + "Write test cases-Submit bug-Write test cases");
    }
 
}

Let's test:

package com.zhy.pattern.template;
 
public class Test
{
    public static void main(String[] args)
    {
 
        Worker it1 = new ITWorker("Hongyang");
        it1.workOneDay();
        Worker it2 = new ITWorker("Lao Zhang");
        it2.workOneDay();
        Worker hr = new HRWorker("Didi");
        hr.workOneDay();
        Worker qa = new QAWorker("Lao Li");
        qa.workOneDay();
        Worker pm = new ManagerWorker("Pit goods");
        pm.workOneDay();
 
    }
}

Output result:

-----------------work start ---------------
Hongyang enters the company
 Hongyang turns on the computer
 Hongyang write program-measure bug-fix bug
 Hongyang turns off the computer
 Hongyang leaves the company
-----------------work end ---------------
-----------------work start ---------------
Didi enters the company
 DeeDee, turn on the computer
 DeeDee, look at your resume-phone-answer the phone
 DeeDee, turn off the computer
 Didi left the company
-----------------work end ---------------
-----------------work start ---------------
Lao Li enters the company
 Lao Li turns on the computer
 Lao Li writes test cases-Submit bug-Write test cases
 Lao Li turns off the computer
 Lao Li left the company
-----------------work end ---------------
-----------------work start ---------------
Pit goods into the company
 Open the computer
 Pit cargo fight dota...
Pit cargo turn off the computer
 Pit goods leave the company
-----------------work end ---------------

Well, congratulations on learning another design pattern, template method pattern.

Let's take a look at the class diagram of the template method pattern and the class diagram of our program:

Hooks can also be set optionally in the template method. For example, if we want to record the time when programmers leave the company, we can add a hook in the superclass:

    public boolean isNeedPrintDate()
    {
        return false;
    }
    /**
     * Leave the company
     */
    public void exitCompany()
    {
        if (isNeedPrintDate())
        {
            System.out.print(new Date().toLocaleString()+"-->");
        }
        System.out.println(name + "Leave the company");
    }
package com.zhy.pattern.template;
 
public class ITWorker extends Worker
{
 
    public ITWorker(String name)
    {
        super(name);
    }
 
    @Override
    public void work()
    {
        System.out.println(name + "Write program-measure bug-fix bug");
    }
 
    @Override
    public boolean isNeedPrintDate()
    {
        return true;
    }
    
}

Finally, let's look at the test results:

-----------------work start ---------------
Hongyang enters the company
 Hongyang turns on the computer
 Hongyang write program-measure bug-fix bug
 Hongyang turns off the computer
2014-5-19 19:17:05-->Hongyang leaves the company
-----------------work end ---------------

 

Tags: Design Pattern

Posted by christine75 on Fri, 20 May 2022 15:37:30 +0300