Simple_Factory_Pattern

"Simple Factory Pattern" uses polymorphic to implement the open and close principle.

The "Factory" actually encapsulates the logic of different object's creation.

The "simple" reflect in the whole process.The inner of it contains so many judgement statements.

Detailed code examples:

package simple_factory_pattern;

public abstract class Operation {
    protected double A;
    protected double B;

    public double getA() {
        return A;
    }

    public void setA(double a) {
        A = a;
    }

    public double getB() {
        return B;
    }

    public void setB(double b) {
        B = b;
    }

    public abstract double getResult();
}

扫描二维码关注公众号,回复: 3402031 查看本文章

package simple_factory_pattern;

public class Add extends Operation {
    public double getResult() {
        return super.getA() + super.getB();
    }
}

package simple_factory_pattern;

public class Minus extends Operation {
    public double getResult() {
        return super.getA() - super.getB();
    }
}

package simple_factory_pattern;

public class Multiple extends Operation {
    public double getResult() {
        return super.getA() * super.getB();
    }
}

package simple_factory_pattern;

public class Divided extends Operation {
    public double getResult() {
        try {
            if (super.getB() == 0)
                throw new Exception("Wrong with B equals zero!");
        } catch (Exception e) {
            e.printStackTrace();
        } // finally {
          // return super.getA()/super.getB();
          // If the sentence above exists,the previous sentence will be covered.
          // Because the sentences which contained in the clause of "finally"
          // must be executed in final.
          // }
        return super.getA() / super.getB();

    }
}
/***
 * "throws" is used to illustrate these possible Exceptions in the current
 * function.But the process of it is executed by the caller.So we can just sees
 * them as the symbol of exceptions which may be threw. By comparison,"throw" is
 * a active behavior which produce the exception that contains the specific
 * meaning.
 */

package simple_factory_pattern;

public class OperationFactory {
    public static Operation createOperation(String operate) {
        Operation oper = null;
        switch (operate) {
        case "+":
            oper = new Add();
            break;
        case "-":
            oper = new Minus();
            break;
        case "*":
            oper = new Multiple();
            break;
        case "/":
            oper = new Divided();
            break;
        default:
            System.out.println("The operator is wrong!");
            break;
        }
        return oper;
    }
}

package simple_factory_pattern;

import java.util.Scanner;

public class Main {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        String oper = sc.next();
        double a = sc.nextDouble();
        double b = sc.nextDouble();
        Operation o = OperationFactory.createOperation(oper);
        o.setA(a);
        o.setB(b);
        System.out.println(o.getResult());
        sc.close();
    }
}
This is a general introduction to the 23 design patterns:
https://blog.csdn.net/GZHarryAnonymous/article/details/81567214

猜你喜欢

转载自blog.csdn.net/GZHarryAnonymous/article/details/82694886