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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值