大话设计模式java版本 第一章 简单工程模式

第一章 简单工程模式`

public abstract class Operation {
    private double _numberA;
    private double _numberB;

    public Operation() {
        super();
    }



    public Operation(double _numberA, double _numberB) {
        super();
        this._numberA = _numberA;
        this._numberB = _numberB;
    }



    public double get_numberA() {
        return _numberA;
    }



    public void set_numberA(double _numberA) {
        this._numberA = _numberA;
    }



    public double get_numberB() {
        return _numberB;
    }



    public void set_numberB(double _numberB) {
        this._numberB = _numberB;
    }



    public abstract double getResult();
}



public class OperationAdd extends Operation {

    @Override
    public double getResult() {
        double result = 0;
        result=super.get_numberA()+super.get_numberB();
        return result;
    }

}



public class OperationSub extends Operation{

    @Override
    public double getResult() {
        double result=0;
        result=super.get_numberA()-super.get_numberB();
        return result;
    }

}


public class OperationFactory {
    public static Operation creatOperation(String operate){
        Operation opera=null;
        switch(operate){
            case "+":
                opera=new OperationAdd();
                break;

            case "-":
                opera=new OperationSub();
                break;

        }
        return opera;
    }
}



public class test {
    public static void main(String[] args) {
        Operation opera;
        opera=OperationFactory.creatOperation("+");
        opera.set_numberA(5);
        opera.set_numberB(6);
        System.out.println(opera.getResult());
    }
}

你可能感兴趣的:(大话设计模式java版本 第一章 简单工程模式)