Difference between Strategy pattern and Command pattern

后端 未结 6 1555
一整个雨季
一整个雨季 2021-01-29 21:22

What is the difference between the Strategy pattern and the Command pattern? I am also looking for some examples in Java.

6条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-29 21:24

    Words are already given. Here is the difference in concrete code.

    public class ConcreteStrategy implements BaseStrategy {
    
        @Override
        public void execute(Object argument) {
            // Work with passed-in argument.
        }
    
    }
    

    public class ConcreteCommand implements BaseCommand {
    
        private Object argument;
    
        public ConcreteCommand(Object argument) {
            this.argument = argument;
        }
    
        @Override
        public void execute() {
            // Work with own state.
        }
    
    }
    

提交回复
热议问题