Abstract class application -- template design pattern

Posted by alpha_juno on Sat, 11 Apr 2020 18:15:53 +0200

Now suppose there are three kinds of reality (or more things):

Robot: it has two basic operations: charging and working;

Human: it has three basic operations: eating, working and sleeping;

Pig: it has two basic operations: eating and sleeping.


A behavior class defined by

abstract class Action {					// Define an abstract behavior class. The behavior is not concrete
	// When defining a constant, you must ensure that the result of adding two contents is not another behavior. For example, if the result of EAT + SLEEP is 6, it will not conflict with other values
	public static final int EAT = 1;	// Define the order to eat
	public static final int SLEEP = 5;	// Define sleep commands
	public static final int WORK = 7;	// Commands to define work
	public void command(int flag) {
		switch (flag) {	// switch only supports numerical judgment, while if supports conditional judgment
		case EAT:	// Current operation for eating
			this.eat();// Call the specific "eat" method in the subclass
			break;
		case SLEEP:	// Operation currently sleeping
			this.sleep();	// Call the specific sleep method in the subclass
			break;
		case WORK:	// Actions currently working
			this.work();// Call specific "work" methods in subclasses
			break;
		case EAT + WORK:	// Behavior combination, this section is just an example, not a demonstration
			this.eat();	// Call "eat" method
			this.work();// Method to call work
			break;
		}
	}
	public abstract void eat();	// Define the operation standard of subclass
	public abstract void sleep();	// Define the operation standard of subclass
	public abstract void work();	// Define the operation standard of subclass
}

Define the behavior subclass of robot

class Robot extends Action {			// Defining robot behavior
	public void eat() {		// Override action
		System.out.println("Robot power up!");
	}
	public void sleep() {	// This operation does not need to be overridden, so the method body is empty
	}
	public void work() {		// Override action
		System.out.println("Robots are working hard!");
	}
}

Define human class

class Human extends Action {			// Defining human behavior
	public void eat() {		// Override action
		System.out.println("Humans are eating!");
	}
	public void sleep() {		// Override action
		System.out.println("Humans are sleeping and resting!");
	}
	public void work() {		// Override action
		System.out.println("People work hard for their dreams!");
	}
}

Define pig class

class Pig extends Action {
	public void eat() { 		// Override action
		System.out.println("Pigs are eating trough!");
	}
	public void sleep() { 			// Override action
		System.out.println("Pigs are sleeping and fattening up!");
	}
	public void work() {		// This operation does not need to be overridden, so the method body is empty
	}
}

Testing behavior

public class TestDemo {
	public static void main(String args[]) {
		fun(new Robot());	// Behavior subclass of transfer robot
		fun(new Human());	// Transmission of human behavior subclass
		fun(new Pig());	// Behavioral subclasses of transmitting pigs
	}
	/**
	 * Perform specific operation actions. Assume that the Department only performs three actions: EAT, SLEEP and WORK
	 * act Specific behavior object
	 */
	public static void fun(Action act) {
		act.command(Action.EAT);		// Call "eat" operation
		act.command(Action.SLEEP);		// Call "sleep" operation
		act.command(Action.WORK);		// Call work action
	}
}


Program execution result:

Robot power up! (robot behavior)

Robots are working hard! (robot behavior)

Humans are eating! (human behavior)

Humans are sleeping and resting! (human behavior)

People work hard for their dreams! (human behavior)

Pigs are eating trough! (pig behavior)

Pigs are sleeping and fattening up! (pig behavior)