简单工厂模式
Operation.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | package calc; public abstract class Operation { private static double _numberA = 0 ; private static double _numberB = 0 ; static class NumberA { public static double get_numberA() { return _numberA; } public static void set_numberA( double numberA) { _numberA = numberA; } } static class NumberB { public static double get_numberB() { return _numberB; } public static void set_numberB( double numberB) { _numberB = numberB; } } public abstract double GetResult(); } class OperationAdd extends Operation { public double GetResult() { double result = 0 ; result = NumberA.get_numberA() + NumberB.get_numberB(); return result; } } class OperationSub extends Operation { public double GetResult() { double result = 0 ; result = NumberA.get_numberA() - NumberB.get_numberB(); return result; } } class OperationMul extends Operation { public double GetResult() { double result = 0 ; result = NumberA.get_numberA() * NumberB.get_numberB(); return result; } } class OperationDiv extends Operation { @Override public double GetResult() { double result = 0 ; if (NumberB.get_numberB() == 0 ) try { throw new Exception( "not divide by zero" ); } catch (Exception e) { // TODO Auto-generated catch block System.out.println( "not divide by zero" ); } result = NumberA.get_numberA() / NumberB.get_numberB(); return result; } } |
OperationFactory.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | package calc; public class OperationFactory { public static Operation createOperate (String operator) { Operation oper = null ; if (operator == "+" ) oper = new OperationAdd(); else if (operator == "-" ) oper = new OperationSub(); else if (operator == "*" ) oper = new OperationMul(); else if (operator == "/" ) oper = new OperationDiv(); return oper; } } |
测试类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | package calc; import java.io.*; class Calculator { // 输入两个数, 相除得结果 /** * @param args */ public static void main(String[] args) throws IOException { // BufferedReader b = new BufferedReader(new InputStreamReader(System.in)); // System.out.println("please input a number A:"); // String A = b.readLine(); // System.out.println("please input a number B:"); // String B = b.readLine(); // int C = (new Integer(A)).intValue()/(new Integer(B)).intValue(); // System.out.println("The result is :" + C); Operation oper; oper = OperationFactory.createOperate( "/" ); Operation.NumberA.set_numberA( 1 ); Operation.NumberB.set_numberB( 0 ); double result = oper.GetResult(); System.out.println(result); } } |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 现代计算机视觉入门之:什么是图片特征编码
· .NET 9 new features-C#13新的锁类型和语义
· Linux系统下SQL Server数据库镜像配置全流程详解
· 现代计算机视觉入门之:什么是视频
· 你所不知道的 C/C++ 宏知识
· 不到万不得已,千万不要去外包
· C# WebAPI 插件热插拔(持续更新中)
· 会议真的有必要吗?我们产品开发9年了,但从来没开过会
· 【译】我们最喜欢的2024年的 Visual Studio 新功能
· 如何打造一个高并发系统?