最好大家能给出最明了的例子java与模式中的例子不易体会到它的好处

解决方案 »

  1.   

    我给一个抽象工厂方法的例子吧!
    --
    public class PCTest {
    public static void main(String[] args) {
    CPU IBMcpu = (new IBMPC()).CPUfactory();
    HD IBMhd = (new IBMPC()).HDfactory();
    CPU HPcpu = (new HPPC()).CPUfactory();
    HD HPhd = (new HPPC()).HDfactory();
    System.out.println(IBMcpu.model());
    System.out.println(IBMhd.model());
    System.out.println(HPcpu.model());
    System.out.println(HPhd.model());
      }
    }
    --
    public class IBMPC implements PC {
    public CPU CPUfactory() {
    return (new IBMCPU());
    }
    public HD HDfactory() {
    return (new IBMHD());
    }
    }
    --
    public class HPPC implements PC {
    public CPU CPUfactory() {
    return (new HPCPU());
    }
    public HD HDfactory() {
    return (new HPHD());
    }
    }
    --
    public class IBMCPU implements CPU {
    private int speed;
    private String model;
    IBMCPU(){
    speed = 20;
    model = "IBM600";
    }
    IBMCPU(int speed, String model) {
    this.speed = speed;
    this.model = model;
    }
    public int speed() {
    return speed;
    }
    public String model(){
    return model;
    }

    }
    --
    public class HPCPU implements CPU {
    private int speed;
    private String model;
    HPCPU(){
    speed = 10;
    model = "HP600";
    }
    HPCPU(int speed, String model) {
    this.speed = speed;
    this.model = model;
    }
    public int speed() {
    return speed;
    }
    public String model(){
    return model;
    }

    }
    --
    public class IBMHD implements HD {
    private int capacity;
    private String model;

    IBMHD() {
    capacity = 5400;
    model = "IBM-HD-2";
    }
    IBMHD(int capacity, String model) {
    this.capacity = capacity;
    this.model = model;
    }
    public int capacity() {
    return capacity;
    }
    public String model() {
    return model;
    }
    }
    --
    public class HPHD implements HD {
    private int capacity;
    private String model;

    HPHD() {
    capacity = 5400;
    model = "HP-HD-1";
    }
    HPHD(int capacity, String model) {
    this.capacity = capacity;
    this.model = model;
    }
    public int capacity() {
    return capacity;
    }
    public String model() {
    return model;
    }
    }
    --
    public interface CPU {
    public int speed();
    public String model();
    }
    --
    public interface HD {
    public int capacity();
    public String model();
    }
      

  2.   

    我一般在这两种情况下会考虑Factory:
    1.父类Sample有子类SampleA,SampleB,SampleC.......
    public class SampleFactory{
        public static Sample sampleFactory(int i){
         if(i==1)
           return new SampleA();
         else if(i==2)
           return new SampleB();
         ......
        }
    }
    得到sampleA:(SampleA)SampleFactory.sampleFactory(1);2.有限的对象来进行分配。你看看ConnectionPools是怎么写的。
       Hashtable Pools=new Hashtable(20);
        public Connection getConnection(String name) 
     { 
      DBConnectionPool pool = (DBConnectionPool) pools.get(name); 
      if (pool != null) 
      { 
      return pool.getConnection(); 
      } 
      return null; 
     }