本站不再更新,欢迎光临 java开发技术网
随笔-230  评论-230  文章-8  trackbacks-0

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 有猫相伴的日子 阅读(384) 评论(0)  编辑  收藏 所属分类: Patterns

只有注册用户登录后才能发表评论。


网站导航:
 
本站不再更新,欢迎光临 java开发技术网