1、简单工厂模式——反射+Properties实现计算器

设计模式案例源于本人对大话设计模式和网上资料学习总结,每个设计模式先上代码,后续有时间补充


package com.thpin.repository.designpattern;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.Scanner;
public class SimpleFactoryDemo {
    public static void main(String[] args) throws InterruptedException {
        Operation oper = null;
        String operate = null;
        int n1 = 0;
        int n2 = 0;
        String flag = "y";
        Scanner sc = new Scanner(System.in);
        while ("y".equals(flag)) {
            // 应用运行过程中可以临时修改配置文件中操作符对应的类
            System.out.print("请输入操作符(+、-、*、/、%):");
            operate = sc.nextLine();
            oper = OperationFactory.createOperation(operate);
            System.out.print("请输入第一个操作数:");
            n1 = Integer.parseInt(sc.nextLine());
            System.out.print("请输入第二个操作数:");
            n2 = Integer.parseInt(sc.nextLine());
            System.out.println(n1 + operate + n2 + "=" + oper.getResult(n1, n2));
            System.out.print("继续请输入y,停止输入任意字符:");
            flag = sc.nextLine();
        }
        sc.close();
    }
}
/*
 * 简单计算器
 * 简单工厂模式   反射+Properties
 * 面向对象、松耦合、可扩展、可维护、可复用、灵活性好
 */
public class OperationFactory {
    private static Properties init() {
        // 从properties文件中读取operate对应的完整包名
        Properties properties = new Properties();
        try {
            properties.load(
                    new FileInputStream("src/main/java/com/thpin/repository/designpattern/operateMapping.properties"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return properties;
    }

    public static Operation createOperation(String operate) {
        Operation oper = null;
        try {
            String fullName = init().getProperty(operate);
            oper = (Operation) Class.forName(fullName).newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return oper;
    }

    public static void main(String[] args) throws InterruptedException {
        Operation oper = null;
        String operate = null;
        int n1 = 0;
        int n2 = 0;
        String flag = "y";
        Scanner sc = new Scanner(System.in);
        while ("y".equals(flag)) {
            // 应用运行过程中可以临时修改配置文件中操作符对应的类
            System.out.print("请输入操作符(+、-、*、/、%):");
            operate = sc.nextLine();
            oper = OperationFactory.createOperation(operate);
            System.out.print("请输入第一个操作数:");
            n1 = Integer.parseInt(sc.nextLine());
            System.out.print("请输入第二个操作数:");
            n2 = Integer.parseInt(sc.nextLine());
            System.out.println(n1 + operate + n2 + "=" + oper.getResult(n1, n2));
            System.out.print("继续请输入y,停止输入任意字符:");
            flag = sc.nextLine();
        }
        sc.close();
    }
}

/*
 * 运算操作类接口,可根据需求派生出多个实现类
 */
abstract class Operation {
    public abstract int getResult(int a, int b);
}

/*
 * 加法操作
 */
class OperationAdd extends Operation {
    public int getResult(int a, int b) {
        return a + b;
    }
}

/*
 * 减法操作
 */
class OperationSub extends Operation {
    public int getResult(int a, int b) {
        return a - b;
    }
}

/*
 * 乘法操作
 */
class OperationMul extends Operation {
    public int getResult(int a, int b) {
        return a * b;
    }
}

/*
 * 除法操作
 */
class OperationDiv extends Operation {
    public int getResult(int a, int b) {
        return a / b;
    }
}

/*
 * 取模操作,后加的操作
 */
class OperationMod extends Operation {
    public int getResult(int a, int b) {
        return a % b;
    }
}


operateMapping.properties:

+=com.thpin.repository.designpattern.OperationAdd
-=com.thpin.repository.designpattern.OperationSub
*=com.thpin.repository.designpattern.OperationMul
/=com.thpin.repository.designpattern.OperationDiv

%=com.thpin.repository.designpattern.OperationMod #后加的取模映射

结果:

请输入操作符(+、-、*、/、%):+
请输入第一个操作数:1
请输入第二个操作数:2
1+2=3
继续请输入y,停止输入任意字符:y
请输入操作符(+、-、*、/、%):-
请输入第一个操作数:5
请输入第二个操作数:2
5-2=3

继续请输入y,停止输入任意字符:


从后添加的取模操作功能,足可以看出设计模式的优势

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值