创建型模式有抽象工厂模式,建造者模式,工厂方法模式,原型模式,单例模式
1.简单工厂模式
首先我们从简单工厂模式说起。简单工厂模式,抽象工厂模式,工厂方法模式是工厂家族的三兄弟了。
简单工厂模式示意图
客户端创建一个运算元素,这个运算元素关联工厂的静态方法,也就是得到运算方法。
我们拿着这个运算方法调用就得到最后结果。
https://github.com/tori22/DesignPattern/tree/master/src/SimpleFactoryPattern
Product.java
package SimpleFactoryPattern; public class Product { public String createRealProduct() { String product = ""; return product; } }
ProductA.java
package SimpleFactoryPattern; public class ProductA extends Product { @Override public String createRealProduct() { String productA = "产品A"; return productA; } }
ProductB.java
package SimpleFactoryPattern; public class ProductB extends Product { @Override public String createRealProduct() { String productB = "产品B"; return productB; } }
ProductFactory.java
package SimpleFactoryPattern; public class ProductFactory { public static Product createProduct(ProductType type) { Product product = null; switch (type) { case PRODUCTA: product = new ProductA(); break; case PRODUCTB: product = new ProductB(); break; default: break; } return product; } }
ProductType.java
package SimpleFactoryPattern; public enum ProductType { PRODUCTA,PRODUCTB }
test.java
package SimpleFactoryPattern; public class Test { public static void main(String[] args) { Product product = ProductFactory.createProduct(ProductType.PRODUCTA); String productA = product.createRealProduct(); System.out.println(productA); product = ProductFactory.createProduct(ProductType.PRODUCTB); String productB = product.createRealProduct(); System.out.println(productB); } }
分析,我们在test类可以看到,首先我们通过工厂的静态方法得到具体的方法。
我们的运算结果,通过调用具体的类的方法,得到。
有兴趣的可以把它改写成四则运算&简单工厂。
遗憾的是,我们的简单工厂不符合开放封闭准则(对扩展开放,对修改封闭)
因为假设添加了一个新的productC,我们还需要修改switch中的分支选项。
2.抽象工厂模式
简单工厂:客户端如果想进行加法操作,首先要关联工厂,然后找到抽象工厂里面的加法类。
工厂方法:客户端如果想进行加法操作,首相要关联抽象工厂,然后找到抽象工厂里面的加法工厂,然后再找到加法类。
https://github.com/tori22/DesignPattern/tree/master/src/FactoryPattern
package FactoryPattern; public class AddFactory implements IFactory{ @Override public Operation createOperation() { return new OperationAdd(); } } package FactoryPattern; /* /简单工厂模式,产生具有共同特性的对象。 */ public class Client { public static void main(String[] args) { IFactory operFactory = new AddFactory(); Operation operation = operFactory.createOperation(); operation.setmNumberA(1); operation.setmNumberB(2); double result = operation.getResult(); System.out.println("add result: " + result); operFactory = new SubFactory(); Operation subOperation = operFactory.createOperation(); subOperation.setmNumberA(1); subOperation.setmNumberB(2); double resultSub = subOperation.getResult(); System.out.println("sub result: " + resultSub); operFactory = new MulFactory(); Operation mulOperation = operFactory.createOperation(); mulOperation.setmNumberA(1); mulOperation.setmNumberB(2); double resultMul = mulOperation.getResult(); System.out.println("mul result: " + resultMul); operFactory = new DivFactory(); Operation divOperation = operFactory.createOperation(); divOperation.setmNumberA(1); divOperation.setmNumberB(2); double resultDiv = divOperation.getResult(); System.out.println("div result: " + resultDiv); } } package FactoryPattern; public class DivFactory implements IFactory { @Override public Operation createOperation() { return new OperationDiv(); } } package FactoryPattern; public interface IFactory { public Operation createOperation(); } package FactoryPattern; public class MulFactory implements IFactory { @Override public Operation createOperation() { return new OperationMul(); } } package FactoryPattern; public class Operation { private double mNumberA = 0; private double mNumberB = 0; public double getmNumberA() { return mNumberA; } public void setmNumberA(double mNumberA) { this.mNumberA = mNumberA; } public void setmNumberB(double mNumberB) { this.mNumberB = mNumberB; } public double getmNumberB() { return mNumberB; } public double getResult() { double result = 0; return result; } } package FactoryPattern; public class OperationAdd extends Operation{ @Override public double getResult() { double result = 0; result = getmNumberA() + getmNumberB(); return result; } } package FactoryPattern; public class OperationDiv extends Operation{ @Override public double getResult() { double result = 0; result = getmNumberA() / getmNumberB(); return result; } } package FactoryPattern; public class OperationMul extends Operation { @Override public double getResult() { double result = 0; result = getmNumberA() * getmNumberB(); return result; } } package FactoryPattern; public class OperationSub extends Operation { @Override public double getResult() { double result = 0; result = getmNumberA() - getmNumberB(); return result; } } package FactoryPattern; public class SubFactory implements IFactory { @Override public Operation createOperation() { return new OperationSub(); } }
简言之,客户端关联的大工厂下面,又有小工厂,小工厂里面是具体的实现类了。
3.抽象工厂
https://github.com/tori22/DesignPattern/tree/master/src/AbstractFactoryPattern
package AbstractFactoryPattern; public class AccessFactory implements IFactory { @Override public IUser createUser() { return new AccessUser(); } } package AbstractFactoryPattern; public class AccessUser implements IUser { @Override public void insert(User user) { System.out.println("在ACCESS中给USER表增加一条记录"); } @Override public User getUser(int id) { System.out.println("在ACCESS中根据ID得到USER表一条记录"); return null; } } package AbstractFactoryPattern; public class Client { public static void main(String[] args) { User user = new User(); IFactory factory = new SqlserverFactory(); IUser iu = factory.createUser(); iu.insert(user); iu.getUser(1); } } package AbstractFactoryPattern; public interface IFactory { IUser createUser(); } package AbstractFactoryPattern; public interface IUser { void insert(User user); User getUser(int id); } package AbstractFactoryPattern; public class SqlserverFactory implements IFactory { @Override public IUser createUser() { return new SqlserverUser(); } } package AbstractFactoryPattern; public class SqlserverUser implements IUser { @Override public void insert(User user) { System.out.println("在SQL SERVER中给USER表增加一条记录"); } @Override public User getUser(int id) { System.out.println("在SQL SERVER中根据ID得到USER表一条记录"); return null; } } package AbstractFactoryPattern; public class User { private int id; private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }