简单工厂模式
简单介绍:客户端传入具体的选择条件,有工厂创建具体的类的实例,也就是分离了客户端与具体的实现类;
缺点:如果增添一个实现类,需要更改对应的工厂里面的条件分支,也就是修改原有工厂类。
目录结构
代码实现
client
package controller; import service.Operation; import service.OperationFactory; /** * @author SHshuo * @data 2021/10/16--8:41 * 简单工厂模式、由工厂创建里面具体的对象 */ public class Client { public static void main(String[] args) { Operation oper = OperationFactory.createOperate("+"); oper.setNumberA(1); oper.setNumberB(2); System.out.println(oper.getResult()); } }工厂类
package service; import service.Impl.OperationAdd; import service.Impl.OperationDiv; import service.Impl.OperationMul; import service.Impl.OperationSub; /** * @author SHshuo * @data 2021/10/16--8:35 */ public class OperationFactory { public static Operation createOperate(String operate){ Operation oper = null; switch (operate){ case "+": oper = new OperationAdd(); break; case "-": oper = new OperationSub(); break; case "*": oper = new OperationMul(); break; case "/": oper = new OperationDiv(); break; } return oper; } }具体的实现类
package service.Impl; import service.Operation; /** * @author SHshuo * @data 2022/4/9--10:35 */ public class OperationAdd extends Operation { @Override public double getResult() { return numberA + numberB; } }
工厂方法模式
概念:定义一个用于创建对象的接口,让子类决定实例化哪一个类。工厂方法使一个类的实例化延迟到子类。
简单来说:就是用工厂来创建对象,每个具体实现类都有一个对应的工厂,多个工厂共同继承抽象工厂;所以在增添实现类的时候,只需要增加工厂和对应的实现类即可
代码实现
目录结构
具体代码
Client类、面向的接口、传递的是具体的工厂来创建对象
package controller; import service.Impl.LeiFeng; import service.LeiFengFactory; import service.UndergraduateFactory; import service.VolunteerFactory; /** * @author SHshuo * @data 2021/10/16--8:41 * 工厂方法模式、用工厂创建对象 */ public class Client { public static void main(String[] args) { LeiFengFactory leifengFactory; // 创建的是大学生学习雷锋工厂的对象 leifengFactory = new UndergraduateFactory(); // 创建的是志愿者学习雷锋工厂的对象 // leifengFactory = new VolunteerFactory(); // 用工厂来创建对象 LeiFeng student = leifengFactory.createLeiFeng(); student.buyRice(); student.sweep(); student.wash(); } }
LeiFeng、LeiFeng工厂是父类接口、父类
package service; import service.Impl.LeiFeng; /** * @author SHshuo * @data 2021/10/16--8:35 * 雷锋工厂 */ public interface LeiFengFactory { LeiFeng createLeiFeng(); }
package service.Impl; /** * @author SHshuo * @data 2021/10/16--8:42 * 父类 */ public class LeiFeng { public void wash(){ System.out.println("洗衣"); } public void sweep(){ System.out.println("扫地"); } public void buyRice(){ System.out.println("买米"); } }
package service; import service.Impl.LeiFeng; import service.Impl.Undergraduate; /** * @author SHshuo * @data 2021/10/16--11:02 * 大学生学习雷锋工厂 */ public class UndergraduateFactory implements LeiFengFactory { @Override public LeiFeng createLeiFeng() { return new Undergraduate(); } }
package service.Impl; /** * @author SHshuo * @data 2021/10/16--8:36 * 继承雷锋的类 */ public class Undergraduate extends LeiFeng { }
UML类图
引入策略模式、把原来的具体策略换成具体的工厂即实现,工厂方法模式一个具体的工厂类对应一个具体的对象
添加Factory.class
package service; import service.Impl.LeiFeng; /** * @author SHshuo * @data 2021/10/16--14:44 */ public class Factory { private LeiFengFactory leiFengFactory; public Factory(LeiFengFactory leiFengFactory){ this.leiFengFactory = leiFengFactory; } public LeiFeng createLeiFeng(){ return leiFengFactory.createLeiFeng(); } }
package controller; import service.Factory; import service.Impl.LeiFeng; import service.LeiFengFactory; import service.UndergraduateFactory; import service.VolunteerFactory; /** * @author SHshuo * @data 2021/10/16--8:41 * 工厂方法模式、用工厂创建对象 */ public class Client { public static void main(String[] args) { // 引入策略模式的概念、把工厂当成具体的策略 Factory leifengFactory = new Factory(new UndergraduateFactory()); // 用工厂来创建对象 LeiFeng student = leifengFactory.createLeiFeng(); student.buyRice(); student.sweep(); student.wash(); } }