简单工厂模式(1)
例子是:计算器,
方法一:是简单的main实现
第一个类:
/**
* 计算器,控制台程序,输入两个数,一个运算符,得到结果
* @author zhangqf
*
*/
public class calculate {
//用传统的控制台输入的方法
public static void main(String[] args) {
BufferedReader strin=new BufferedReader(new InputStreamReader(System.in));
try {
System.out.println("请输入一个数:");
String strFirst=strin.readLine();
double dbfirst=convertTool.StringToDouble(strFirst);
//System.out.println("第一个:"+dbfirst);
System.out.println("请输入第二个数:");
String strTwo=strin.readLine();
double dbTwo=convertTool.StringToDouble(strTwo);
//System.out.println("第二个:"+dbTwo);
System.out.println("请输入操作符:");
String strOperate=strin.readLine();
//System.out.println("操作符:"+strOperate);
double dbResult=calculate.OperateResult(dbfirst, dbTwo, strOperate);
System.out.println("计算结果:"+dbResult);
} catch (IOException e) {
System.out.println("您的输入有错:"+e.getMessage());
e.printStackTrace();
}
}
public static double OperateResult(double dbfirst,double dbTwo,String operate) {
double result=0.0;
switch (operate) {
case "+":
result=dbfirst+dbTwo;
break;
case "-":
result=dbfirst-dbTwo;
break;
case "*":
result=dbfirst*dbTwo;
break;
case "/":
if (dbTwo==0) {
System.out.println("除数不能为0;");
}else{
result=dbfirst/dbTwo;
}
break;
default:
System.out.println("输入的符号不符合");
break;
}
return result;
}
}
第二个类:
public class convertTool {
public static double StringToDouble (String s) {
if (s==null||s.length()==0)
return 0d;
return Double.parseDouble(s);
}
}
第二种方法:简单工厂模式:我自己理解就是用的:面向对象,继承,封装,多态
/**
*1. 接口类
* @author zhangqf
*
*/
public interface operationRestlt {
public double dbResule(double _dbFirst,double _dbTwo);
}
/**
* 2.加号运算
* @author zhangqf
*
*/
public class plusOperate implements operationRestlt{
@Override
public double dbResule(double _dbFirst, double _dbTwo) {
double dbresult=_dbFirst+_dbTwo;
return dbresult;
}
}
/**
*3. 减号运算
* @author zhangqf
*/
public class subOperate implements operationRestlt{
@Override
public double dbResule(double _dbFirst, double _dbTwo) {
double dbresult=_dbFirst-_dbTwo;
return dbresult;
}
}
/**
* 4.乘号运算
* @author zhangqf
*/
public class multipleOperate implements operationRestlt {
@Override
public double dbResule(double _dbFirst, double _dbTwo) {
double dbresule=_dbFirst*_dbTwo;
return dbresule;
}
}
/**
* 5.除号运算
* @author zhangqf
*/
public class diviedOperate implements operationRestlt{
@Override
public double dbResule(double _dbFirst, double _dbTwo) {
double dbresult=0d;
if (_dbTwo==0) {
System.out.println("被除数不能为0");
}else{
dbresult=_dbFirst/_dbTwo;
}
return dbresult;
}
}
//业务类
/**
*6. 业务类
* @author zhangqf
*
*/
public class operateAction {
public static double operation(double _dbFirst,double _dbTwo,String oper) {
double dbResult=0d;
switch (oper) {
case "+":
plusOperate plusOperate =new plusOperate();
dbResult=plusOperate.dbResule(_dbFirst, _dbTwo);
break;
case "-":
subOperate operate=new subOperate();
dbResult=operate.dbResule(_dbFirst, _dbTwo);
break;
case "*":
multipleOperate multipleOperate=new multipleOperate();
dbResult=multipleOperate.dbResule(_dbFirst, _dbTwo);
break;
case "/":
diviedOperate diviedOperate=new diviedOperate();
dbResult=diviedOperate.dbResule(_dbFirst, _dbTwo);
break;
default:
System.out.println("输入的操作符不存在!");
break;
}
return dbResult;
}
}
import java.util.Scanner;
/**
* 7.主程序调用运行
* @author zhangqf
*
*/
public class main {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
System.out.println("请输入第一个数:");
double dbfirst=scanner.nextDouble();
System.out.println("请输入第二个数:");
double dbtwo=scanner.nextDouble();
System.out.println("请输入操作符【+,-,*,/】");
String operate=scanner.next();
double result=operateAction.operation(dbfirst,dbtwo,operate);
System.out.println(result);
}
}