シンプルなファクトリーモデル

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));
    }
}

参考資料:ChengJieによるビッグトークデザインパターン

おすすめ

転載: blog.csdn.net/qq_37771811/article/details/103755554