web123456

Java Classic Design Pattern--04. Behavioral Pattern

/**Command Execution Class Interface**/ interface Employee { void execute(String s); } /**Command class, strategy **/ public class Commander { /**Strategy**/ String thought; /**The managers under the strategy**/ Employee manager; /**Initialization Strategy**/ public Commander(String thought, Employee manager) { this.thought = thought; this.manager = manager; } /**Release Strategy**/ public void command() { /**The administrator executes the policy, the commander does not know how the administrator executes **/ manager.execute(thought); } } /**Command execution class, administrator, implement execution class interface**/ class Manager implements Employee { /**Executors under the manager**/ List<Employee> executors; /**Initialization Manager**/ public Manager(List<Employee> executors) { this.executors = executors; } /**Command execution method**/ @Override public void execute(String thought) { /**Travel the executor, execute the specific method**/ Iterator<Employee> iterator = executors.listIterator(); while(iterator.hasNext()) { iterator.next().execute(thought); iterator.remove(); } /** Assign tasks to different executors according to your own ideas**/ executeParticular(thought + "Colorful Black"); executeParticular(thought + "Cms backend") ; executeParticular(thought + "Stress Test"); } /**Segment of task methods**/ public void executeParticular(String myThought) { if (myThought.indexOf("Background") != -1) { new CodeExecutor().execute(myThought); } else if (myThought.indexOf("pressure") != -1) { new TestExecutor().execute(myThought); } else if (myThought.indexOf("Colorful Black") != -1) { new DesignExecutor().execute(myThought); } } } /**Executor abstract class, only used as a paradigm auxiliary class**/ abstract class Executor implements Employee { } /**Programmer Executor Class**/ class CodeExecutor extends Executor { /**Programmer's specific execution logic**/ @Override public void execute(String thought) { System.out.println("I am a programmer, I'm responsible for implementing it." + thought + "Logistics"); } } /**Design executor category**/ class DesignExecutor extends Executor { /**Design specific execution logic**/ @Override public void execute(String thought) { System.out.println("I am the design, I am responsible for the design." + thought + "Style"); } } /**Test Executor Class**/ class TestExecutor extends Executor { /**Test specific execution logic**/ @Override public void execute(String thought) { System.out.println("I'm the test, I'm responsible for the test." + thought + "Functions"); } } class CommandTest { public static void main(String[] args) { /**Add executors for department**/ List<Employee> executors = new ArrayList<>(); executors.add(new DesignExecutor()); executors.add(new CodeExecutor()); executors.add(new TestExecutor()); /**Add managers to the department and establish his relationship with the executor**/ Employee manager = new Manager(executors); /**Add a strategy for the department and establish his relationship with the manager**/ Commander commander = new Commander("Let the pig fly", manager); /**Strategist publishes policy**/ commander.command(); } }