ref: http://www.cnblogs.com/zhangchenliang/p/3700820.html
1. "Simple Factory"
package torv.abc; public class HelloMain { public static void main(String[] args) { Driver.createCar("audi").drive(); } } //====================================================== product car abstract class Car { private String brand; public abstract void drive(); public String getBrand() { return brand; } public void setBrand(String brandName) { brand = brandName; } } //------------------------------------------------benz class BenzCar extends Car { public BenzCar() { setBrand("Benz"); } @Override public void drive() { System.out.println(getBrand() + "--go-----"); } } //------------------------------------------------bmw class BmwCar extends Car { public BmwCar() { setBrand("Bmw"); } @Override public void drive() { System.out.println(getBrand() + "--go-----"); } } //------------------------------------------------audi class AudiCar extends Car { public AudiCar() { setBrand("Audi"); } @Override public void drive() { System.out.println(getBrand() + "--go-----"); } } //====================================================== factory class Driver { public static Car createCar(String carName) { Car car = null; if (carName.equalsIgnoreCase("benz")) { car = new BenzCar(); } else if (carName.equalsIgnoreCase("bmw")) { car = new BmwCar(); } else if (carName.equalsIgnoreCase("audi")) { car = new AudiCar(); } return car; } }
2. "Factory Method"
package torv.abc; public class HelloMain { public static void main(String[] args) { Driver driver = new BmwDriver(); driver.createCar().drive(); } } //====================================================== product car abstract class Car { private String brand; public abstract void drive(); public String getBrand() { return brand; } public void setBrand(String brandName) { brand = brandName; } } //------------------------------------------------benz class BenzCar extends Car { public BenzCar() { setBrand("Benz"); } @Override public void drive() { System.out.println(getBrand()+"--go-----"); } } //------------------------------------------------bmw class BmwCar extends Car { public BmwCar() { setBrand("Bmw"); } @Override public void drive() { System.out.println(getBrand()+"--go-----"); } } //------------------------------------------------audi class AudiCar extends Car { public AudiCar() { setBrand("Audi"); } @Override public void drive() { System.out.println(getBrand()+"--go-----"); } } //====================================================== factory abstract class Driver { public abstract Car createCar(); } class BenzDriver extends Driver{ @Override public Car createCar() { return new BenzCar(); } } class BmwDriver extends Driver{ @Override public Car createCar() { return new BmwCar(); } } class AudiDriver extends Driver{ @Override public Car createCar() { return new AudiCar(); } }
3. "Abstract Factory"
package torv.abc; public class HelloMain { public static void main(String[] args) { Driver driver = new SportDriver(); driver.createAudiCar().drive(); } } //====================================================== product car abstract class Car { private String brand; private String kind; public abstract void drive(); public String getBrand() { return brand; } public void setBrand(String brandName) { brand = brandName; } public String getKind() { return kind; } public void setKind(String kindName) { kind = kindName; } } //------------------------------------------------benz abstract class BenzCar extends Car { public BenzCar() { setBrand("Benz"); } } class BenzSportCar extends BenzCar { public BenzSportCar() { setKind("Sport"); } @Override public void drive() { System.out.println(getBrand() + "--" + getKind() + "--go-------"); } } class BenzBizCar extends BenzCar { public BenzBizCar() { setKind("Biz"); } @Override public void drive() { System.out.println(getBrand() + "--" + getKind() + "--go-------"); } } //------------------------------------------------bmw abstract class BmwCar extends Car { public BmwCar() { setBrand("Bmw"); } } class BmwSportCar extends BmwCar { public BmwSportCar() { setKind("Sport"); } @Override public void drive() { System.out.println(getBrand() + "--" + getKind() + "--go-------"); } } class BmwBizCar extends BmwCar { public BmwBizCar() { setKind("Biz"); } @Override public void drive() { System.out.println(getBrand() + "--" + getKind() + "--go-------"); } } //------------------------------------------------audi abstract class AudiCar extends Car { public AudiCar() { setBrand("Audi"); } } class AudiSportCar extends AudiCar { public AudiSportCar() { setKind("Sport"); } @Override public void drive() { System.out.println(getBrand() + "--" + getKind() + "--go-------"); } } class AudiBizCar extends AudiCar { public AudiBizCar() { setKind("Biz"); } @Override public void drive() { System.out.println(getBrand() + "--" + getKind() + "--go-------"); } } //====================================================== factory abstract class Driver { public abstract Car createBenzCar(); public abstract Car createBmwCar(); public abstract Car createAudiCar(); } class SportDriver extends Driver { @Override public Car createBenzCar() { return new BenzSportCar(); } @Override public Car createBmwCar() { return new BmwSportCar(); } @Override public Car createAudiCar() { return new AudiSportCar(); } } class BizDriver extends Driver { @Override public Car createBenzCar() { return new BenzBizCar(); } @Override public Car createBmwCar() { return new BmwBizCar(); } @Override public Car createAudiCar() { return new AudiBizCar(); } }
3. "Simple Factory" + Reflect
package torv.abc; public class HelloMain { public static void main(String[] args) { try { Driver.createCar("torv.abc.BenzSportCar").drive(); } catch (Exception e) { e.printStackTrace(); } } } //====================================================== product car abstract class Car { private String brand; private String kind; public abstract void drive(); public String getBrand() { return brand; } public void setBrand(String brandName) { brand = brandName; } public String getKind() { return kind; } public void setKind(String kindName) { kind = kindName; } } //------------------------------------------------benz abstract class BenzCar extends Car { public BenzCar() { setBrand("Benz"); } } class BenzSportCar extends BenzCar { public BenzSportCar() { setKind("Sport"); } @Override public void drive() { System.out.println(getBrand() + "--" + getKind() + "--go-------"); } } class BenzBizCar extends BenzCar { public BenzBizCar() { setKind("Biz"); } @Override public void drive() { System.out.println(getBrand() + "--" + getKind() + "--go-------"); } } //------------------------------------------------bmw abstract class BmwCar extends Car { public BmwCar() { setBrand("Bmw"); } } class BmwSportCar extends BmwCar { public BmwSportCar() { setKind("Sport"); } @Override public void drive() { System.out.println(getBrand() + "--" + getKind() + "--go-------"); } } class BmwBizCar extends BmwCar { public BmwBizCar() { setKind("Biz"); } @Override public void drive() { System.out.println(getBrand() + "--" + getKind() + "--go-------"); } } //------------------------------------------------audi abstract class AudiCar extends Car { public AudiCar() { setBrand("Audi"); } } class AudiSportCar extends AudiCar { public AudiSportCar() { setKind("Sport"); } @Override public void drive() { System.out.println(getBrand() + "--" + getKind() + "--go-------"); } } class AudiBizCar extends AudiCar { public AudiBizCar() { setKind("Biz"); } @Override public void drive() { System.out.println(getBrand() + "--" + getKind() + "--go-------"); } } //====================================================== factory abstract class Driver { public static Car createCar(String name) throws Exception{ return (Car) Class.forName(name).newInstance(); } }