使用场景及解决问题
创建对象的业务发生改变时,所有包含该业务的方法都要去维护代码
工厂模式本质是 封装了创建对象这一行为,统一管理减少分散式修改
工厂模式的参与者是 创建者(工厂) 和 实际对象(产品)
简单工厂模式
客户端无需知晓和维护对象创建的具体信息,只需根据约定的请求参数来向工厂类获取对应的对象
public class Client {
public static void main(String[] args) {
Product product = SimpleFactory.getBean(RequestString.PRODUCT_TYPE_A);
product.processProduct();
}
}
对象的创建任务由工厂承担和维护,根据约定的参数生成对应对象
public class SimpleFactory { public static Product getBean(String type) { if (RequestString.PRODUCT_TYPE_A.equals(type)) { return new ProductA(); } else if (RequestString.PRODUCT_TYPE_B.equals(type)) { return new ProductB(); } else return null; } } public class RequestString { public static String PRODUCT_TYPE_A = "A"; public static String PRODUCT_TYPE_B = "B"; }
工厂生产的产品
public abstract class Product { public abstract void processProduct(); public Product() {} } public class ProductA extends Product{ @Override public void processProduct() { System.out.println("process ProductA method"); } } public class ProductB extends Product{ @Override public void processProduct() { System.out.println("process ProductB method"); } }
简单工厂优点
简单易实现,需求变更易于维护:
已有创建需求替换时: 原引用处无需更改,仍按规定字符串获取分支对象,仅需更改简单工厂创建方法中的对应分支
新的创建需求或过期的需求: 增删种类及对应的分支
简单工厂缺点
承担的创建任务过多时分支庞杂不易维护
工厂模式
工厂模式处理的是工厂和产品之间的关系
public abstract class Factory { public Product getProduct(String type) { Product product = factoryMethod(type); product.processProduct(); return product; } public abstract Product factoryMethod (String type) {} } public class ProductAFactory extends Factory { @Override public Product factoryMethod (String type) { if (RequestString.PRODUCT_TYPE_ONE.equals(type)) { return new ProductA1(); } else if (RequestString.PRODUCT_TYPE_TWO.equals(type)) { return new ProductA2(); } else return null; } } public class ProductBFactory extends Factory { @Override public Product factoryMethod (String type) { if (RequestString.PRODUCT_TYPE_ONE.equals(type)) { return new ProductB1(); } else if (RequestString.PRODUCT_TYPE_TWO.equals(type)) { return new ProductB2(); } else return null; } } public abstract class Product { public abstract void processProduct(); public Product() {} } public class ProductA1 extends Product{ @Override public void processProduct() { System.out.println("process A1"); } } public class ProductA2 extends Product{ @Override public void processProduct() { System.out.println("process A2"); } } public class ProductB1 extends Product{ @Override public void processProduct() { System.out.println("process B1"); } } public class ProductB2 extends Product{ @Override public void processProduct() { System.out.println("process B2"); } } public class RequestString { public static String PRODUCT_TYPE_ONE = "1"; public static String PRODUCT_TYPE_TWO = "2"; } public class Client { public static void main(String[] args) { Factory factory = new ProductBFactory(); Product product = factory.getProduct(RequestString.PRODUCT_TYPE_ONE); } }
抽象工厂模式
一个工厂生产一种产品的各个组件
public interface Factory { public ComponentOne factoryMethodPartOne(); public ComponentTwo factoryMethodPartTwo(); } public class ProductAFactory implements Factory { @Override public ComponentOne factoryMethodPartOne() { return new AComponentOne(); } @Override public ComponentTwo factoryMethodPartTwo() { return new AComponentTwo(); } } public class ProductBFactory implements Factory { @Override public ComponentOne factoryMethodPartOne() { return new BComponentOne(); } @Override public ComponentTwo factoryMethodPartTwo() { return new BComponentTwo(); } } public abstract class Product { ComponentOne c1; ComponentTwo c2; public abstract void processProduct(); public Product(Factory factory) { c1 = factory.factoryMethodPartOne(); c2 = factory.factoryMethodPartTwo(); } } public class ProductA extends Product{ @Override public void processProduct() { System.out.println("process A1"); } } public class ProductB extends Product{ @Override public void processProduct() { System.out.println("process A2"); } } public class Client { public static void main(String[] args) { Factory factory = new ProductAFactory(); Product product = new ProductA(factory); product.processProduct(); } }