java抽象工厂实战与总结

文章目录

  • 一、工厂模式(三种)
    • 1.简单工厂模式
      • 1.1 概念:
      • 1.2 `使用场景`:
      • 1.3 模型图解:
      • 1.4 伪代码:
    • 2.工厂方法模式
      • 2.1 概念:
      • 2.2 `使用场景`:
      • 2.3 模型图解:
      • 2.4 伪代码
    • 3.抽象工厂模式
      • 3.1 概念
      • 3.2 `使用场景`
      • 3.3 模型图解:以两层模型做例子
      • 3.4 伪代码
      • 3.4.1 以品牌角度设计工厂
      • 3.5 实战:抽象工厂+策略模式
        • 3.5.1 业务方法调用
        • 3.5.2 抽象工厂创建二级工厂,定义获取业务对象接口(二级工厂去创建具体的业务对象)
        • 3.5.3 二级工厂 获得对象
        • 3.5.3 二级工厂 具体的业务对象
        • 3.5.3 处理的业务
    • 补充:6大设计原则
    • 3.6 总结:


一、工厂模式(三种)

1.简单工厂模式

1.1 概念:

通过一个工厂类来创建对象,根据不同的参数或条件返回相应的对象实例。
隐藏了对象的创建细节,客户端只需通过工厂类获取对象而不需要直接实例化对象。

1.2 使用场景

使用者:根据不同的参数或条件返回相应的对象实例

1.3 模型图解:

角色:手机总工厂、参数、手机、小米手机、华为手机
后续补充:

1.4 伪代码:

public class SimpleFactoryTest {static class SimpleFactory {static SimpleFactoryTest.Mobile createMobile(int mobileType) throws Exception {if (mobileType == 1) {return new SimpleFactoryTest.HuaWei();} else if (mobileType == 2) {return new SimpleFactoryTest.XiaoMi();} else {throw new Exception("手机类型不存在!!!");}}}public static class Mobile {protected void call() {System.out.println("mobile...");}}public static class HuaWei extends Mobile {public void call() {System.out.println("huaWei...");}}public static class XiaoMi extends Mobile {public void call() {System.out.println("xiaomi...");}}public static void main(String[] args) throws Exception {SimpleFactoryTest.Mobile mobile = SimpleFactory.createMobile(1);mobile.call();}
}

2.工厂方法模式

2.1 概念:

定义了一个创建对象的接口,具体对象的创建由对象工厂决定。
使得对象的创建延迟到子类工厂,从而实现了对扩展开放、对修改关闭的原则。

2.2 使用场景

使用者:根据不同的子工厂创建相应的对象实例

2.3 模型图解:

角色:手机总工厂、小米手机工厂、华为手机工厂、手机、小米手机、华为手机
后续补充

2.4 伪代码

public class FactoryMethodTest {interface FactoryMethod {Mobile createMobile();}static class HuaweiFactory implements FactoryMethod {@Overridepublic Mobile createMobile() {return new Huawei();}}static class XiaomiFactory implements FactoryMethod {@Overridepublic Mobile createMobile() {return new Xiaomi();}}static class Mobile {protected void call() {System.out.println("mobile...");}}static class Huawei extends Mobile {@Overridepublic void call() {System.out.println("huawei...");}}static class Xiaomi extends Mobile {@Overridepublic void call() {System.out.println("xiaomi...");}}public static void main(String[] args) {final HuaweiFactory huaweiFactory = new HuaweiFactory();final Mobile mobile = huaweiFactory.createMobile();mobile.call();}
}

3.抽象工厂模式

3.1 概念

抽象工厂模式提供一个接口,用于创建一系列相关或相互依赖的对象。
通过使用抽象工厂及其产品接口来创建对象,从而将客户端与具体的产品实现解耦。

3.2 使用场景

适用:三层关系

产品品牌子品牌
手机华为手机mate60手机
手机小米手机红米手机

适用:两层关系

产品品牌
手机华为
电脑小米

3.3 模型图解:以两层模型做例子

角色::总工厂、小米工厂、华为工厂、小米手机、华为手机、小米路由器、华为路由器

3.4 伪代码

3.4.1 以品牌角度设计工厂


/*** descr: 以品牌角度设计工厂** @date: 2024/1/24**/
public interface AbstractFactory {Phone createPhone();Computer createComputer();class HuaweiFactory implements AbstractFactory {@Overridepublic Phone createPhone() {return new HuaweiPhone();}@Overridepublic Computer createComputer() {return new HuaweiComputer();}}class XiaomiFactory implements AbstractFactory {@Overridepublic Phone createPhone() {return new XiaomiPhone();}@Overridepublic Computer createComputer() {return new XiaomiComputer();}}class HuaweiPhone implements Phone {@Overridepublic void call() {System.out.println("huawei call...");}}class HuaweiComputer implements Computer {@Overridepublic void play() {System.out.println("huawei play...");}}class XiaomiPhone implements Phone {@Overridepublic void call() {System.out.println("xiaomi play...");}}class XiaomiComputer implements Computer {@Overridepublic void play() {System.out.println("xiaomi play...");}}interface Phone {void call();}interface Computer {void play();}
}

