package com.macrotea.pattern.factory.abstr; import com.macrotea.pattern.factory.base.Food; import com.macrotea.pattern.factory.base.Vehicle; import com.macrotea.pattern.factory.base.Weapon; public abstract class AbstractFactory { public abstract Food createFood(); public abstract Vehicle createVehicle(); public abstract Weapon createWeapon(); }
package com.macrotea.pattern.factory.ext; import com.macrotea.pattern.factory.abstr.AbstractFactory; import com.macrotea.pattern.factory.base.Food; import com.macrotea.pattern.factory.base.Vehicle; import com.macrotea.pattern.factory.base.Weapon; import com.macrotea.pattern.factory.sub.DongfengCar; import com.macrotea.pattern.factory.sub.LongjingTea; import com.macrotea.pattern.factory.sub.XiaolongPlane; public class ChinaFactory extends AbstractFactory{ @Override public Food createFood() { return new LongjingTea(); } @Override public Vehicle createVehicle() { return new DongfengCar(); } @Override public Weapon createWeapon() { return new XiaolongPlane(); } }
package com.macrotea.pattern.factory.ext; import com.macrotea.pattern.factory.abstr.AbstractFactory; import com.macrotea.pattern.factory.base.Food; import com.macrotea.pattern.factory.base.Vehicle; import com.macrotea.pattern.factory.base.Weapon; import com.macrotea.pattern.factory.sub.Car; import com.macrotea.pattern.factory.sub.Gun; import com.macrotea.pattern.factory.sub.Nut; public class DefaultFactory extends AbstractFactory{ @Override public Food createFood() { return new Nut(); } @Override public Vehicle createVehicle() { return new Car(); } @Override public Weapon createWeapon() { return new Gun(); } }
package com.macrotea.pattern.factory.base; public abstract class Food { public abstract void eat(); }
package com.macrotea.pattern.factory.base; public abstract class Vehicle { public abstract void run(); }
package com.macrotea.pattern.factory.base; public abstract class Weapon { public abstract void shoot(); }
package com.macrotea.pattern.factory.sub; import com.macrotea.pattern.factory.base.Vehicle; public class Car extends Vehicle{ @Override public void run() { System.out.println("Car-run()"); } }
package com.macrotea.pattern.factory.sub; import com.macrotea.pattern.factory.base.Vehicle; public class DongfengCar extends Vehicle { @Override public void run() { System.out.println("东风牌货车开动..."); } }
package com.macrotea.pattern.factory.sub; import com.macrotea.pattern.factory.base.Weapon; public class Gun extends Weapon{ @Override public void shoot() { System.out.println("Gun-shoot()"); } }
package com.macrotea.pattern.factory.sub; import com.macrotea.pattern.factory.base.Food; public class LongjingTea extends Food { @Override public void eat() { System.out.println("来杯龙井..."); } }
package com.macrotea.pattern.factory.sub; import com.macrotea.pattern.factory.base.Food; public class Nut extends Food{ @Override public void eat() { System.out.println("Nut-eat()"); } }
package com.macrotea.pattern.factory.sub; import com.macrotea.pattern.factory.base.Weapon; public class XiaolongPlane extends Weapon { @Override public void shoot() { System.out.println("枭龙歼灭机射击..."); } }
package com.macrotea.pattern.factory.test; import com.macrotea.pattern.factory.abstr.AbstractFactory; import com.macrotea.pattern.factory.base.Food; import com.macrotea.pattern.factory.base.Vehicle; import com.macrotea.pattern.factory.base.Weapon; import com.macrotea.pattern.factory.ext.DefaultFactory; public class Mainer { public static void main(String[] args) { /*默认工厂*/ AbstractFactory defFactory=new DefaultFactory(); //食物 Food food=defFactory.createFood(); food.eat(); //交通工具 Vehicle vehicle=defFactory.createVehicle(); vehicle.run(); //武器 Weapon weapon=defFactory.createWeapon(); weapon.shoot(); /* 中国工厂 AbstractFactory cnFactory=new ChinaFactory(); //食物 Food food=cnFactory.createFood(); food.eat(); //交通工具 Vehicle vehicle=cnFactory.createVehicle(); vehicle.run(); //武器 Weapon weapon=cnFactory.createWeapon(); weapon.shoot(); */ } }
抽象工厂方便控制产品系列
普通工厂方便控制产品种类