工厂模式分为三大类
一、简单工厂
组成如下:
(1) 工厂类角色:这是本模式的核心,含有一定的商业逻辑和判断逻辑。在java中它往往由一个具体类实现。
具体例子:
抽象产品、具体产品角色类定义如下:
package com.open.design.factory; public class SimpleFactoryData { //2.抽象产品角色:它一般是具体产品继承的父类或者实现的接口。在java中由接口或者抽象类来实现。 public interface Car { void printName(); } //3.具体产品角色:工厂类所创建的对象就是此角色的实例。在java中由一个具体类实现。 public static class Benz implements Car { @Override public void printName() { System.out.println("i am Benz."); } } public static class BMW implements Car { @Override public void printName() { System.out.println("i am BMW."); } } public static class Audi implements Car { @Override public void printName() { System.out.println("i am Audi."); } } public static class Tesla implements Car { @Override public void printName() { System.out.println("i am Tesla."); } } }
package com.open.design.factory; import com.open.design.factory.SimpleFactoryData.Car; //1.工厂类角色:这是本模式的核心,含有一定的商业逻辑和判断逻辑。在java中它往往由一个具体类实现 public class SimpleFactory { public static Car produce(String carName) { Car car=null; if("Benz".equals(carName)) { car=new SimpleFactoryData.Benz(); } else if("BMW".equals(carName)) { car=new SimpleFactoryData.BMW(); } else if("Audi".equals(carName)) { car=new SimpleFactoryData.Audi(); } else if("Tesla".equals(carName)) { car=new SimpleFactoryData.Tesla(); } return car; } }
package com.open.design.factory; import com.open.design.factory.SimpleFactoryData.Car; public class SimpleFactoryTest { /** * @param args */ public static void main(String[] args) { Car car=SimpleFactory.produce("Benz"); car.printName(); car=SimpleFactory.produce("BMW"); car.printName(); car=SimpleFactory.produce("Audi"); car.printName(); car=SimpleFactory.produce("Tesla"); car.printName(); } }
优点与缺点:
二、工厂方法模式(也称多态工厂)
组成如下:
具体例子如下:
抽象产品、具体产品角色类定义如下:
package com.open.design.factory; public class PolymorphicData { //3.抽象产品角色:它是具体产品继承的父类或者是实现的接口。在java中一般有抽象类或者接口来实现。 public interface Car { void printName(); } //4.具体产品角色:具体工厂角色所创建的对象就是此角色的实例。在java中由具体的类来实现。 public static class Sedan implements Car { @Override public void printName() { System.out.println("i am Sedan."); } } public static class Train implements Car { @Override public void printName() { System.out.println("i am Train."); } } public static class Bike implements Car { @Override public void printName() { System.out.println("i am Bike."); } } }
抽象工厂、具体工厂类定义如下:
package com.open.design.factory; import com.open.design.factory.PolymorphicData.Car; public class PolymorphicFactory { //1.抽象工厂角色: 这是工厂方法模式的核心,它与应用程序无关。是具体工厂角色必须实现的接口或者必须继承的父类。在java中它由抽象类或者接口来实现。 public static abstract class CarFactory { abstract Car createCar(); } //2.具体工厂角色:它含有和具体业务逻辑有关的代码。由应用程序调用以创建对应的具体产品的对象。 public static class SedanFactory extends CarFactory { public Car createCar() { return new PolymorphicData.Sedan(); }; } public static class TrainFactory extends CarFactory { public Car createCar() { return new PolymorphicData.Train(); }; } public static class BikeFactory extends CarFactory { public Car createCar() { return new PolymorphicData.Bike(); }; } }
package com.open.design.factory; import com.open.design.factory.PolymorphicData.Car; import com.open.design.factory.PolymorphicFactory.BikeFactory; import com.open.design.factory.PolymorphicFactory.CarFactory; import com.open.design.factory.PolymorphicFactory.SedanFactory; import com.open.design.factory.PolymorphicFactory.TrainFactory; public class PolymorphicTest { /** * @param args */ public static void main(String[] args) { CarFactory factory=new SedanFactory(); Car car =factory.createCar(); car.printName(); factory=new TrainFactory(); car =factory.createCar(); car.printName(); factory=new BikeFactory(); car =factory.createCar(); car.printName(); } }
优点与缺点:
三、抽象工厂
组成与工厂方法模式一致,只不过抽象工厂是针对多个产品等级结构(即产品簇),工厂方法是针对一个产品等级结构。
具体例子如下:
抽象产品、具体产品角色类定义如下:
package com.open.design.factory; public class AbstractFactryCar { //2.抽象产品角色:它一般是具体产品继承的父类或者实现的接口。在java中由接口或者抽象类来实现。 public interface Car { void printName(); } //3.具体产品角色:工厂类所创建的对象就是此角色的实例。在java中由一个具体类实现。 public static class Benz implements Car { @Override public void printName() { System.out.println("i am Benz."); } } public static class BMW implements Car { @Override public void printName() { System.out.println("i am BMW."); } } public static class Audi implements Car { @Override public void printName() { System.out.println("i am Audi."); } } public static class Tesla implements Car { @Override public void printName() { System.out.println("i am Tesla."); } } }
package com.open.design.factory; public class AbstractFactryHouse { //2.抽象产品角色:它一般是具体产品继承的父类或者实现的接口。在java中由接口或者抽象类来实现。 public interface House { void printHouseName(); } //3.具体产品角色:工厂类所创建的对象就是此角色的实例。在java中由一个具体类实现。 public static class Villa implements House { @Override public void printHouseName() { System.out.println("i am Villa."); } } public static class TileRoofedHouse implements House { @Override public void printHouseName() { System.out.println("i am TileRoofedHouse."); } } public static class CommodityHouse implements House { @Override public void printHouseName() { System.out.println("i am CommodityHouse."); } } }
package com.open.design.factory; import com.open.design.factory.AbstractFactryCar.Car; import com.open.design.factory.AbstractFactryHouse.House; public class AbstractFactry { //抽象工厂类 public static abstract class AbsDream { public abstract Car createCar(); public abstract House createHouse(); } //2.具体工厂角色:它含有和具体业务逻辑有关的代码。由应用程序调用以创建对应的具体产品的对象。 public static class DreamAFactory extends AbsDream { public Car createCar() { return new AbstractFactryCar.Benz(); }; public House createHouse() { return new AbstractFactryHouse.TileRoofedHouse(); }; } public static class DreamBFactory extends AbsDream { public Car createCar() { return new AbstractFactryCar.Audi(); }; public House createHouse() { return new AbstractFactryHouse.CommodityHouse(); }; } public static class DreamCFactory extends AbsDream { public Car createCar() { return new AbstractFactryCar.Tesla(); }; public House createHouse() { return new AbstractFactryHouse.Villa(); }; } }
package com.open.design.factory; import com.open.design.factory.AbstractFactry.AbsDream; import com.open.design.factory.AbstractFactryCar.Car; import com.open.design.factory.AbstractFactryHouse.House; public class AbstractFactryTest { /** * @param args */ public static void main(String[] args) { AbsDream dream = new AbstractFactry.DreamAFactory(); Car car =dream.createCar(); House house=dream.createHouse(); car.printName(); house.printHouseName(); //------------------- dream = new AbstractFactry.DreamBFactory(); car =dream.createCar(); house=dream.createHouse(); car.printName(); house.printHouseName(); //------------------- dream = new AbstractFactry.DreamCFactory(); car =dream.createCar(); house=dream.createHouse(); car.printName(); house.printHouseName(); } }
优点与缺点:
四、动态工厂
组成与简单工厂类似。
具体例子:
抽象产品、具体产品角色类定义如下:
package com.open.design.factory; public class DynamicFactoryData { //2.抽象产品角色:它一般是具体产品继承的父类或者实现的接口。在java中由接口或者抽象类来实现。 public interface Car { void printName(); } //3.具体产品角色:工厂类所创建的对象就是此角色的实例。在java中由一个具体类实现。 public static class Benz implements Car { @Override public void printName() { System.out.println("i am Benz."); } } public static class BMW implements Car { @Override public void printName() { System.out.println("i am BMW."); } } public static class Audi implements Car { @Override public void printName() { System.out.println("i am Audi."); } } public static class Tesla implements Car { @Override public void printName() { System.out.println("i am Tesla."); } } }
package com.open.design.factory; import java.lang.reflect.Constructor; import com.open.design.factory.DynamicFactoryData.Car; public class DynamicFactory { public static Car produce(String carName) { ClassLoader mClassLoader=DynamicFactory.class.getClassLoader(); try { Class<?> mClass = mClassLoader.loadClass(carName); Constructor<?>[] mConstructor=mClass.getConstructors(); return (Car) mConstructor[0].newInstance(); } catch (Exception e) { e.printStackTrace(); } return null; } }
package com.open.design.factory; import com.open.design.factory.DynamicFactoryData.Car; public class DynamicFactoryTest { /** * @param args */ public static void main(String[] args) { Car car=DynamicFactory.produce("com.open.design.factory.DynamicFactoryData$Benz"); car.printName(); car=DynamicFactory.produce("com.open.design.factory.DynamicFactoryData$BMW"); car.printName(); car=DynamicFactory.produce("com.open.design.factory.DynamicFactoryData$Audi"); car.printName(); car=DynamicFactory.produce("com.open.design.factory.DynamicFactoryData$Tesla"); car.printName(); } }
优点与缺点:
本文代码地址:https://github.com/zz7zz7zz/design-pattern
参考博文:http://www.cnblogs.com/forlina/archive/2011/06/21/2086114.html
原文:http://blog.csdn.net/zz7zz7zz/article/details/31809481