简单工厂模式,策略模式和命令模式实现计算器
程序员文章站
2022-05-24 23:50:10
...
简要介绍
本实验采用简单工厂模式,策略模式和命令模式实现计算器的简单加减乘除算法,以及实现计算器的简单的撤销。
设计思路
简单工厂模式:
主要特点:在工厂类中包含了必要的逻辑判断,根据客户端的选择条件动态实例化相关的类。对于客户端来说,去除了与具体产品的依赖。
缺点:当增加一个其他运算时候,除了增加类,还必须修改在简单工厂类中的case语句,违背了修改的封闭性原则。
策略与简单工厂模式结合:
CalculateContext类中属性含有运算类!同时在CalculateContext类内部实现简单工厂模式;这样子客户端只需要认识CalculateContext类即可!
思想:其他类中的成员属性含有基类,有了基类以后可以在构造方法内部实现工厂模式。最后在其他类调用基类的方法就可以将基类完全隐藏起来了。在客户端实例化的是CalculateContext的对象,调用的是类CalculateContext中的calculate()和undo()方法;这使得操作类的父类Calculator都不让客户端认识了。
命令模式:
将行为封装成一个命令,然后将命令添加到调用者内,再由调用者发送给真正的处理者。即通过调用者调用处理者执行命令的方式,在本案例的计算器中,计算器作为调用者,命令是四则运算操作,计算类作为真正处理者,这样就对于计算器与计算类进行了解耦。
类图
源代码
四个计算类:
package com.Calculator;
public abstract class Calculate {
public double sum=1;
public abstract void calculate(double value);
}
加法计算类:
package com.Calculator;
public class addCal extends Calculate {
public void calculate(double value){
System.out.print("加法器计算:" + sum + "+(" + value + ")=");
sum = sum + value;
System.out.println("运算结果:" + sum);
}
}
减法计算类:
package com.Calculator;
public class subCal extends Calculate {
public void calculate(double value) {
System.out.print("减法器计算:" + sum + "-(" + value + ")=");
sum = sum - value;
System.out.println("运算结果:" + sum);
}
}
乘法计算类:
package com.Calculator;
public class mulCal extends Calculate {
public void calculate(double value){
System.out.print("乘法器计算:" + sum + "*(" + value + ")=");
sum = sum * value;
System.out.println("运算结果:" + sum);
}
}
除法计算类:
package com.Calculator;
public class divCal extends Calculate {
public void calculate(double value){
System.out.print("除法器计算:" + sum + "/(" + value + ")=");
if(value==0)
System.out.println("0不能为除数") ;
else
sum = sum / value;
System.out.println("运算结果:" + sum);
}
}
四个命令封装:
package com.Calculator;
public abstract class Command {
public abstract void execute(double value);//在原先基础上再加上一个value值
public abstract void undo();//撤销
}
加法命令:
package com.Calculator;
public class addCommand extends Command {//命令模式,封装算法的具体实现
private addCal add=new addCal();
private double value=0;
public void execute(double value){
this.value=value;
add.calculate(value);
}
public void undo(){
add.calculate(-value);
}
}
减法命令
package com.Calculator;
public class subCommand extends Command{
private subCal sub=new subCal();
private double value=0;
public void execute(double value){
this.value=value;
sub.calculate(value);
}
public void undo(){
sub.calculate(-value);
}
}
乘法命令:
package com.Calculator;
public class mulCommand extends Command {
private mulCal mul = new mulCal();
private divCal div = new divCal();
private double value = 0;
public void execute(double value) {
this.value = value;
mul.calculate(value);
}
public void undo() {
mul.calculate( 1/value);
}
}
除法命令:
package com.Calculator;
public class divCommand extends Command {
private divCal div=new divCal();
private double value=0;
public void execute(double value){
this.value=value;
div.calculate(value);
}
public void undo(){
div.calculate(1/value);
}
}
四个计算器:
package com.Calculator;
public abstract class Calculator {
private Command command;
public void setCommand(Command command){};
public void calculate(double value){
};
public void undo(){};
}
加法计算器:
package com.Calculator;
public class addCalculator extends Calculator{
private Command command=new addCommand();//命令模式,封装算法
//private addCal add=new addCal();
//执行运算
public void calculate(double value){
command.execute(value);
}
public void undo() {
System.out.println("撤销,此时结果:" );
command.undo();
}
}
减法计算器:
package com.Calculator;
public class subCalculator extends Calculator {
private Command command=new subCommand();
public void calculate(double value){
command.execute(value);
}
public void undo() {
System.out.println("撤销,此时结果:");
command.undo();
}
}
乘法计算器:
package com.Calculator;
public class mulCalculator extends Calculator {
private Command command=new mulCommand();
public void calculate(double value){
command.execute(value);
}
public void undo() {
System.out.println("撤销,此时结果:");
command.undo();
}
}
除法计算器:
package com.Calculator;
public class divCalculator extends Calculator {
private Command command=new divCommand();
//执行运算
public void calculate(double value){
command.execute(value);
}
public void undo() {
System.out.println("撤销,此时结果:");
command.undo();
}
}
策略模式下的CalculateContext类
package com.Calculator;
public class CalculateContext {
private Calculator calculator=null;//策略模式
private Command command=null;
double value;
public CalculateContext(String op){//工厂模式
switch(op) {
case "+":
calculator = new addCalculator();
command=new addCommand();
break;
case "-":
calculator = new subCalculator();
command=new subCommand();
break;
case "*":
calculator = new mulCalculator();
command=new mulCommand();
break;
case "/":
calculator = new divCalculator();
command=new divCommand();
break;
}
}
public void calculate(double value){
this.value=value;
this.calculator.calculate(value);
}
public void undo(){
this.calculator.undo();
}
}
客户端测试:
package com.Calculator;
public class client {
public static void main(String[] args) {
CalculateContext cc = new CalculateContext("+");
cc.calculate(2);
cc.calculate(2);
cc.undo();
CalculateContext cc1 = new CalculateContext("-");
cc1.calculate(2);
cc1.calculate(2);
cc1.undo();
CalculateContext cc2 = new CalculateContext("*");
cc2.calculate(2);
cc2.calculate(2);
cc2.undo();
CalculateContext cc3 = new CalculateContext("/");
cc3.calculate(2);
cc3.calculate(2);
cc3.undo();
}
}
6.运行结果:
上一篇: 命令模式undo/redo实例计算器
下一篇: CentOS时间、日期、计算器命令