Simple factory pattern Summary



Intuitive way to use a computer to understand the function of process-oriented to solve the problem,
the problem of coupling occurs,
a short-term solution,
so that the program is not easy to maintain and expand.
It should be applied to object-oriented programming ideas which, through encapsulation, inheritance, polymorphism will reduce the coupling procedure,
the use of enhanced mode program design flexibility, making it easy to modify and reuse.
In order to develop better programming habits and thinking,
start to start learning and using design patterns,
hope there are plans to study design patterns, and gradually learned from a trained, trained, trained, into the inner!

A simple factory pattern:
create a special class, it creates objects of different classes of cases, to achieve separation of business logic and interface logic, to reduce the coupling.

Second, in order to achieve a simple calculator, for example: 
to create an abstract base class operator
inherit the abstract class, achieve specific operational methods abstract class
to create a simple factory class, by a simple calculation method factory class is selected according to the situation that you need to create the selected class object.

abstract class Operation{
    private double numberA=0;
    private double numberB=0;

    public double getNumberA() {
        return numberA;
    }

    public void setNumberA(double numberA) {
        this.numberA = numberA;
    }

    public double getNumberB() {
        return numberB;
    }

    public void setNumberB(double numberB) {
        this.numberB = numberB;
    }

    abstract double getResult() throws Exception;
}

/**
 * 继承抽象类,实现一些具体的运算
 */
class Add extends Operation{

    @Override
    double getResult() {
        return getNumberA()+getNumberB();
    }
}

class Less extends Operation{


    @Override
    double getResult() {
        return getNumberA()-getNumberB();
    }
}

class Mul extends Operation {


    @Override
    double getResult() throws Exception{
        if (getNumberB()!=0){
            return getNumberA()/getNumberB();
        }else{
            throw new Exception("除数为零,不合法");
        }
    }
}

public class SimpleFactory {
    public Operation chooseFunc (String operate){
        Operation operation=null;
        switch (operate){
            case "+":
                operation=new Add();
                break;
            case "-":
                operation=new Less();
                break;
            case "/":
                operation=new Mul();
                break;
            default:
                System.out.println("继续扩展方法吧");
                break;
        }
        return operation;
    }
}

class Test{
    public static void main(String[] args) throws Exception {
        double numberA=23;
        double numberB=22;
        String[] operate={"+","-","/"};

        SimpleFactory simpleFactory=new SimpleFactory();
        Operation operation=simpleFactory.chooseFunc(operate[2]);
        operation.setNumberA(numberA);
        operation.setNumberB(numberB);
        System.out.println(operation.getResult());
    }
}

 

  

 


Guess you like

Origin www.cnblogs.com/chen-ying/p/10979058.html