9. Design patterns, templates, methods and patterns show the programmer's day

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

Looking at the definition briefly, the template method defines the steps of an algorithm and allows subclasses to provide implementations for one or more steps. The definition is fairly clear. Here is an example to show the company's work situation (only for entertainment, if there are similarities, please check in). Briefly describe: the company has programmers, testers, HR, project managers, etc., use the template method mode below to record the work situation of all personnel:

First, let's take a super class. A workOneDay method is defined in the super class and 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;
	}
 
	/**
	 * 记录一天的工作
	 */
	public final void workOneDay()
	{
 
		System.out.println("-----------------work start ---------------");
		enterCompany();
		computerOn();
		work();
		computerOff();
		exitCompany();
		System.out.println("-----------------work end ---------------");
 
	}
 
	/**
	 * 工作
	 */
	public abstract void work();
 
	/**
	 * 关闭电脑
	 */
	private void computerOff()
	{
		System.out.println(name + "关闭电脑");
	}
 
	/**
	 * 打开电脑
	 */
	private void computerOn()
	{
		System.out.println(name + "打开电脑");
	}
 
	/**
	 * 进入公司
	 */
	public void enterCompany()
	{
		System.out.println(name + "进入公司");
	}
 
	/**
	 * 离开公司
	 */
	public void exitCompany()
	{
		System.out.println(name + "离开公司");
	}
 
}

Defines a skeleton for going to work (algorithm), including the following steps:
a. Enter the company

b. Turn on the computer

c. Work situation

d. Turn off the computer

e. Leaving the company

It can be seen that we have implemented a, b, d, and e in the super class, and the sub class only implements the abstract method of work to record the daily work situation. The following types of diaosi are admitted:

Programmer:

package com.zhy.pattern.template;
 
public class ITWorker extends Worker
{
 
	public ITWorker(String name)
	{
		super(name);
	}
 
	@Override
	public void work()
	{
		System.out.println(name + "写程序-测bug-fix bug");
	}
 
}

HR:

package com.zhy.pattern.template;
 
public class HRWorker extends Worker
{
 
	public HRWorker(String name)
	{
		super(name);
	}
 
	@Override
	public void work()
	{
		System.out.println(name + "看简历-打电话-接电话");
	}
 
}

Testers:

package com.zhy.pattern.template;
 
public class QAWorker extends Worker
{
 
	public QAWorker(String name)
	{
		super(name);
	}
 
	@Override
	public void work()
	{
		System.out.println(name + "写测试用例-提交bug-写测试用例");
	}
 
}

project manager:

package com.zhy.pattern.template;
 
public class ManagerWorker extends Worker
{
 
	public ManagerWorker(String name)
	{
		super(name);
	}
 
	@Override
	public void work()
	{
		System.out.println(name + "打dota...");
	}
 
}

Let's test it below:

package com.zhy.pattern.template;
 
public class Test
{
	public static void main(String[] args)
	{
 
		Worker it1 = new ITWorker("鸿洋");
		it1.workOneDay();
		Worker it2 = new ITWorker("老张");
		it2.workOneDay();
		Worker hr = new HRWorker("迪迪");
		hr.workOneDay();
		Worker qa = new QAWorker("老李");
		qa.workOneDay();
		Worker pm = new ManagerWorker("坑货");
		pm.workOneDay();
 
	}
}

Output result:

-----------------work start ---------------
鸿洋进入公司
鸿洋打开电脑
鸿洋写程序-测bug-fix bug
鸿洋关闭电脑
鸿洋离开公司
-----------------work end ---------------
-----------------work start ---------------
迪迪进入公司
迪迪打开电脑
迪迪看简历-打电话-接电话
迪迪关闭电脑
迪迪离开公司
-----------------work end ---------------
-----------------work start ---------------
老李进入公司
老李打开电脑
老李写测试用例-提交bug-写测试用例
老李关闭电脑
老李离开公司
-----------------work end ---------------
-----------------work start ---------------
坑货进入公司
坑货打开电脑
坑货打dota...
坑货关闭电脑
坑货离开公司
-----------------work end ---------------

Well, congratulations, and learned a design pattern, template method pattern.

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

Insert picture description here

Hooks can also be optionally set in the template method. For example, if we want to record the time the programmer left the company, we can add a hook to the super class:

public boolean isNeedPrintDate()
	{
		return false;
	}
	/**
	 * 离开公司
	 */
	public void exitCompany()
	{
		if (isNeedPrintDate())
		{
			System.out.print(new Date().toLocaleString()+"-->");
		}
		System.out.println(name + "离开公司");
	}

An isNeedPrintDate method is added to the super class, and it returns false by default without printing the time. If a certain subclass needs to call the printing time, you can override the hook method and return true. For example, the program has copied this method:

package com.zhy.pattern.template;
 
public class ITWorker extends Worker
{
 
	public ITWorker(String name)
	{
		super(name);
	}
 
	@Override
	public void work()
	{
		System.out.println(name + "写程序-测bug-fix bug");
	}
 
	@Override
	public boolean isNeedPrintDate()
	{
		return true;
	}
	
}

Finally, look at the test results:

-----------------work start ---------------
鸿洋进入公司
鸿洋打开电脑
鸿洋写程序-测bug-fix bug
鸿洋关闭电脑
2014-5-19 19:17:05-->鸿洋离开公司
-----------------work end ---------------

Well, about hooks, the super class can provide default implementation or empty implementation, and the subclass can override or not, depending on the requirements.

Recently, I wrote another crawler program, using the template method mode, and I will share with you:

Requirement analysis: The program needs to crawl data for 20 specific websites; the result data returned by each website page is different, the URL is different, the parameters are different, etc.; but the crawling process is the same.

So I designed it like this:

a. Define a Rule class (including: url, params, request_method, and which piece of data to return [according to the selector])

b. Grab data through Rule

c. Process the data

I defined the skeleton of the algorithm in the above 3 steps, b is the super class implementation, and a and c are realized by the subclass:

package com.zhy.pattern.template;
 
public abstract class AbsExtractInfo
{
 
	
	/**
	 * 抓取的算法骨架
	 */
	public void extract()
	{
		Rule rule = generateRule() ;
		List<Element> eles = getInfosByRule(rule);
		dealResult(eles);
	}
	
	/**
	 * 生成一个Rule
	 * @return
	 */
	public abstract Rule generateRule();
	
	/**
	 * 抓取的实现
	 * @param rule
	 * @return
	 */
	private List<Element> getInfosByRule(Rule rule)
	{
		// the implements omitted 
	}
	/**
	 * 处理抓取的结果
	 * @param results
	 */
	public void dealResult(List<Element> results);
}

Among them, the GenerateRule method happens to be the abstract method pattern in the factory pattern (define an interface for creating objects, but the subclass determines which class to instantiate. The factory method pattern defers the process of class instantiation to the subclass) , If you forget, you can check the design pattern factory pattern.

Okay, that's it. Finally, everyone is welcome to leave a message.

Guess you like

Origin blog.csdn.net/GTC_GZ/article/details/108745584