1业务和逻辑写在一起
import java.util.*;
import java.io.Console;
class Factory
{
public static void main(String[] args)
{
try
{
System.out.println("工厂模式");
Console console = System.console();
String strNumberA = console.readLine("请输入数字A:");
String strOperate = console.readLine("请输入运算符号(+-*/):");
String strNumberB = console.readLine("请输入数字B:");
String strResult = "";
switch (strOperate)
{
case "+":
strResult = String.valueOf(Double.valueOf(strNumberA) + Double.valueOf(strNumberB));
break;
case "-":
strResult = String.valueOf(Double.valueOf(strNumberA) - Double.valueOf(strNumberB));
break;
case "*":
strResult = String.valueOf(Double.valueOf(strNumberA) * Double.valueOf(strNumberB));
break;
case "/":
if (strNumberB != "0")
strResult = strResult = String.valueOf(Double.valueOf(strNumberA) / Double.valueOf(strNumberB));
else
strResult = "除数不能为0";
break;
}
System.out.println("结果是: " + strResult);
}
catch (Exception ex)
{
System.out.println("错了");
}
}
}
2运算和显示分开
import java.util.*;
import java.io.Console;
class Decouping {
public static class Operation {
public static double GetResult(double numberA, double numberB, String operate) {
double result = 0d;
switch (operate) {
case "+":
result = numberA + numberB;
break;
case "-":
result = numberA - numberB;
break;
case "*":
result = numberA * numberB;
break;
case "/":
if (numberB != 0)
result = numberA / numberB;
else
result = 0;
break;
}
return result;
}
}
public static void main(String[] args) {
try {
System.out.println("工厂模式");
Console console = System.console();
String strNumberA = console.readLine("请输入数字A:");
String strOperate = console.readLine("请输入运算符号(+-*/):");
String strNumberB = console.readLine("请输入数字B:");
String strResult = "";
strResult = String.valueOf(Operation.GetResult(Double.valueOf(strNumberA), Double.valueOf(strNumberB), strOperate));
System.out.println("结果是: " + strResult);
}
catch (Exception ex) {
System.out.println("您的输入有错");
}
}
}
3简单工厂
import java.util.*;
class Operation {
double numberA = 0;
double numberB = 0;
private double result = 0;
public void setNumberA(double numberA){
this.numberA = numberA;
}
public double getNumberA(double numberA){
return numberA;
}
public void setNumberB(double numberB){
this.numberB = numberB;
}
public double getNumberB(double numberB){
return numberB;
}
public double GetResult() {
double result = 0;
return result;
}
}
class OperationAdd extends Operation {
public double GetResult() {
double result = 0;
result = numberA + numberB;
return result;
}
}
class OperationSub extends Operation {
public double GetResult() {
double result = 0;
result = numberA - numberB;
return result;
}
}
class OperationMul extends Operation {
public double GetResult() {
double result = 0;
result = numberA * numberB;
return result;
}
}
class OperationDiv extends Operation {
public double GetResult() {
double result = 0;
// if ( numberB == 0 )
// throw new Exception("除数不能为0");
result = numberA / numberB;
return result;
}
}
class LXJSimpleFactory {
public static Operation createOperate(String operate) {
Operation oper = null;
switch (operate) {
case "+":
oper = new OperationAdd();
break;
case "-":
oper = new OperationAdd();
break;
case "*":
oper = new OperationAdd();
break;
case "/":
oper = new OperationAdd();
break;
}
return oper;
}
}
public class SimpleFactory {
public static void main(String[] args) {
System.out.println("简单工厂");
Operation oper;
oper = LXJSimpleFactory.createOperate("+");
oper.numberA = 1;
oper.numberB = 29;
double result = oper.GetResult();
System.out.println("1+29 is:" + result);
}
}