1 简单工厂模式
1.1 类的结构图
1.2 代码实现
package edu.slxy.simplyfactory;
public class Operation {
private double _numberA = 0;
private double _numberB = 0;
public double get_numberA() {
return _numberA;
}
public void set_numberA(double _numberA) {
this._numberA = _numberA;
}
public double get_numberB() {
return _numberB;
}
public void set_numberB(double _numberB) {
this._numberB = _numberB;
}
public double GetResult(){
double result = 0;
return result;
}
}
// 加法类
class OperationAdd extends Operation{
public double GetResult(){
double result = 0;
result = get_numberA() + get_numberB();
return result;
}
}
// 减法类
class OperationSub extends Operation{
public double GetResult(){
double result = 0;
result = get_numberA() - get_numberB();
return result;
}
}
// 乘法类
class OperationMul extends Operation{
public double GetResult(){
double result = 0;
result = get_numberA() * get_numberB();
return result;
}
}
// 除法类
class OperationDiv extends Operation{
public double GetResult(){
double result = 0;
if (get_numberB() == 0)
try {
throw new Exception("除数不能为0");
} catch (Exception e) {
e.printStackTrace();
}
result = get_numberA()/get_numberB();
return result;
}
}
简单运算工厂类
package edu.slxy.simplyfactory;
public class OperationFactory {
public static Operation createOperation(String operate){
Operation oper = null;
switch(operate){
case "+":
oper = new OperationAdd();
break;
case "-":
oper = new OperationSub();
break;
case "*":
oper = new OperationMul();
break;
case "/":
oper = new OperationDiv();
break;
}
return oper;
}
public static void main(String[] args) {
//如果三个加法对象要修改为减法,需要修改三处
Operation oper;
oper = OperationFactory.createOperation("+");
oper.set_numberA(1);
oper.set_numberB(2);
double result = oper.GetResult();
System.out.println(result);
Operation oper1;
oper1 = OperationFactory.createOperation("+");
oper1.set_numberA(3);
oper1.set_numberB(4);
double result1 = oper1.GetResult();
System.out.println(result1);
Operation oper2;
oper2 = OperationFactory.createOperation("+");
oper2.set_numberA(3);
oper2.set_numberB(4);
double result2 = oper1.GetResult();
System.out.println(resul2);
}
}
2 工厂方法模式
2.1 定义
工厂方法模型 :定义一个用于创建对象的接口,让子类决定实例化哪一个类。工厂方法是一个类的实例化延迟到其子类。
2.2 类的结构图
2.3 代码实现
package edu.slxy.factorymethod;
public class Operation {
private double _numberA = 0;
private double _numberB = 0;
public double get_numberA() {
return _numberA;
}
public void set_numberA(double _numberA) {
this._numberA = _numberA;
}
public double get_numberB() {
return _numberB;
}
public void set_numberB(double _numberB) {
this._numberB = _numberB;
}
public double GetResult(){
double result = 0;
return result;
}
}
// 加法类
class OperationAdd extends Operation {
public double GetResult(){
double result = 0;
result = get_numberA() + get_numberB();
return result;
}
}
// 减法类
class OperationSub extends Operation {
public double GetResult(){
double result = 0;
result = get_numberA() - get_numberB();
return result;
}
}
// 乘法类
class OperationMul extends Operation {
public double GetResult(){
double result = 0;
result = get_numberA() * get_numberB();
return result;
}
}
// 除法类
class OperationDiv extends Operation {
public double GetResult(){
double result = 0;
if (get_numberB() == 0)
try {
throw new Exception("除数不能为0");
} catch (Exception e) {
e.printStackTrace();
}
result = get_numberA()/get_numberB();
return result;
}
}
package edu.slxy.factorymethod;
public interface IFactory {
//工厂接口
Operation CreateOperation();
}
// 加减乘除各建一个具体的工厂去实现这个接口
class AddFactory implements IFactory{
@Override
public Operation CreateOperation() {
return new OperationAdd();
}
}
class SubFactory implements IFactory{
@Override
public Operation CreateOperation() {
return new OperationSub();
}
}
class MulFactory implements IFactory{
@Override
public Operation CreateOperation() {
return new OperationMul();
}
}
class DivFactory implements IFactory{
@Override
public Operation CreateOperation() {
return new OperationDiv();
}
}
//客户端
public class OperationTest {
public static void main(String[] args) {
//如果要修改减法运算只需要在这里 修改一次创建对象。
IFactory factory = new AddFactory();
Operation oper = factory.CreateOperation();
oper.set_numberA(1);
oper.set_numberB(2);
double result = oper.GetResult();
System.out.println("result : " + result);
Operation oper1 = factory.CreateOperation();
oper1.set_numberA(1);
oper1.set_numberB(2);
double result1 = oper1.GetResult();
System.out.println("result : " + result1);
}
}
/**
* 总结
* 简单工厂模式
* 1. 如果有需求更改加法的算法,要到OperationAdd就可以。
* 2. 现在添加一些复杂的运算,比如平方根,立方根等
* 添加对应的相关子类,and 还需修改运算类工厂 在switch中增加分支。
* 修改原有的类是违反开放-封闭原则。
*/