CarSimpleFactory.java 533 字节
Newer Older
1 2 3
package com.iluwatar.simplefactory;

/**
4
 * Factory of cars.
5 6
 */
public class CarSimpleFactory {
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
  
  /**
   * Enumeration for different types of cars.
   */
  static enum CarType {
    FORD, FERRARI
  }
  
  /**
   * Factory method takes as parameter a car type and initiate the appropriate class.
   */
  public static Car getCar(CarType type) {
    switch (type) {
      case FORD: return new Ford();
      case FERRARI: return new Ferrari();
      default: throw new IllegalArgumentException("Model not supported.");
    }
  }
25
}