5、工厂方法模式—计算器操作

工厂方法模式(Factory Method):定义一个创建对象的接口,让子类决子类定实例化哪个类。工厂方法使一个类的实例化延迟到其子类。

UML图:

5、工厂方法模式—计算器操作_第1张图片

package com.thpin.repository.designpattern;

public class FactoryMethodDemo {
    public static void main(String[] args) {
        int result = -1;
        IFactory factory = null;
        Operation operation = null;
        
        factory = new OperationAddFactory();
        operation = factory.createOperation();
        result = operation.getResult(5, 2);
        System.out.println("5 + 2 = " + result);
        
        factory = new OperationSubFactory();
        operation = factory.createOperation();
        result = operation.getResult(5, 2);
        System.out.println("5 - 2 = " + result);
        
        factory = new OperationMulFactory();
        operation = factory.createOperation();
        result = operation.getResult(5, 2);
        System.out.println("5 * 2 = " + result);
        
        factory = new OperationDivFactory();
        operation = factory.createOperation();
        result = operation.getResult(5, 2);
        System.out.println("5 / 2 = " + result);
        
        factory = new OperationModFactory();
        operation = factory.createOperation();
        result = operation.getResult(5, 2);
        System.out.println("5 % 2 = " + result);
    }
}

/*
 * 工厂接口
 */
interface IFactory {
    Operation createOperation();
}

/*
 * 加法操作工厂
 */
class OperationAddFactory implements IFactory {
    public Operation createOperation() {
        return new OperationAdd();
    }
}

/*
 * 减法操作工厂
 */
class OperationSubFactory implements IFactory {
    public Operation createOperation() {
        return new OperationSub();
    }
}

/*
 * 乘法操作工厂
 */
class OperationMulFactory implements IFactory {
    public Operation createOperation() {
        return new OperationMul();
    }
}

/*
 * 除法操作工厂
 */
class OperationDivFactory implements IFactory {
    public Operation createOperation() {
        return new OperationDiv();
    }
}

/*
 * 取模操作工厂
 */
class OperationModFactory implements IFactory {
    public Operation createOperation() {
        return new OperationMod();
    }
}

abstract class Operation {
    public abstract int getResult(int a, int b);
}

/*
 * 加法操作
 */
class OperationAdd extends Operation {
    public int getResult(int a, int b) {
        return a + b;
    }
}

/*
 * 减法操作
 */
class OperationSub extends Operation {
    public int getResult(int a, int b) {
        return a - b;
    }
}

/*
 * 乘法操作
 */
class OperationMul extends Operation {
    public int getResult(int a, int b) {
        return a * b;
    }
}

/*
 * 除法操作
 */
class OperationDiv extends Operation {
    public int getResult(int a, int b) {
        return a / b;
    }
}

/*
 * 取模操作
 */
class OperationMod extends Operation {
    public int getResult(int a, int b) {
        return a % b;
    }
}

结果:

5 + 2 = 7
5 - 2 = 3
5 * 2 = 10

5 / 2 = 2

5 % 2 = 1


    简单工厂模式违背开闭原则,需要工厂根据传入字符串来判断创建对象,而新增产品时会修改工厂的判断代码。(这一点利用反射+Properties可以很好的避免) 

    工厂方法模式是简单工厂模式的进一步抽象和推广,利用多态性只需要替换客户端的工厂实现类。工厂方法模式保持了简单工厂模式的优点,避免了修改工厂代码的缺点。但同时也避免不了增加了代码开发量。

你可能感兴趣的:(设计模式)