1、简单工厂模式——反射+Properties实现计算器
程序员文章站
2024-01-20 16:55:58
...
设计模式案例源于本人对大话设计模式和网上资料学习总结,每个设计模式先上代码,后续有时间补充
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,停止输入任意字符:
从后添加的取模操作功能,足可以看出设计模式的优势