package com.haut.grain.junit.test;

public  class Command {
private Object state;
public void setState(Object state) {
    this.state = state;
}
public Object getState() {
    return this.state;
}
public  void execute(){
    System.out.println("当前的状态是:"+state.toString());
}
}

以上是command类为基类

实现类UpCommand

package com.haut.grain.junit.test;

public class UpCommand extends Command{

@Override
public void execute() {
    System.out.println("向上命令的状态是:"+this.getState());
}

}

CommandManager类

package com.haut.grain.junit.test;

public abstract class CommandManager {

    public void process(Object commandState) {
        // grab a new instance of the appropriate Command interface
        Command command = createCommand();
        // set the state on the (hopefully brand new) Command instance
        command.setState(commandState);
        command.execute();
    }

    // okay... but where is the implementation of this method?
    protected abstract Command createCommand();
}

配置文件

	<bean ></bean>
	<bean >
	<lookup-method name="createCommand" bean="upCommand"/>
	</bean>

     总结:上面就是通过createCommand方法来注入command对象,当是抽象方法时替换,如果是实现的方法了覆盖。

相关文章:

  • 2021-07-08
  • 2021-12-19
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-13
  • 2022-01-01
猜你喜欢
  • 2022-12-23
  • 2021-05-12
  • 2020-05-01
  • 2021-05-20
  • 2018-05-18
  • 2021-11-15
相关资源
相似解决方案