使用newInstance创建对象

时间:2012-02-22 14:46:04

标签: java factory

我正在从 Thinking In Java 这本书中做一些练习。我练习一些问题。它说:

  

构造函数是一种工厂方法。修改RegisteredFactories.java,以便代替使用显式工厂,类对象存储在List中,newInstance()用于创建每个对象。

我怎样才能实现这一目标?下面是工厂接口和RegisteredFactories类的代码。谢谢

package typeinfo.factory;    
public interface Factory<T> { T create(); } ///:~
package cont;

import typeinfo.factory.*;
import java.util.*;


class Part {

  public String toString() {
    return getClass().getSimpleName();
  }

  static List<Factory<? extends Part>> partFactories =
    new ArrayList<Factory<? extends Part>>();     

  static {
    // Collections.addAll() gives an "unchecked generic
    // array creation ... for varargs parameter" warning.
    partFactories.add(new FuelFilter.Factory());
    partFactories.add(new AirFilter.Factory());
    partFactories.add(new CabinAirFilter.Factory());
    partFactories.add(new OilFilter.Factory());
    partFactories.add(new FanBelt.Factory());
    partFactories.add(new PowerSteeringBelt.Factory());
    partFactories.add(new GeneratorBelt.Factory());
  }

  private static Random rand = new Random(47);
  public static Part createRandom() {
    int n = rand.nextInt(partFactories.size());
    return partFactories.get(n).create();
  }
}

class Filter extends Part {}

class FuelFilter extends Filter {
  // Create a Class Factory for each specific type:
  public static class Factory
  implements typeinfo.factory.Factory<FuelFilter> {
    public FuelFilter create() { return new FuelFilter(); }
  }
}

class AirFilter extends Filter {
  public static class Factory
  implements typeinfo.factory.Factory<AirFilter> {
    public AirFilter create() { return new AirFilter(); }
  }
} 

class CabinAirFilter extends Filter {
  public static class Factory
  implements typeinfo.factory.Factory<CabinAirFilter> {
    public CabinAirFilter create() {
      return new CabinAirFilter();
    }
  }
}

class OilFilter extends Filter {
  public static class Factory
  implements typeinfo.factory.Factory<OilFilter> {
    public OilFilter create() { return new OilFilter(); }
  }
} 

class Belt extends Part {}

class FanBelt extends Belt {
  public static class Factory
  implements typeinfo.factory.Factory<FanBelt> {
    public FanBelt create() { return new FanBelt(); }
  }
}

class GeneratorBelt extends Belt {
  public static class Factory
  implements typeinfo.factory.Factory<GeneratorBelt> {
    public GeneratorBelt create() {
      return new GeneratorBelt();
    }
  }
} 

class PowerSteeringBelt extends Belt {
  public static class Factory
  implements typeinfo.factory.Factory<PowerSteeringBelt> {
    public PowerSteeringBelt create() {
      return new PowerSteeringBelt();
    }
  }
} 

public class RegisteredFactories {
  public static void main(String[] args) {
    for(int i = 0; i < 10; i++)
      System.out.println(Part.createRandom());
  }
}

2 个答案:

答案 0 :(得分:2)

这里:

import java.util.*;

class Part {

  public String toString() {

    return getClass().getSimpleName();

  }

  static List<Class> partFactories = new ArrayList<Class>();

  static {

    // Collections.addAll() gives an "unchecked generic

    // array creation ... for varargs parameter" warning.

    partFactories.add(FuelFilter.class);

    partFactories.add(AirFilter.class);

    partFactories.add(CabinAirFilter.class);

    partFactories.add(OilFilter.class);

    partFactories.add(FanBelt.class);

    partFactories.add(PowerSteeringBelt.class);

    partFactories.add(GeneratorBelt.class);
  }


  private static Random rand = new Random(47);

  public static Part createRandom() throws Exception {

    int n = rand.nextInt(partFactories.size());

    return (Part)(partFactories.get(n).newInstance());

  }

}



class Filter extends Part {}

class FuelFilter extends Filter {
}
class AirFilter extends Filter {
}
class CabinAirFilter extends Filter {
}
class OilFilter extends Filter {
}
class Belt extends Part {}
class FanBelt extends Belt {
}
class GeneratorBelt extends Belt {
}
class PowerSteeringBelt extends Belt {
}



public class aa{

  public static void main(String[] args) {

    for(int i = 0; i < 10; i++)

      try {System.out.println(Part.createRandom()); }catch (Exception e) {}

  }

}

答案 1 :(得分:0)

static List<Factory<? extends Part>> partFactories
那就是清单,
它包含几个工厂,并在静态块中启动。
此代码使用createRandom方法通过其工厂随机创建对象
我们的想法是,不是通过调用new来创建对象,而是使用其工厂创建一个对象 这里的所有工厂都实现了接口Factory,这意味着它们有一个名为create()的方法,它返回所需类的新实例(工厂所属的类)