package com.lcq.factory; public interface Moveable { public void run(); }
然后我们创建具体的实现类:
package com.lcq.factory; public class Plane implements Moveable{ @Override public void run() { System.out.println("扇着翅膀飞奔而来。。。"); } }
接下来我们将Plane类创建对象的过程进行封装,实现工厂。在实现工厂类之前,我们可以对工厂类进行抽象,创建抽象工厂类:
package com.lcq.factory; public abstract class VehicleFactory { public abstract Moveable create(); }
在具体的工厂类中继承抽象的工厂类:
package com.lcq.factory; public class PlaneFactory extends VehicleFactory{ @Override public Moveable create() { return new Plane(); } }
这样客户就可以使用工厂进行对象的创建和使用了:
package com.lcq.factory; public class Test2 { public static void main(String[] args) { VehicleFactory factory = new PlaneFactory(); // VehicleFactory factory = new BroomFactory(); Moveable m = factory.create(); m.run(); } }
假如我们想提供另外的交通工具类就会非常灵活。比如我们想将交通工具换成扫帚,则可以创建Broom对象:
package com.lcq.factory; public class Broom implements Moveable{ @Override public void run() { System.out.println("刮着沙尘暴呼啸而来。。。"); } }
实现它的工厂生产类:
package com.lcq.factory; public class BroomFactory extends VehicleFactory{ @Override public Moveable create() { return new Broom(); } }
在客户类中替换掉工厂就可以了:
VehicleFactory factory = new BroomFactory();
所以简单工厂模式对于创建新的产品是比较方便灵活的;
简单工厂类图:
二.工厂方法
简单的模拟一下工厂方法:
interface IProduct { public void productMethod(); } class Product implements IProduct { public void productMethod() { System.out.println("产品"); } } interface IFactory { public IProduct createProduct(); } class Factory implements IFactory { public IProduct createProduct() { return new Product(); } } public class Client { public static void main(String[] args) { IFactory factory = new Factory(); IProduct prodect = factory.createProduct(); prodect.productMethod(); } }
三·抽象工厂实现
我们的需求是客户需要一系列的产品,比如这些产品包括:交通工具,武器,食物。
我们首先创建这些产品:
交通工具:
package com.lcq.factory.abstractfactory; public abstract class Vehicle { public abstract void run(); }
具体的交通工具Car:
package com.lcq.factory.abstractfactory; /** * 单例模式和工厂方法模式 * @author lcq * */ public class Car extends Vehicle{ @Override public void run() { System.out.println("开车去东北。。。"); } }
武器:
package com.lcq.factory.abstractfactory; public abstract class Weapon { public abstract void shoot(); }
具体的武器实现:
package com.lcq.factory.abstractfactory; public class AK47 extends Weapon { @Override public void shoot() { System.out.println("da da da..."); } }
食物:
package com.lcq.factory.abstractfactory; public abstract class Food { public abstract void print(); }
具体的食物:
package com.lcq.factory.abstractfactory; public class Apple extends Food { @Override public void print() { System.out.println("eat apple"); } }
接下来定义我们的抽象工厂类,在该类中我们定义抽象的创建交通工具和武器以及食品的抽象方法:
package com.lcq.factory.abstractfactory; public abstract class AbstractFactory { public abstract Vehicle createVehicle(); public abstract Weapon createWeapon(); public abstract Food createFood(); }
定义具体生产产品的工厂类:
package com.lcq.factory.abstractfactory; public class DefaultFactory extends AbstractFactory { @Override public Vehicle createVehicle() { return new Car(); } @Override public Weapon createWeapon() { return new AK47(); } @Override public Food createFood() { return new Apple(); } }
这样客户就可以利用工厂类去使用系列产品了:
package com.lcq.factory.abstractfactory; /** * 测试抽象工厂 * @author lcq * */ public class test { public static void main(String[] args) { AbstractFactory factory = new DefaultFactory(); Vehicle vehicle = factory.createVehicle(); vehicle.run(); Weapon weapon = factory.createWeapon(); weapon.shoot(); Food food = factory.createFood(); food.print(); } }
以上是实现了一个系列的产品,如果想改成其他系列的产品,可以比较方便的实现:
创建另外系列的产品:
交通工具:
package com.lcq.factory.abstractfactory; public class Broom extends Vehicle { @Override public void run() { System.out.println("冒着沙尘暴一路飞奔而来。。。"); } }
武器:
package com.lcq.factory.abstractfactory; public class MagicStick extends Weapon { @Override public void shoot() { System.out.println("fire hu hu hu..."); } }
食品:
package com.lcq.factory.abstractfactory; public class MushRoom extends Food { @Override public void print() { System.out.println("mushroom"); } }
创建好对应的工厂类:
package com.lcq.factory.abstractfactory; public class MagicFactory extends AbstractFactory{ @Override public Vehicle createVehicle() { return new Broom(); } @Override public Weapon createWeapon() { return new MagicStick(); } @Override public Food createFood() { return new MushRoom(); } }
在客户类中修改即可更换产品系列:
AbstractFactory factory = new MagicFactory();
抽象工厂类图:
三·模拟spring实现工厂模式
基本思路是结合配置文件实现工厂模式。
和先前的流程相似,创建Moveable接口和其实现类Car:
package com.spring.factory; public interface Moveable { public void run(); }
package com.spring.factory; /** * 单例模式和工厂方法模式 * @author lcq * */ public class Car implements Moveable{ @Override public void run() { System.out.println("开车去东北。。。"); } }
再添加一个实现类Train;
package com.spring.factory; public class Train implements Moveable { @Override public void run() { System.out.println("开着火车冒着烟。。。"); } }
创建配置文件spring.properties:
vehicleType=com.spring.factory.Train
在客户类中使用工厂:
package com.spring.factory; import java.util.Properties; public class Test { public static void main(String[] args) throws Exception { Properties properties = new Properties(); properties.load(Test.class.getClassLoader().getResourceAsStream( "com/spring/factory/spring.properties")); String vehicleTypeName = properties.getProperty("vehicleType"); Object o = Class.forName(vehicleTypeName).newInstance(); Moveable m = (Moveable) o; m.run(); } }
这样在更改产品时可以直接修改配置文件,比如:
vehicleType=com.spring.factory.Car
即可。