Command有很多種實現方式,可以隨便的按便這個規律來自由發揮。
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();
?}
}
posted on 2006-07-13 23:00
有貓相伴的日子 閱讀(396)
評論(0) 編輯 收藏 所屬分類:
Patterns