首页 > 其他分享 >策略模式-StrategyPattern-使用案例

策略模式-StrategyPattern-使用案例

时间:2023-05-27 19:12:44浏览次数:52  
标签:sort sorting pattern 模式 strategy 案例 array StrategyPattern class

The Strategy pattern is a behavioral design pattern that allows you to define a family of algorithms, encapsulate each one as a separate class, and make them interchangeable. It enables clients to choose from different algorithms at runtime without tightly coupling the client code to specific implementations.

In real development scenarios, the Strategy pattern is commonly used when you have multiple algorithms or variations of an algorithm that can be used interchangeably. It helps in promoting code reusability, maintainability, and flexibility by encapsulating the algorithms and allowing them to be easily swapped without modifying the client code.

Here's a detailed use case of the Strategy pattern in Java, where we'll implement a sorting algorithm that can be dynamically chosen at runtime.

// Step 1: Define the strategy interface
interface SortingStrategy {
    void sort(int[] array);
}

// Step 2: Implement concrete strategy classes
class BubbleSortStrategy implements SortingStrategy {
    @Override
    public void sort(int[] array) {
        // Implementation of bubble sort algorithm
        // ...
        System.out.println("Bubble sort strategy applied.");
    }
}

class MergeSortStrategy implements SortingStrategy {
    @Override
    public void sort(int[] array) {
        // Implementation of merge sort algorithm
        // ...
        System.out.println("Merge sort strategy applied.");
    }
}

// Step 3: Create a context class that uses the strategy
class SortingContext {
    private SortingStrategy sortingStrategy;

    public void setSortingStrategy(SortingStrategy sortingStrategy) {
        this.sortingStrategy = sortingStrategy;
    }

    public void sortArray(int[] array) {
        sortingStrategy.sort(array);
    }
}

// Step 4: Demonstrate the usage of the strategy pattern
public class Main {
    public static void main(String[] args) {
        int[] array = {5, 2, 7, 1, 3};

        SortingContext context = new SortingContext();

        // Use bubble sort strategy
        context.setSortingStrategy(new BubbleSortStrategy());
        context.sortArray(array);

        // Use merge sort strategy
        context.setSortingStrategy(new MergeSortStrategy());
        context.sortArray(array);
    }
}

  

In this example, we have a SortingStrategy interface that defines the contract for sorting algorithms. We then create two concrete strategy classes, BubbleSortStrategy and MergeSortStrategy, that implement the sorting algorithm using their respective strategies.

The SortingContext class acts as a context and is responsible for sorting the array using the chosen strategy. It has a method setSortingStrategy() to set the strategy dynamically, and sortArray() method that delegates the sorting task to the current strategy.

In the Main class, we demonstrate the usage of the strategy pattern. We create an instance of SortingContext and set the sorting strategy to BubbleSortStrategy, and then invoke the sortArray() method. Similarly, we change the sorting strategy to MergeSortStrategy and invoke the sortArray() method again.

By using the Strategy pattern, we can easily switch between different sorting algorithms without modifying the client code. This allows for flexibility and extensibility in our application, as we can add new strategies or modify existing ones without impacting the existing codebase.

source:https://chat.openai.com

标签:sort,sorting,pattern,模式,strategy,案例,array,StrategyPattern,class
From: https://www.cnblogs.com/rmsimple/p/17437172.html

相关文章

  • Unity的OnOpenAsset:深入解析与实用案例
    UnityOnOpenAsset在Unity中,OnOpenAsset是一个非常有用的回调函数,它可以在用户双击资源文件时自动打开一个编辑器窗口。这个回调函数可以用于自定义资源编辑,提高工作效率。本文将介绍OnOpenAsset的使用方法,并提供三个使用例子。OnOpenAsset的使用方法OnAsset是UnityEditor的一......
  • Unity中的PostProcessBuild:深入解析与实用案例
    Unity中的PostProcessBuild:深入解析与实用案例在Unity游戏开发中,我们经常需要在构建完成后对生成的应用程序进行一些额外的处理。这时,我们可以使用Unity提供的PostProcessBuild功能。本文将详细介绍Unity中的PostProcessBuild方法,并通过三个实用案例来展示其强大的功能。什么是P......
  • MVVM 设计模式
    本篇文章学习于:刘铁猛老师《深入浅出WPF》什么是MVVM模式?MVVM的全称是——Model、View、ViewModel,翻译过来就是:模型、视图、视图模型。ViewModel是比较抽象的,它起到承上启下的作用,用于处理业务逻辑。每一个View都需要有对应的Model和ViewModel。ViewModel与View的沟通:A.传递......
  • Unity中的PostProcessScene:深入解析与实用案例
    Unity中的PostProcessScene:深入解析与实用案例在Unity游戏开发中,我们经常需要对场景进行后处理,以实现更丰富的视觉效果。Unity提供了一个名为PostProcessScene的功能,可以让我们在场景加载完成后,对场景进行一系列的处理。本文将详细介绍PostProcessScene的使用方法,并通过三个实用案......
  • Unity中的RegisterPlugins:深入解析与实用案例
    Unity中的RegisterPlugins:深入解析与实用案例在Unity游戏开发中,我们经常需要使用第三方插件来实现一些特定的功能。为了让这些插件能够在Unity中正常工作,我们需要对它们进行注册。本文将详细介绍Unity中的RegisterPlugins方法,并通过三个实用案例来展示其强大的功能。什么是Regist......
  • Unity中的PostProcessBuild:深入解析与实用案例
    Unity中的PostProcessBuild:深入解析与实用案例在Unity游戏开发中,我们经常需要在构建完成后对生成的应用程序进行一些额外的处理。这时,我们可以使用Unity提供的PostProcessBuild功能。本文将详细介绍Unity中的PostProcessBuild方法,并通过三个实用案例来展示其强大的功能。什么是Po......
  • 结构型——桥接模式
    推荐文档:https://www.cnblogs.com/zhili/p/DesignPatternSummery.htmlhttps://www.runoob.com/design-pattern/design-pattern-tutorial.html什么是桥接模式?桥接(Bridge)是用于把抽象化与实现化解耦,使得二者可以独立变化。这种类型的设计模式属于结构型模式,它通过提供抽象化和......
  • 结构型——装饰器模式
    推荐文档:https://www.cnblogs.com/zhili/p/DesignPatternSummery.htmlhttps://www.runoob.com/design-pattern/design-pattern-tutorial.html什么是装饰器模式?装饰器模式(DecoratorPattern)允许向一个现有的对象添加新的功能,同时又不改变其结构。这种类型的设计模式属于结构型......
  • 结构型——组合模式
    推荐文档:https://www.cnblogs.com/zhili/p/DesignPatternSummery.htmlhttps://www.runoob.com/design-pattern/design-pattern-tutorial.html什么是组合模式?组合模式(CompositePattern),又叫部分整体模式,是用于把一组相似的对象当作一个单一的对象。组合模式依据树形结构来组合......
  • 创建型——工厂模式
    推荐文档:https://www.cnblogs.com/zhili/p/DesignPatternSummery.htmlhttps://www.runoob.com/design-pattern/design-pattern-tutorial.html什么是工厂模式?工厂模式(FactoryPattern)是最常用的设计模式之一。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式......