3.5 实战:抽象工厂+策略模式

3.5.1 业务方法调用
/*** 根据业务类型计算真正需要动账的存欠类型** @param materialInboundDO* @param walletTypeAndWeight* @return*/private Map<String, BigDecimal> doBizType(MaterialInboundDO materialInboundDO, Map<String, BigDecimal> walletTypeAndWeight) {final AbstractFactory factory = AbstractFactory.getFactory(materialInboundDO.getBillType());final MaterialWalletHandler materialInboundBean = factory.getMaterialInboundOutboundBean(materialInboundDO.getBizType());return materialInboundBean.doBizType(walletTypeAndWeight);}
3.5.2 抽象工厂创建二级工厂,定义获取业务对象接口(二级工厂去创建具体的业务对象)
/*** 抽象工厂解决供应商/客户来料不同的业务类型对客户钱包的动账变化** @date: 2023/7/18**/
public abstract class AbstractFactory {/*** 获得单据类型工厂** @param billType 单据类型* @return*/public static AbstractFactory getFactory(String billType) {if (BillTypeEnum.MATERIAL_CUSTOMER_IN.getKey().equals(billType)) {return new InboundCustomerFactory();} else if ( BillTypeEnum.MATERIAL_SUPPLIER_IN.getKey().equals(billType)) {return new InboundSupplierFactory();}  else if (BillTypeEnum.MATERIAL_CUSTOMER_OUT.getKey().equals(billType)) {return new OutboundCustomerFactory();}  else if (BillTypeEnum.MATERIAL_SUPPLIER_OUT.getKey().equals(billType)) {return new OutboundSupplierFactory();}throw new ServiceException("原料业务工厂不存在");}/*** 获得业务对象** @param bizType 业务类型* @return*/public abstract MaterialWalletHandler getMaterialInboundOutboundBean(String bizType);
}
3.5.3 二级工厂 获得对象
/*** descr 客户来料工厂** @date: 2023/7/18**/
public class InboundCustomerFactory extends AbstractFactory {@Overridepublic InboundCustomerHandler getMaterialInboundOutboundBean(String bizType) {if (BizTypeEnum.M_CUSTOMER_IN.getKey().equals(bizType)) {return new InboundCustomerIn();}throw new ServiceException("客户来料业务类型不存在");}
}
3.5.3 二级工厂 具体的业务对象
/*** descr 客户:客户来料** @date: 2023/7/18**/
public class InboundCustomerIn implements InboundCustomerHandler {@Overridepublic Map<String, BigDecimal> doBizType(Map<String, BigDecimal> walletTypeAndWeight) {return walletTypeAndWeight;}
}
3.5.3 处理的业务

顶层接口,如下子业务处理方式

/*** descr 来料/出料业务定义接口** @date: 2023/7/18**/
public interface MaterialWalletHandler {Map<String, BigDecimal> doBizType(Map<String, BigDecimal> walletTypeAndWeight);}

在这里插入图片描述

中间层子接口之一:用于规范业务

/*** descr 客户来料接口** @date: 2023/7/18**/
public interface InboundCustomerHandler extends MaterialWalletHandler {}

中间层接口实现1:用于处理业务

/*** descr 客户:客户来料** @date: 2023/7/18**/
public class InboundCustomerIn implements InboundCustomerHandler {@Overridepublic Map<String, BigDecimal> doBizType(Map<String, BigDecimal> walletTypeAndWeight) {return walletTypeAndWeight;}
}

中间层接口实现2:用于处理业务

/*** descr 供应商:发料兑料** @date: 2023/7/18**/
public class OutboundSupplierMix implements OutboundSupplierHandler {@Overridepublic Map<String, BigDecimal> doBizType(Map<String, BigDecimal> walletTypeAndWeight) {final HashMap<String, BigDecimal> walletTypeAndWeightMap = new HashMap<>(walletTypeAndWeight);walletTypeAndWeightMap.remove(WalletTypeEnum.CASH.getKey());return walletTypeAndWeightMap;}
}

补充:6大设计原则

1.单一职责
2.开放封闭
3.接口隔离
4.里氏替换
5.依赖倒置
6.迪米特法则

转送:java常用设计模式

3.6 总结:

  1. 以上实战中,代码没有写的很完美,还有许多改善的地方
  2. 抽象工厂在实际运用中比一般都例子都相对复杂;或可能写的理解的有所欠缺
  3. 抽象工厂定义了对象怎么创建;策略模式定义了业务怎么实现
  4. 设计模式的使用主要是对业务代码进行简化或抽象,尽量符合6大设计原则;在技术人员沟通中,只要说出某种设计模式,就能知道业务大概是怎么处理的,极大的减少了沟通成本

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/427554.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

