设计模式——简单工厂模式(SimpleFactory Pattern)
2013-10-21 23:59 雪夜&流星 阅读(262) 评论(0) 编辑 收藏 举报最近做项目总是遇到这样或者那样的问题,代码不够简洁,代码可扩展性不够好,耦合度又太高了,导致经常有种想去重构又无从下手的感觉。
无意间翻出了之前买的一本书《大话设计模式》读了一遍,受益匪浅,决定将其中的代码用java来实现一遍,个中精髓自行体会,
此处只做代码的具体实现。^-^
UML图:
模型:
产品基类:Product
package com.cnblog.clarck; /** * 产品类 * * @author clarck * */ public class Product { /** * 创建产品 * * @return */ public String createRealProduct() { String product = ""; return product; } }
产品A的实例:ProductA
package com.cnblog.clarck; /** * 产品A * * @author clarck * */ public class ProductA extends Product { @Override public String createRealProduct() { String productA = "产品A"; return productA; } }
产品B的实例:ProductB
package com.cnblog.clarck; /** * 产品B * * @author clarck * */ public class ProductB extends Product { @Override public String createRealProduct() { String productB = "产品B"; return productB; } }
简单工厂类:ProductFactory
package com.cnblog.clarck; /** * 产品工厂 * * @author clarck * */ 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
package com.cnblog.clarck; /** * 产品A,产品B的类型 * * @author clarck * */ public enum ProductType { PRODUCTA, PRODUCTB }
测试类:Test
package com.cnblog.clarck; /** * 测试用例 * * @author clarck * */ 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.PRODUCTA); String productB = product.createRealProduct(); System.out.println(productB); } }
实例运用:
---------------------------------------------------------------------------------------
数据运算基类:Operation
package com.cnblog.clarck; /** * 数据运算基类 * * @author clarck * */ public class Operation { private double mNmberA = 0; private double mNumberB = 0; public double getNumberA() { return mNmberA; } public void setNmberA(double numberA) { this.mNmberA = numberA; } public double getNumberB() { return mNumberB; } public void setNumberB(double numberB) { this.mNumberB = numberB; } /** * 获取运算结果 * @return */ public double getResult() { double result = 0; return result; } }
加法类:OperationAdd
package com.cnblog.clarck; /** * 加法类 * * @author clarck * */ public class OperationAdd extends Operation { @Override public double getResult() { double result = 0; result = getNumberA() + getNumberB(); return result; } }
减法运算类:OperationSub
package com.cnblog.clarck; /** * 减法类 * * @author clarck * */ public class OperationSub extends Operation { @Override public double getResult() { double result = getNumberA() - getNumberB(); return result; } }
乘法运算类:OperationMul
package com.cnblog.clarck; /** * 乘法 * * @author clarck * */ public class OperationMul extends Operation { @Override public double getResult() { double result = getNumberA() * getNumberB(); return result; } }
除法类:OperationDiv
package com.cnblog.clarck; /** * 除法类 * * @author clarck * */ public class OperationDiv extends Operation { @Override public double getResult() { if (getNumberB() == 0) { throwException(); } double result = getNumberA() / getNumberB(); return result; } private void throwException() { try { throw new Exception("除数不能为0."); } catch (Exception e) { e.printStackTrace(); } } }
简单工厂类:OperationFactory
package com.cnblog.clarck; /** * 工厂类 * * @author clarck * */ public class OperationFactory { /** * 创建运算类的工厂 * * @param operate * 运算类对象 * @return * 创建成功-运算类对象,返回空 */ public static Operation createOperation(OperationSymbol operate) { Operation oper = null; switch (operate) { case ADDITION: oper = new OperationAdd(); break; case SUBTRACTION: oper = new OperationSub(); break; case MULTIPLICATION: oper = new OperationMul(); break; case DIVISION: oper = new OperationDiv(); break; default: break; } return oper; } }
运算符号类:OperationSymbol
package com.cnblog.clarck; /** * 加法, 减法, 乘法, 除法 * * @author clarck * */ public enum OperationSymbol { ADDITION, SUBTRACTION, MULTIPLICATION, DIVISION }
测试类:Test
package com.cnblog.clarck; /** * 测试类 * * @author clarck * */ public class Test { public static void main(String[] args) { Operation oper = null; oper = OperationFactory.createOperation(OperationSymbol.ADDITION); oper.setNmberA(1.0); oper.setNumberB(2.0); double addResult = oper.getResult(); System.out.println("addResult =" + addResult); oper = OperationFactory.createOperation(OperationSymbol.SUBTRACTION); oper.setNmberA(1.0); oper.setNumberB(2.0); double subResult = oper.getResult(); System.out.println("subResult =" + subResult); oper = OperationFactory.createOperation(OperationSymbol.MULTIPLICATION); oper.setNmberA(1.0); oper.setNumberB(2.0); double mulResult = oper.getResult(); System.out.println("mulResult =" + mulResult); oper = OperationFactory.createOperation(OperationSymbol.DIVISION); oper.setNmberA(1.0); oper.setNumberB(2.0); double divResult = oper.getResult(); System.out.println("divResult =" + divResult); } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本