Command有很多種實(shí)現(xiàn)方式,可以隨便的按便這個(gè)規(guī)律來自由發(fā)揮。
package com.pdw.pattern;
import java.util.*;
import org.apache.commons.lang.StringUtils;
interface Command{
?public void execute();
}
interface Parameter{
?public? String getCommandType();
}
class Engineer implements Command{
?public void execute() {
??// TODO Auto-generated method stub
??System.out.println("Enginer....");
?}
?
}
class Programer implements Command{
?public void execute() {
??// TODO Auto-generated method stub
??System.out.println("Programer....");
?}
?
}
class Doctor implements Command{
?public void execute() {
??// TODO Auto-generated method stub
??System.out.println("Doctor.............");
?}
?
}
class EngineerParameter implements Parameter{
?public String getCommandType() {
??// TODO Auto-generated method stub
??return "Engineer";
?}
?
}
class CommandProduce{
?public static List commandList=new ArrayList();
?public CommandProduce(){
??commandList.add(new Engineer());
??commandList.add(new Programer());
??commandList.add(new Doctor());
?}
?public static Command getCommand(Parameter p){
??Iterator it=commandList.iterator();
??while(it.hasNext()){
???Object c=(Object)it.next();
???System.out.println(c.getClass().getName());
???if(StringUtils.indexOf(c.getClass().getName(),p.getCommandType())>0){
????return (Command)c;
???}
??}
??return null;
?}
}
public class CommandImpl {
?public static void main(String[] args) {
??// TODO Auto-generated method stub
??EngineerParameter ep=new EngineerParameter();
??CommandProduce cp=new CommandProduce();
??(CommandProduce.getCommand(ep)).execute();
?}
}