设计模式-工厂模式和策略模式的区别

工厂模式和策略模式都是设计模式中的重要组件,它们用于解决不同类型的软件设计问题。以下是它们之间的区别:

  1. 目的不同

    • 工厂模式(Factory Pattern)的主要目的是创建对象。它提供了一种创建对象的接口,使得子类可以决定实例化哪一个类。工厂模式的重点在于如何创建和管理对象。
    • 策略模式(Strategy Pattern)旨在定义一系列算法,将每个算法封装起来,并使它们可以互换。策略模式使得算法可以在不影响客户端的情况下发生变化。它的重点在于如何使用不同的方法或算法来完成某个行为。
  2. 使用场景

    • 工厂模式常用于当我们需要在运行时动态决定创建哪个类的实例,并且这些类拥有共同的接口或基类时。
    • 策略模式适合用于需要动态改变某个对象的行为或者算法,且这些行为可以彼此替换时。
  3. 角色不同

    • 工厂模式包括工厂接口(或抽象类)和具体工厂类,它们负责创建产品对象。
    • 策略模式包含策略接口和具体策略实现,在上下文类中使用这些策略。
  4. 设计意图

    • 工厂模式隐藏了创建对象的复杂性和具体实现,客户端只需通过工厂得到所需对象,而不需要知道对象的创建过程。
    • 策略模式允许客户端在同一算法家族中选择算法,确保算法的变化不会影响到使用算法的客户。

场景:假设我们有一个应用程序,需要通过不同的方式来创建各种类型的文档,比如Word文档、PDF文档、Excel文档。每种文档类型有不同的创建实现。

实现工厂模式

  1. 文档接口:定义一个通用接口或抽象类来表示文档。

    java

    interface Document {
        void open();
    }
    

    Open in:Code Editor

  2. 具体文档类:实现文档接口的不同类。

    java

    class WordDocument implements Document {
        public void open() {
            System.out.println("Opening a Word document.");
        }
    }
    
    class PdfDocument implements Document {
        public void open() {
            System.out.println("Opening a PDF document.");
        }
    }
    

    Open in:Code Editor

  3. 工厂类:通过工厂类提供文档创建的统一接口。

    java

    class DocumentFactory {
        public Document createDocument(String type) {
            if (type.equalsIgnoreCase("Word")) {
                return new WordDocument();
            } else if (type.equalsIgnoreCase("PDF")) {
                return new PdfDocument();
            } else {
                throw new IllegalArgumentException("Unknown document type");
            }
        }
    }
    

    Open in:Code Editor

  4. 使用工厂

    java

    public class Main {
        public static void main(String[] args) {
            DocumentFactory factory = new DocumentFactory();
            Document doc = factory.createDocument("Word");
            doc.open();  // Output: Opening a Word document.
        }
    }
    

    Open in:Code Editor

策略模式案例

场景:假设我们有一个应用程序负责对不同类型的支付进行处理。我们需要支持多种支付策略,例如信用卡支付、支付宝支付和微信支付。

实现策略模式

  1. 支付策略接口:定义一个通用接口来表示支付策略。

    java

    interface PaymentStrategy {
        void pay(int amount);
    }
    

    Open in:Code Editor

  2. 具体策略类:实现支付策略接口的不同类。

    java

    class CreditCardPayment implements PaymentStrategy {
        public void pay(int amount) {
            System.out.println("Paying " + amount + " using Credit Card.");
        }
    }
    
    class AlipayPayment implements PaymentStrategy {
        public void pay(int amount) {
            System.out.println("Paying " + amount + " using Alipay.");
        }
    }
    

    Open in:Code Editor

  3. 上下文类:在上下文类中使用策略接口。

    java

    class PaymentContext {
        private PaymentStrategy strategy;
    
        public PaymentContext(PaymentStrategy strategy) {
            this.strategy = strategy;
        }
    
        public void executePayment(int amount) {
            strategy.pay(amount);
        }
    }
    

    Open in:Code Editor

  4. 使用策略

    java

    public class Main {
        public static void main(String[] args) {
            PaymentContext context = new PaymentContext(new CreditCardPayment());
            context.executePayment(100);  // Output: Paying 100 using Credit Card.
    
            context = new PaymentContext(new AlipayPayment());
            context.executePayment(200);  // Output: Paying 200 using Alipay.
        }
    }
    

    Open in:Code Editor

区别总结

工厂模式关注的是创建过程,而策略模式关注的是行为或算法的变化。它们可以在不同的设计中单独使用,也可以结合使用以实现更复杂的功能。

  • 工厂模式:通过创建工厂类,将对象实例化的责任转移到工厂。当我们不知道需要实例化哪个类时,会使用工厂模式。它隐藏了对象实例化的复杂性。

  • 策略模式:允许在执行时选择不同算法或操作。当我们需要动态地改变对象的行为或者算法时,会使用策略模式。这使得算法可以独立于使用它的客户端而变化。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值