工厂方法模式,定义一个用于创建对象的接口,让子类决定实例化哪个类。工厂方法使一个类的实例化延迟到其子类。
UML图:
运算基类:
package com.cnblog.clarck;
/**
* 数据运算基类
*
* @author clarck
*
*/
public class Operation {
private double mNmberA = 0;
private double mNumberB = 0;
public double getNumberA() {
return mNmberA;
}
public void setNmberA(double numberA) {
this.mNmberA = numberA;
}
public double getNumberB() {
return mNumberB;
}
public void setNumberB(double numberB) {
this.mNumberB = numberB;
}
/**
* 获取运算结果
* @return
*/
public double getResult() {
double result = 0;
return result;
}
}
具体产品类:
package com.cnblog.clarck;
/**
* 加法类
*
* @author clarck
*
*/
public class OperationAdd extends Operation {
@Override
public double getResult() {
double result = 0;
result = getNumberA() + getNumberB();
return result;
}
}
package com.cnblog.clarck;
/**
* 减法类
*
* @author clarck
*
*/
public class OperationSub extends Operation {
@Override
public double getResult() {
double result = getNumberA() - getNumberB();
return result;
}
}
package com.cnblog.clarck;
/**
* 乘法
*
* @author clarck
*
*/
public class OperationMul extends Operation {
@Override
public double getResult() {
double result = getNumberA() * getNumberB();
return result;
}
}
package com.cnblog.clarck;
/**
* 除法类
*
* @author clarck
*
*/
public class OperationDiv extends Operation {
@Override
public double getResult() {
if (getNumberB() == 0) {
throwException();
}
double result = getNumberA() / getNumberB();
return result;
}
private void throwException() {
try {
throw new Exception("the div number can't be zero.");
} catch (Exception e) {
e.printStackTrace();
}
}
}
工厂类接口:
package com.cnblog.clarck;
/**
* 算法工厂
*
* @author clarck
*
*/
public interface IFactory {
public Operation createOperation();
}
工厂类:
package com.cnblog.clarck;
/**
* 加法工厂
*
* @author clarck
*
*/
public class AddFactory implements IFactory {
@Override
public Operation createOperation() {
return new OperationAdd();
}
}
package com.cnblog.clarck;
/**
* 减法工厂
*
* @author clarck
*
*/
public class SubFactory implements IFactory {
@Override
public Operation createOperation() {
return new OperationSub();
}
}
package com.cnblog.clarck;
/**
* 乘法
*
* @author clarck
*
*/
public class MulFactory implements IFactory {
@Override
public Operation createOperation() {
return new OperationMul();
}
}
package com.cnblog.clarck;
/**
* 除法工厂
*
* @author clarck
*
*/
public class DivFactory implements IFactory {
@Override
public Operation createOperation() {
return new OperationDiv();
}
}
运算符号类:
package com.cnblog.clarck;
/**
* 加法, 减法, 乘法, 除法
*
* @author clarck
*
*/
public enum OperationSymbol {
ADDITION, SUBTRACTION, MULTIPLICATION, DIVISION
}
测试类:
package com.cnblog.clarck;
/**
* 测试类
*
* @author clarck
*
*/
public class Client {
public static void main(String[] args) {
IFactory operFactory = new AddFactory();
Operation operation = operFactory.createOperation();
operation.setNmberA(1);
operation.setNumberB(2);
double result = operation.getResult();
System.out.println("add result: " + result);
operFactory = new SubFactory();
Operation subOperation = operFactory.createOperation();
subOperation.setNmberA(1);
subOperation.setNumberB(2);
double subResult = subOperation.getResult();
System.out.println("sub result: " + subResult);
operFactory = new MulFactory();
Operation mulOperation = operFactory.createOperation();
mulOperation.setNmberA(1);
mulOperation.setNumberB(2);
double mulResult = mulOperation.getResult();
System.out.println("mul result: " + mulResult);
operFactory = new DivFactory();
Operation divOperation = operFactory.createOperation();
divOperation.setNmberA(1);
divOperation.setNumberB(2);
double divResult = divOperation.getResult();
System.out.println("div result: " + divResult);
}
}