工厂模式
最核心的功能就是完成对象的创建;
该模式可以避免直接使用new操作符在客户端创建对象;
找出会变化的地方,将其从不变的代码中分离出来!
工厂模式的应用主要分以下4种:
一、静态工厂
通过静态方法返回对象。静态方法可以接收参数,然后根据传入的参数决定返回的对象。
二、简单工厂
将工厂组合到客户端,由客户端调用工厂来获取对象。
* 简单工厂与静态工厂的区别:
静态工厂的行为不会改变,而简单工厂则可以再派生出子类来覆盖创建对象的行为!
三、工厂方法
将对象的创建委托给子类某个方法,父类通过顶层接口引用该方法返回的实例对象。
四、抽象工厂
创建系列产品时使用此模式;
抽象工厂是简单工厂和工厂方法结合使用的产物;
客户端组合抽象工厂与系列产品的接口,使用接口来引用抽象工厂所创建的对象。
一、静态工厂
package pattern.staticfactory; import org.hqh.util.JUtil; /** * 静态工厂 */ public class ComputerFactory { /** * 将具体实例化的过程从客户端分离出来 * 可能有很多客户端需要创建这些对象,当需要修改时,仅修改这里就可以了--->单点维护! */ public static Computer createComputer(ComputerType type) { if(type==ComputerType.ThinkPad) return new ThinkPad("TinkPad", 5000.00); if(type==ComputerType.Apple) return new Apple("Apple", 10000.00); throw new RuntimeException("Not support type:" + type.name()); } } enum ComputerType { ThinkPad,Apple; } //该类不能被实例化,只能实例化其子类 abstract class Computer { protected String brand; protected double price; @Override public String toString() { return "Computer [brand=" + brand + ", price=" + JUtil.NumberFormator.format(price) + "]"; } } class ThinkPad extends Computer { public ThinkPad(String brand, double price) { this.brand = brand; this.price = price; } } class Apple extends Computer { public Apple(String brand, double price) { this.brand = brand; this.price = price; } }
package pattern.staticfactory; public class Client { public void showCptList() { Computer thinkPad = ComputerFactory.createComputer(ComputerType.ThinkPad); System.out.println(thinkPad); Computer apple = ComputerFactory.createComputer(ComputerType.Apple); System.out.println(apple); } public static void main(String[] args) { Client c = new Client(); c.showCptList(); } }
二、简单工厂
package pattern.simplefactory; import org.hqh.util.JUtil; /** * 简单工厂 */ public class ComputerFactory { public Computer createComputer(ComputerType type) { if(type==ComputerType.ThinkPad) return new ThinkPad("TinkPad", 5000.00); if(type==ComputerType.Apple) return new Apple("Apple", 10000.00); throw new RuntimeException("Not support type:" + type.name()); } } enum ComputerType { ThinkPad,Apple; } //该类不能被实例化,只能实例化其子类 abstract class Computer { protected String brand; protected double price; @Override public String toString() { return "Computer [brand=" + brand + ", price=" + JUtil.NumberFormator.format(price) + "]"; } } class ThinkPad extends Computer { public ThinkPad(String brand, double price) { this.brand = brand; this.price = price; } } class Apple extends Computer { public Apple(String brand, double price) { this.brand = brand; this.price = price; } }
package pattern.simplefactory; public class Client { ComputerFactory factory;//组合工厂到客户端 public Client(ComputerFactory factory) { this.factory = factory; } public void showCptList() { for(ComputerType type : ComputerType.values()) { Computer computer = factory.createComputer(type); System.out.println(computer); } } public static void main(String[] args) { ComputerFactory cptFactory = new ComputerFactory(); Client c = new Client(cptFactory); c.showCptList(); } }
三、工厂方法
package pattern.factorymethod; import org.hqh.util.JUtil; public abstract class Computer { String brand; double price; @Override public String toString() { return "Computer [brand=" + brand + ", price=" + JUtil.NumberFormator.format(price) + "]"; } public void test() { System.out.println("运行测试"); } public void box() { System.out.println("装箱"); } } class ComputerType { interface CommonType {} static enum ThinkPadType implements CommonType{ E,X,T,S; } static enum AppleType implements CommonType{ Air,Pro,iMac; } } class ThinkPad extends Computer { public ThinkPad(String brand, double price) { this.brand = brand; this.price = price; } } class Apple extends Computer { public Apple(String brand, double price) { this.brand = brand; this.price = price; } }
package pattern.factorymethod; import pattern.factorymethod.ComputerType.AppleType; import pattern.factorymethod.ComputerType.ThinkPadType; public abstract class ComputerStore { public Computer order(ComputerType.CommonType type) { Computer computer = assemble(type); computer.test(); computer.box(); return computer; } //工厂方法,将对象的创建延迟到子类中进行 abstract Computer assemble(ComputerType.CommonType type); } class ThinkPadStore extends ComputerStore { @Override Computer assemble(ComputerType.CommonType type) { if(!(type instanceof ThinkPadType)) throw new RuntimeException("ThinkPadStore not support type: " + type); return new ThinkPad(((ThinkPadType)type).name(), 5000.00); } } class AppleStore extends ComputerStore { @Override Computer assemble(ComputerType.CommonType type) { if(!(type instanceof AppleType)) throw new RuntimeException("AppleStore not support type: " + type); return new ThinkPad(((AppleType)type).name(), 15000.00); } }
package pattern.factorymethod; public class Client { public static void main(String[] args) { ComputerStore thinkPadStore = new ThinkPadStore(); Computer thinkPad = thinkPadStore.order(ComputerType.ThinkPadType.E); System.out.println(thinkPad); ComputerStore appleStore = new AppleStore(); Computer apple = appleStore.order(ComputerType.AppleType.Air); System.out.println(apple); } }
四、抽象工厂
package pattern.abstractmethod; public abstract interface MaterialFactory { CPU createCPU(); GraphicCard createGraphicCard(); } class ThinkPadMaterialFacotry implements MaterialFactory { public CPU createCPU() { return new AMD(); } public GraphicCard createGraphicCard() { return new ATI(); } } class AppleMaterialFacotry implements MaterialFactory { public CPU createCPU() { return new Intel(); } public GraphicCard createGraphicCard() { return new NVIDIA(); } }
package pattern.abstractmethod; public abstract class CPU { } class Intel extends CPU { @Override public String toString() { return "Intel"; } } class AMD extends CPU { @Override public String toString() { return "AMD"; } }
package pattern.abstractmethod; public abstract class GraphicCard { } class NVIDIA extends GraphicCard { @Override public String toString() { return "NVIDIA"; } } class ATI extends GraphicCard { @Override public String toString() { return "ATI"; } }
package pattern.abstractmethod; import pattern.abstractmethod.ComputerType.AppleType; import pattern.abstractmethod.ComputerType.ThinkPadType; public abstract class ComputerStore { public Computer order(ComputerType.CommonType type) { Computer computer = create(type);//工厂方法 computer.assemble(); computer.test(); computer.box(); return computer; } //工厂方法,将对象的创建延迟到子类中进行 abstract Computer create(ComputerType.CommonType type); } class ThinkPadStore extends ComputerStore { @Override Computer create(ComputerType.CommonType type) { if(!(type instanceof ThinkPadType)) throw new RuntimeException("ThinkPadStore not support type: " + type); return new ThinkPad(((ThinkPadType)type).name(), 5000.00, new ThinkPadMaterialFacotry());//传入工厂 } } class AppleStore extends ComputerStore { @Override Computer create(ComputerType.CommonType type) { if(!(type instanceof AppleType)) throw new RuntimeException("AppleStore not support type: " + type); return new ThinkPad(((AppleType)type).name(), 15000.00, new AppleMaterialFacotry());//传入工厂 } }
package pattern.abstractmethod; import org.hqh.util.JUtil; public abstract class Computer { String brand; double price; CPU cpu; GraphicCard graphicCard; public void test() { System.out.println("运行测试"); } public void box() { System.out.println("装箱"); } public abstract void assemble(); @Override public String toString() { return "Computer [brand=" + brand + ", price=" + JUtil.NumberFormator.format(price) + ", cpu=" + cpu + ", graphicCard=" + graphicCard + "]"; } } class ComputerType { interface CommonType {} static enum ThinkPadType implements CommonType{ E,X,T,S; } static enum AppleType implements CommonType{ Air,Pro,iMac; } } class ThinkPad extends Computer { MaterialFactory factory; public ThinkPad(String brand, double price, MaterialFactory factory) { this.brand = brand; this.price = price; this.factory = factory; } @Override public void assemble() { this.cpu = factory.createCPU();//通过工厂创建原料 this.graphicCard = factory.createGraphicCard();//通过工厂创建原料 } } class Apple extends Computer { MaterialFactory factory; public Apple(String brand, double price, MaterialFactory factory) { this.brand = brand; this.price = price; this.factory = factory; } @Override public void assemble() { this.cpu = factory.createCPU();//通过工厂创建原料 this.graphicCard = factory.createGraphicCard();//通过工厂创建原料 } }
package pattern.abstractmethod; public class Client { public static void main(String[] args) { ComputerStore thinkPadStore = new ThinkPadStore(); Computer thinkPad = thinkPadStore.order(ComputerType.ThinkPadType.E); System.out.println(thinkPad); ComputerStore appleStore = new AppleStore(); Computer apple = appleStore.order(ComputerType.AppleType.Air); System.out.println(apple); } }