设计模式之简单工厂模式

这一生的挚爱 提交于 2019-12-05 06:27:45

本博客例子均来自  大话设计模式一书!

 

1.首先定一个计算器:

实现简单的加减乘除

public class Compute1 {
    public static void main(String args[]) {
        try {
            Scanner sc = new Scanner(System.in);
            String result = "";
            System.out.println("请输入一个数字A");
            int numA = sc.nextInt();
            System.out.println("请输入一个数字B");
            int numB = sc.nextInt();
            sc.nextLine();
            System.out.println("请输入运算符+-*/");
            String Opration = sc.nextLine();
            switch (Opration) {
            case "+":
                result = String.valueOf(numA + numB);
                break;
            case "-":
                result = String.valueOf(numA - numB);
                break;
            case "*":
                result = String.valueOf(numA * numB);
                break;
            case "/":
                if (numB == 0) {
                    result = "除数不能为0";
                    break;
                }
                result = String.valueOf(numA / numB);
                break;
            default:
                break;
            }
            System.out.println(result);
        } catch (Exception e) {
            System.out.println("您的输入有误:" + e.getMessage());
        }
    }
}

此方法可以实现,但是耦合度高。如果要修改其中的一个操作,就要对整个类进行操作。且不易于复用,如果我要单用加法,或者减法是没法实现的。

这不是面向对象的特点。首先改造此方法:

1.2降低耦合度

public class Compute2 {
    public static double GetResult(double numA,double numB,String operate) {
        double result = 0.00;
        switch(operate) {
            case "+":
                result = numA + numB;
                break;
            case "-":
                result = numA - numB;
                break;
            case "*":
                result = numA * numB;
                break;
            case "/":
                result = numA / numB;
                break;
        }
        return result;
    }
}
public class Compute2Main {
    public static void main(String args[]) {
        try {
        Compute2 compute2 = new Compute2();
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个数字A");
        double numA = sc.nextDouble();
        System.out.println("请输入一个数字B");
        double numB = sc.nextDouble();
        sc.nextLine();
        System.out.println("请输入运算符+-*/");
        String Opration = sc.nextLine();
        double getResult = Compute2.GetResult(numA, numB, Opration);
        System.out.println(getResult);
        }catch(Exception e) {
            System.out.println("检查您的输入是否有误!"+e.getMessage());
        }
    }
}

这样分离了业务层和表现层。但是可复用的能力还是不行,达不到面向对象的特点。

继续改造1.2的代码。分离加减乘除,这样对于每个方法,我们都可以实现复用!!! 

这才是面向对象的特点啊!

public interface OperationIn {
    double GetResult(double numA,double numB) throws Exception;
}
public class sum implements OperationIn{
    @Override
    public double GetResult(double numA, double numB) throws Exception {
        // TODO Auto-generated method stub
        return numA+numB;
    }
}
public class reduce implements OperationIn{
    @Override
    public double GetResult(double numA,double numB) {
        return numA-numB;
    }
}
public class cc implements OperationIn {
    @Override
    public double GetResult(double numA,double numB) {
        return numA*numB;
    }
}
public class cf implements OperationIn {
    @Override
    public double GetResult(double numA,double numB) throws Exception {
        if(numB==0)
            throw new Exception("除数不能为0");
        return numA/numB;
    }
}

复用以后,我们就可以开始建立我们的“工厂了”

public class OperationFactory {
    public static  OperationIn createOperation(String operate) {
        OperationIn oper = null;
        switch(operate) {
            case "+":
                oper = new sum();
                break;
            case "-":
                oper = new reduce();
                break;
            case "*":
                oper = new cc();
                break;
            case "/":
                oper = new cf();
        }
        return oper;
    }
}

通过运算符号来实现对象的实例化!!

这样,当我们增加功能的时候只需在工厂增加相应的switch分支即可

方便,复用,灵活,封装,多态提现的淋漓尽致

public class Main {

    public static void main(String[] args) throws Exception {
        try {
        // TODO Auto-generated method stub
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个数字A");
        int numA = sc.nextInt();
        System.out.println("请输入一个数字B");
        int numB = sc.nextInt();
        OperationIn oper;
        oper  = OperationFactory.createOperation("-");
        double result = oper.GetResult(numA, numB);
        System.out.println(result);
        }catch(Exception e) {
            System.out.println(e.getMessage());
        }
    }

}

 

 

uml类图如下

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!