The name of this design Pattern is interesting today — Dirty Flag Pattern.

* * * * 01
* * * *

Avoid unnecessary and time-consuming double-counting.

* * * *
** Example code **

In fubao factory, all p1-P7 employees are less than 35 years old. In Chrysanthemum factory, employees over 35 years old are cleared…… Layoffs, optimization, the last out, not more than 35 years old, etc., all kinds of true and false news let us this kind of Internet people on pins and needles, a sense of crisis full 😨

Like me, I just turned 18 and I’m worried about turning 35. It seems that every 35-year-old Internet person has to become a Courier (we don’t know if we can apply for 😢).

For example, on this day, the boss of the company asked you to calculate the average age of the company every day as a basis for CAI. Currently, we only have an organizational structure tree and an interface to query the age of employees according to their work numbers, so our code might be like this:

Define an interface to query the age of an employee based on the employee ID:

public class EmployeeWebService {        public static Integer        queryEmployeeAge(String userId) {                return Integer.valueOf(userId);    }}
Copy the code

Define a tree node to store the employee structure:

@Datapublic class TreeNode<T> {        private T value;        private List<TreeNode<T>>        children = new ArrayList<>();        public TreeNode(T value) {                this.value = value;    }}
Copy the code

Calculate the average age of employees:

public static Integer queryEmployeeAvgAge (TreeNode<String> employeeUserTree) { Integer totalAge = 0; Integer employeeCount = 0; If (employeeUserTree == null) {system.out.println (" employeeUserTree "); return 0; } Queue<TreeNode> queue = new LinkedList<>(); queue.offer(employeeUserTree); while (! queue.isEmpty()) { TreeNode<String> node = queue.poll(); totalAge += EmployeeWebService. queryEmployeeAge(node.getValue()); employeeCount++; for (TreeNode<String> child : node.getChildren()) { queue.offer(child); } } return (totalAge / employeeCount); }Copy the code
3 * * * *
** Dirty flag mode **

Because it is a tree layer structure, traversal is time-consuming, and each employee also has an RPC call to query the age, but if the calculation is needed every day, if the staff of the company does not change much, and no one enters or leaves the company in a day, the data of the previous day can be used, which is to use the dirty mark mode.

Let’s start by defining two variables that can be stored in a database, file, or cache:

public class Constant { public static Boolean IS_EMPLOYEE_CHANGED = false; public static Integer LASTDAY_EMPLOYEE_AVG_AGE = 0; }Copy the code

For example, define an employee entry and exit interface:

public static void changeEmploy(String userId) { Constant.IS_EMPLOYEE_CHANGED = true; }Copy the code

After these changes our code:

public static Integer queryEmployeeAvgAge(TreeNode<String> employeeUserTree){ if(! Constant.IS_EMPLOYEE_CHANGED) { return Constant.LASTDAY_EMPLOYEE_AVG_AGE; Int age = totalAge/employeeCount; Constant.LASTDAY_EMPLOYEE_AVG_AGE = age; return age; }Copy the code

The class diagram 👇 :

4 * * * *
** Dirty mark mode homework **

1. Identify the risk point code in the sample program

2. Find an example where this mode can be used in production scenarios

3. Use template method pattern to implement dirty tag pattern

WeChat: