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

本文通过一个具体的算术运算示例,介绍了工厂方法模式的基本概念、实现方式及其与简单工厂模式的区别。展示了如何通过不同的工厂创建特定类型的对象,从而实现代码的扩展性和灵活性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

UML图:


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可以很好的避免) 

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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值