工厂模式和策略模式区别以及使用

工厂模式和策略模式是两种常见的设计模式,它们有以下区别和使用场景:

区别:

  1. 目的不同:

    • 工厂模式主要用于创建对象,将对象的创建与使用分离,以增加灵活性和可维护性。
    • 策略模式则是用于封装不同的算法或策略,使得它们可以在运行时相互替换。
  2. 关注点不同:

    • 工厂模式关注对象的创建过程。
    • 策略模式关注算法或行为的实现和选择。
  3. 结构不同:

    • 工厂模式通常有一个工厂类,根据不同的条件创建不同的具体产品对象。
    • 策略模式包含多个策略类,以及一个环境类来持有和使用策略对象。

使用场景:

  1. 工厂模式:

    • 当对象的创建逻辑较为复杂,或者需要根据不同的条件创建不同的对象时。
    • 希望隐藏对象创建的细节,提高代码的封装性。
  2. 策略模式:

    • 当一个系统需要在多种算法或策略中进行选择和切换时。
    • 不同的策略之间具有相似性,只是具体的实现方式不同。

例如,在一个电商系统中:

对于创建不同类型的订单(如普通订单、促销订单)可以使用工厂模式。

而对于计算不同的运费策略(按重量、按体积、固定费用等)可以使用策略模式。

以下是一个简单的 Java 工厂模式的代码示例:

首先创建一个产品接口:

interface Product {
    void doSomething();
}

然后创建具体的产品类实现该接口:

class ConcreteProductA implements Product {
    @Override
    public void doSomething() {
        System.out.println("ConcreteProductA is doing something");
    }
}

class ConcreteProductB implements Product {
    @Override
    public void doSomething() {
        System.out.println("ConcreteProductB is doing something");
    }
}

接下来创建工厂类:

class Factory {
    public Product createProduct(String type) {
        if ("A".equalsIgnoreCase(type)) {
            return new ConcreteProductA();
        } else if ("B".equalsIgnoreCase(type)) {
            return new ConcreteProductB();
        }
        return null;
    }
}

最后在客户端使用:

public class Client {
    public static void main(String[] args) {
        Factory factory = new Factory();
        Product productA = factory.createProduct("A");
        Product productB = factory.createProduct("B");

        productA.doSomething();
        productB.doSomething();
    }
}

在上述示例中,通过Factory类来创建不同的具体产品对象,客户端无需关心产品的具体创建过程。

以下是一个使用 Java 实现的策略模式的简单示例,用于计算购物时不同的折扣策略:

interface DiscountStrategy {
    double calculateDiscount(double amount);
}

class PercentageDiscount implements DiscountStrategy {
    private double percentage;

    public PercentageDiscount(double percentage) {
        this.percentage = percentage;
    }

    @Override
    public double calculateDiscount(double amount) {
        return amount * percentage / 100;
    }
}

class FixedAmountDiscount implements DiscountStrategy {
    private double fixedAmount;

    public FixedAmountDiscount(double fixedAmount) {
        this.fixedAmount = fixedAmount;
    }

    @Override
    public double calculateDiscount(double amount) {
        return fixedAmount;
    }
}

class ShoppingCart {
    private DiscountStrategy discountStrategy;

    public ShoppingCart(DiscountStrategy discountStrategy) {
        this.discountStrategy = discountStrategy;
    }

    public double calculateFinalAmount(double totalAmount) {
        double discount = discountStrategy.calculateDiscount(totalAmount);
        return totalAmount - discount;
    }

    public static void main(String[] args) {
        // 使用百分比折扣策略
        DiscountStrategy percentageDiscount = new PercentageDiscount(20);
        ShoppingCart cart1 = new ShoppingCart(percentageDiscount);
        double finalAmount1 = cart1.calculateFinalAmount(100);
        System.out.println("使用百分比折扣后的最终金额: " + finalAmount1);

        // 使用固定金额折扣策略
        DiscountStrategy fixedAmountDiscount = new FixedAmountDiscount(10);
        ShoppingCart cart2 = new ShoppingCart(fixedAmountDiscount);
        double finalAmount2 = cart2.calculateFinalAmount(100);
        System.out.println("使用固定金额折扣后的最终金额: " + finalAmount2);
    }
}

在上述示例中,DiscountStrategy 是策略接口,PercentageDiscount 和 FixedAmountDiscount 是具体的策略实现。ShoppingCart 类使用策略来计算最终金额。

  • 10
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值