commandfactory.java
来自「Java 议程管理系统。使用GoF设计模式进行系统设计」· Java 代码 · 共 76 行
JAVA
76 行
/**
*
*/
package agenda.ctrl;
import java.util.*;
import agenda.cmd.Command;
/**
* class definition for command factory
*
* @author Cyberpet
*
*/
public class CommandFactory {
private HashMap<String, Command> cmdTable = new HashMap<String, Command>();
/*
* add command into the hashmap
*/
public boolean addCmd(Command cmd) {
if (cmd != null)
return this.cmdTable.put(cmd.getName(), cmd) != null;
else
return false;
}
/*
* get command by name
*/
public Command getCmdByName(String name) {
return this.cmdTable.get(name);
}
public String toString() {
String res = new String();
Collection<Command> Col = this.cmdTable.values();
for (Command cmd : Col)
res += cmd.toString();
return res;
}
/**
* static class for Singleton design pattern, instance of CommandFactory
* will be created when the first time you make a call to
* CommandFactory.getInstance
*
* @author Cyberpet
* @see CommandFactory#getInstance()
*/
static class CommandFactorySingletonHolder {
static CommandFactory instance = new CommandFactory();
}
/**
* get the only instance of CommandFactory
*
* @return CommandFactory
* @see CommandFactory
*/
public static CommandFactory getInstance() {
return CommandFactorySingletonHolder.instance;
}
/**
* @param args
* @return void
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?