【设计模式】题目小练2

// 题1. AI 对话系统

// 你要做一个 NPC 对话系统:

// 不同 NPC 有不同的对话脚本(村民、商人、战士)。

// 同时对话还会受玩家状态影响(任务是否完成、声望高低)。

// 👉 请结合 模板方法模式 和 策略模式,设计一下系统结构。

cs 复制代码
using System;

// 抽象策略
public interface ISpeakStrategy
{
    string Execute();
}

// 具体策略
public class RespectStrategy : ISpeakStrategy
{
    public string Execute() => "尊敬";
}
public class StrangeStrategy : ISpeakStrategy
{
    public string Execute() => "陌生";
}
public class SeriousStrategy : ISpeakStrategy
{
    public string Execute() => "严肃";
}
public class AppreciateStrategy : ISpeakStrategy
{
    public string Execute() => "赞赏";
}

// 模板方法模式 ------ NPC基类
public abstract class NPC
{
    // 模板方法:定义固定流程
    public void Talk(ISpeakStrategy strategy)
    {
        Greet();
        CoreDialogue(strategy);
        Farewell();
    }

    protected abstract void Greet();                    // 打招呼
    protected abstract void CoreDialogue(ISpeakStrategy strategy);  // 核心对话
    protected abstract void Farewell();                 // 告别
}

// 具体NPC
public class Villager : NPC
{
    protected override void Greet() => Console.WriteLine("村民:你好啊!");
    protected override void CoreDialogue(ISpeakStrategy strategy)
        => Console.WriteLine($"村民用「{strategy.Execute()}」的语气交谈。");
    protected override void Farewell() => Console.WriteLine("村民:路上小心!");
}

public class Merchant : NPC
{
    protected override void Greet() => Console.WriteLine("商人:客官请进。");
    protected override void CoreDialogue(ISpeakStrategy strategy)
        => Console.WriteLine($"商人用「{strategy.Execute()}」的语气讨价还价。");
    protected override void Farewell() => Console.WriteLine("商人:欢迎下次光临!");
}

public class Warrior : NPC
{
    protected override void Greet() => Console.WriteLine("战士:哼,你找我吗?");
    protected override void CoreDialogue(ISpeakStrategy strategy)
        => Console.WriteLine($"战士用「{strategy.Execute()}」的语气交流。");
    protected override void Farewell() => Console.WriteLine("战士:走好,不要添麻烦。");
}

// 玩家状态决定策略
public class Player
{
    public bool IsCompletedTask { get; set; }
    public float Reputation { get; set; }

    public ISpeakStrategy ChooseStrategyByTask()
        => IsCompletedTask ? new SeriousStrategy() : new AppreciateStrategy();

    public ISpeakStrategy ChooseStrategyByReputation()
        => Reputation >= 60f ? new RespectStrategy() : new StrangeStrategy();
}

// 客户端测试
public class Client
{
    public static void Main()
    {
        Player player = new Player { IsCompletedTask = true, Reputation = 80 };

        NPC villager = new Villager();
        NPC merchant = new Merchant();
        NPC warrior = new Warrior();

        villager.Talk(player.ChooseStrategyByReputation());
        merchant.Talk(player.ChooseStrategyByReputation());
        warrior.Talk(player.ChooseStrategyByTask());
    }
}
相关推荐
liulilittle20 分钟前
C++ 并发双阶段队列设计原理与实现
linux·开发语言·c++·windows·算法·线程·并发
c#上位机29 分钟前
halcon图像去噪—高斯滤波
c#·上位机·halcon·机器视觉
lly20240638 分钟前
并查集快速查找
开发语言
繁星蓝雨43 分钟前
我与C++的故事(杂谈)
开发语言·c++
除了代码啥也不会44 分钟前
Java基于SSE流式输出实战
java·开发语言·交互
Jacob程序员1 小时前
欧几里得距离算法-相似度
开发语言·python·算法
Slow菜鸟1 小时前
Java项目基础架构(二)| 通用响应与异常
java·开发语言
LQxdp1 小时前
复现-[Java Puzzle #2 WP] HEAD权限绕过与字符截断CRLF
java·开发语言·漏洞复现·java 代码审计
克喵的水银蛇1 小时前
Flutter 弹性布局实战:快速掌握 Row/Column/Flex 核心用法
开发语言·javascript·flutter
sztian681 小时前
JavaScript---BOM对象、JS执行机制、location对象
开发语言·前端·javascript