A reconstruction of business code

Posted by titangf on Tue, 19 Nov 2019 19:26:57 +0100

Business needs

The prompt on the side of listening task is dynamically prompted according to the listening time and the completion of the task.

The flow chart is as follows

Pseudo code of client

Code comments before Refactoring:

  1. When the listening task configured by the server changes dynamically, it can be handled correctly and the code is flexible.
  2. There is duplicate code, and if else is too much, and readability is poor.

Refactored code

After the client obtains the list of listening task information, the processor chain is assembled according to the listening time threshold, listening reward points and listening task status of each task:

After assembly, input "listening time", and you can get prompt dynamically.

The detailed code is as follows:

public class ListenTaskProcessor {

	//Accumulated bonus points
	private static Integer totalPoint = 0;

	// threshold
	protected Integer threshold;

	// Bonus points
	protected Integer point;

	// Whether points have been collected
	protected Boolean hasGetPoint;

	// Successor processor
	protected ListenTaskProcessor nextProcessor;

	public ListenTaskProcessor(Integer threshold, Integer point, Boolean hasGetPoint) {
		super();
		this.threshold = threshold;
		this.point = point;
		this.hasGetPoint = hasGetPoint;
	}

	/**
	 * @param hasListenTime Listening time
	 * @param ungetPoint No points
	 * @param totalPoint Accumulated bonus points
	 * @return Listen to the prompts of the task
	 */
	public String process(Integer hasListenTime, Integer ungetPoint) {

		totalPoint = totalPoint + this.point;

		//The listening time is less than the time threshold of the current processor, so there is no need to accumulate points
		if (hasListenTime < threshold) {
			return ungetPoint > 0 ? strOfGetPoint(ungetPoint) : strOfGoonListen(hasListenTime);
		}
		//If there is a follow-up processor, the accumulated integration is directly handed over to the follow-up processor for processing
		else if (null != this.nextProcessor) {
			ungetPoint = addUngetPoints(ungetPoint);
			return this.nextProcessor.process(hasListenTime, ungetPoint);
		}
		//If the listening time is greater than the time threshold of the current processor, the current processor has no successor
		else {
			ungetPoint = addUngetPoints(ungetPoint);
			return ungetPoint > 0 ? strOfGetPoint(ungetPoint) : strOfNextday();
		}
	}




	/**
	 * Accumulate points to be collected
	 * @param ungetPoint Previous points to be collected
	 * @return Points to be collected now
	 */
	private Integer addUngetPoints(Integer ungetPoint) {
		return hasGetPoint ? ungetPoint : ungetPoint + point;
	}


	/**
	 * Prompt for users to continue listening
	 * @param hasListenTime Minutes listened
	 * @return Prompt for users to continue listening
	 */
	private String strOfGoonListen(Integer hasListenTime) {
		return "Listen again" + (threshold - hasListenTime) + "Minutes to collect" + this.point + "integral";
	}


	/**
	 * Prompt to inform users to collect points
	 * @param ungetPoint Points to be collected
	 * @return Prompt for users to receive points
	 */
	private String strOfGetPoint(Integer ungetPoint) {
		return "Receive" + ungetPoint + "integral";
	}

	/**
	 * Tips for getting points tomorrow
	 * @return Tips for getting points tomorrow
	 */
	private String strOfNextday() {
		return "Available tomorrow"+totalPoint+"integral";
	}

	public static void main(String[] args) {

		ListenTaskProcessor processor1 = new ListenTaskProcessor(10, 10, false);
		ListenTaskProcessor processor2 = new ListenTaskProcessor(20, 20, false);
		ListenTaskProcessor processor3 = new ListenTaskProcessor(60, 60, false);
		processor2.nextProcessor = processor3;
		processor1.nextProcessor = processor2;

		System.out.println(processor1.process(1, 0));

	}

}

Topics: Programming less