设计模式学习

设计模式学习

策略模式

策略模式适用于以下场景:

  1. 对象有多种行为或算法,需要根据不同情况选择不同的算法。
  2. 系统中有多个类实现相同的接口或继承相同的抽象类,但具体实现不同。
  3. 需要在运行时动态地添加、删除或切换算法,而不影响客户端代码。
  4. 一个类有多种变形或状态,每个状态有不同的行为,需要根据状态动态改变对象的行为。

第一步:定义策略接口

/**
 * 打游戏策略接口
 * 策略接口定义行为
 * @author wangpeng
 * @ClassName PlayGameStategy
 * @description
 * @date 2024/4/24 16:39
 */
public interface PlayGameStategy {
	/**
     * 打游戏
     * @return
     */
    String playGame();
}

第二步:定义上下文类

/**
 * 上下文
 * @author wangpeng
 * @ClassName PlayGameContext
 * @date 2024/4/24 16:40
 */

public class PlayGameContext {
    private PlayGameStategy playGameStategy;

    public PlayGameContext(PlayGameStategy playGameStategy) {
        this.playGameStategy = playGameStategy;
    }

    public String playGame() {
        return playGameStategy.playGame();
    }

}

第三步:创建要打的游戏类,LOL、Pubg

/**
 * @author wangpeng
 * @ClassName Pubg
 * @description TODO
 * @date 2024/4/24 16:43
 */

public class Lol implements PlayGameStategy {


    @Override
    public String playGame() {
        return "LoL启动!";
    }
}

/**
 * @author wangpeng
 * @ClassName Pubg
 * @description TODO
 * @date 2024/4/24 16:43
 */

public class Pubg implements PlayGameStategy {
    @Override
    public String playGame() {
        return "pubg启动!";
    }
}

第四步:调用

/**
 * @author wangpeng
 * @ClassName TestController
 * @description TODO
 * @date 2024/4/24 16:44
 */
@RestController
@RequestMapping("/test")
public class FuckController {



    @GetMapping("/pubg")
    @Anonymous
    public String playPubg(String game) {
        Pubg pubg = new Pubg();
        PlayGameContext playGameContext = new PlayGameContext(pubg);
        return  playGameContext.playGame();
    }

    @GetMapping("/lol")
    @Anonymous
    public String playLoL(String game) {
        Lol lol = new Lol();
        PlayGameContext playGameContext = new PlayGameContext(lol);
        return playGameContext.playGame();
    }
}

最后效果
在这里插入图片描述
在这里插入图片描述

相关推荐

  1. 设计模式学习总结

    2024-04-28 19:50:02       56 阅读
  2. 设计模式学习笔记

    2024-04-28 19:50:02       40 阅读
  3. 设计模式学习1

    2024-04-28 19:50:02       30 阅读
  4. 设计模式学习(八)——《大话设计模式

    2024-04-28 19:50:02       36 阅读
  5. 23种设计模式学习

    2024-04-28 19:50:02       54 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-28 19:50:02       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-28 19:50:02       106 阅读
  3. 在Django里面运行非项目文件

    2024-04-28 19:50:02       87 阅读
  4. Python语言-面向对象

    2024-04-28 19:50:02       96 阅读

热门阅读

  1. C++并发编程 - 线程管理

    2024-04-28 19:50:02       36 阅读
  2. DreamFusion:深入解读其原理、功能与应用

    2024-04-28 19:50:02       30 阅读
  3. 一种基于LLM的辅助教学方法与流程

    2024-04-28 19:50:02       26 阅读
  4. ubuntu安装源问题

    2024-04-28 19:50:02       27 阅读
  5. 费曼学习法个人总结-1

    2024-04-28 19:50:02       31 阅读
  6. 【Linux驱动层】iTOP-RK3568学习之路(六):定时器

    2024-04-28 19:50:02       29 阅读
  7. 一般数组队列(具有伪溢出的队列)

    2024-04-28 19:50:02       29 阅读
  8. C程序调用C++函数,以及C++调用C函数

    2024-04-28 19:50:02       27 阅读
  9. IP地址的地理位置如何确定?

    2024-04-28 19:50:02       32 阅读