简单的工厂模式

1、简单工厂模式

  • 例:让我们写一个加减乘除的方法,最初的写法:(为了方便未判定除法时分母为0的状况)
public class Operation {
    public static double getReault(double left ,double right,String op) {
        double result = 0;
        switch (op) {
            case "+":
                result = left+right;
                break;
            case "-":
                result = left-right;         
                break;
            case "*":
                result = left*right;
                break;
            case "/":
                result = left/right;
                break;
            default:
                break;
        }
        return result;
    }
}

这种方法虽然在一定程度上将功能进行抽离,但是对于增加运算不是很有利。

  • 当我们运用继承特性时,例:
//首先定义一个父类
public class Operation {
    private double left;
    private double right;
    public double getLeft() {
        return left;
    }
    public void setLeft(double left) {
        this.left = left;
    }
    public double getRight() {
        return right;
    }
    public void setRight(double right) {
        this.right = right;
    }
    public double getReault() {
        return 0;
    }    
}
//第二步,创建加减乘除四个类继承Operation
public class OperationAdd extends Operation {
    @Override
    public double getReault() {
        return this.getLeft() + this.getRight();
    }  
}

public class OperationSub extends Operation {
    @Override
    public double getReault() {
        return this.getLeft() - this.getRight();
    }
}

public class OperationMul extends Operation {
    @Override
    public double getReault() {
        return this.getLeft() * this.getRight();
    }
}

public class OperationDiv extends Operation {
    @Override
    public double getReault() {
        return this.getLeft() / this.getRight();
    }    
}
//定义工厂类
public class OperationFactory {
    public static Operation craeteOperation(String op) {
        Operation operation = null;
        switch (op) {
            case "+":
                operation = new OperationAdd();
                break;
            case "-":
                operation = new OperationSub();
                break;
            case "*":
                operation = new OperationMul();
                break;
            case "/":
                operation = new OperationDiv();
                break;
            default:
                break;
        }
        return operation;      
    }
}

//主函数测试类
public class Test {
    public static void main(String[] args) {
        Operation operation = OperationFactory.craeteOperation("+");
        operation.setLeft(1);
        operation.setRight(2);
        System.out.println(operation.getReault());
    }
}

这时我们可以根据输入的符号通过工厂动态创建对象完成加减乘除运算;

如果出现新的需求,例如增加sqrt函数,只需要创建一个类继承Operation,乍一看代码量好像增加了,但是代码的扩展性提高了。

  • 既然可以通过继承类实现简单工厂模式,那么接口也可以做到,在本例中接口可能不如继承类代码量更少。
//首先定义一个接口
public interface Operation {
    //将公共方法抽出定义在接口中
    double getResult(double left,double right);
}
//加
public class OperationAdd implements Operation {
    @Override
    public double getResult(double left, double right) {
        return left + right;
    }
}
//减
public class OperationSub implements Operation {
    @Override
    public double getResult(double left, double right) {
        return left - right;
    }
}
//乘
public class OperationMul implements Operation {
    @Override
    public double getResult(double left, double right) {
        return left * right;
    }
}
//除
public class OperationDiv implements Operation {
    @Override
    public double getResult(double left, double right) {
        return left / right;
    }
}

//定义的工厂类没有什么变化
public class OperationFactory {
    public static Operation craeteOperation(String op) {
        Operation operation = null;
        switch (op) {
            case "+":
                operation = new OperationAdd();
                break;
            case "-":
                operation = new OperationSub();
                break;
            case "*":
                operation = new OperationMul();
                break;
            case "/":
                operation = new OperationDiv();
                break;
            default:
                break;
        }
        return operation;      
    }
}
//主函数测试类
public class Test {
    public static void main(String[] args) {
        Operation operation = OperationFactory.craeteOperation("+");
        System.out.println(operation.getResult(1, 3));
    }
}

参考资料:大话设计模式,程杰著

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

渡劫-JS

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值