3.chrony服务器

目录 1. 简介 1.1. 重要性 1.2. Linux的两个时钟 1.3. 设置日期时间 1.3.1. timedatectl命令设置 1.3.2. date命令设置 1.4. NTP 1.5. Chrony介绍 2. 安装与配置 2.1. 安装&#xff1a; 2.2. Chrony配置文件分析 2.3. 同步时间服务器 2.3.1. 授时中心 2.3.2. 实验…

进程通信与socket编程实践之猜数字小游戏

socket是实现进程通信的一种重要方式&#xff0c;本文将通过socket编程实现服务器进程与客户端进程之间的通信&#xff0c;并在通信之外实现猜数字的小游戏。 1. 设计思路 本文设计的C/S结构的猜数字游戏功能如下&#xff1a;服务器端自动生成一个1-100之间的随机数字&#x…

5.ROC-AUC机器学习模型性能的常用的评估指标

最近回顾机器学习基础知识部分的时候&#xff0c;看到了用于评估机器学习模型性能的ROC曲线。再次记录一下&#xff0c;想起之前学习的时候的茫然&#xff0c;希望这次可以更加清晰的了解这一指标。上课的时候听老师提起过&#xff0c;当时没有认真去看&#xff0c;所以这次可以…

Java毕业设计-基于jsp+servlet的学生推荐平台管理系统-第81期

获取源码资料&#xff0c;请移步从戎源码网&#xff1a;从戎源码网_专业的计算机毕业设计网站 项目介绍 基于jspservlet的学生推荐平台管理系统&#xff1a;前端 jsp、jquery&#xff0c;后端 servlet、jdbc&#xff0c;集成用户端和管理后台&#xff0c;用户端浏览书籍课程&…

All-Flash Array Key-Value Cache for Large Objects——论文泛读

EuroSys 2023 Paper 论文阅读笔记整理 问题 随着网络内容质量的提高&#xff0c;对缓存大型对象的需求不断增加。在生产内容交付网络&#xff08;CDN&#xff09;、图像和文档服务器中&#xff0c;大多数对象的大别为8KB–32KB&#xff08;CDN服务器&#xff09;、32KB–128K…

AI时代:驱动光模块市场增长

人工智能&#xff08;AI&#xff09;技术的崛起&#xff0c;犹如一股强大的催化力量&#xff0c;正在驱动各行各业经历前所未有的变革性转型。光学收发器市场作为这一范式转变的重要领域之一&#xff0c;正见证着由ChatGPT等先进模型引领的AI革命所带来的深刻影响。本文将深度剖…

学单片机前先学什么?

学单片机前先学什么&#xff1f; 在开始前我有一些资料&#xff0c;是我根据网友给的问题精心整理了一份「单片机的资料从专业入门到高级教程」&#xff0c; 点个关注在评论区回复“888”之后私信回复“888”&#xff0c;全部无偿共享给大家&#xff01;&#xff01;&#xff…

Linux中并发程序设计

进程的创建和回收 进程概念 概念 程序 存放在磁盘上的指令和数据的有序集合&#xff08;文件&#xff09; 静态的 进程 执行一个程序所分配的资源的总称 动态的进程和程序比较 注&#xff1a;进程是存在RAM中&#xff0c;程序是存放在ROM(flash)中的进程内容 BSS段&#xff…

45. 跳跃游戏 II - 力扣(LeetCode)

题目描述 给定一个非负整数数组&#xff0c;你最初位于数组的第一个位置。 数组中的每个元素代表你在该位置可以跳跃的最大长度。 你的目标是使用最少的跳跃次数到达数组的最后一个位置。 题目示例 输入: [2,3,1,1,4] 输出: 2 解释: 跳到最后一个位置的最小跳跃数是 2。从下标…

C#调用SqlSugar操作达梦数据库报错“无效的表或视图名”

安装达梦数据库后&#xff0c;使用SqlSugar连接测试数据库并基于DBFirst方式创建数据库表对应的类&#xff0c;主要代码如下&#xff1a; SqlSugarClient db new SqlSugarClient(new ConnectionConfig(){DbType DbType.Dm,ConnectionString "Serverlocalhost; User Id…

鸿蒙开发案例002

1、目标需求 界面有增大字体按钮&#xff0c;每次点击增大字体按钮&#xff0c;“Hello ArkTS”都会变大 2、源代码 Entry Component struct Page {textValue: string Hello ArkTSState textSize: number 50myClick():void{this.textSize 4}build() {Row() {Column() {//…

前端上传大文件使用分片上传

前提:分片上传针对于一些大的文件、普通大小的文件使用element中的上传组件可以实现效果,例如几G的文件就会比较卡,所以这时候就需要用到分片上传~ 前端及后端分片上传笔记 效果:(上传进度展示) 效果:(上传成功的效果展示) 1、 新建一个上传组件 2、使用vue-simple-…