【设计模式】题目小练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());
    }
}
相关推荐
静水流深_沧海一粟11 小时前
04 | 别再写几十个参数的构造函数了——建造者模式
设计模式
StarkCoder11 小时前
从UIKit到SwiftUI的迁移感悟:数据驱动的革命
设计模式
阿星AI工作室19 小时前
给openclaw龙虾造了间像素办公室!实时看它写代码、摸鱼、修bug、写日报,太可爱了吧!
前端·人工智能·设计模式
_哆啦A梦2 天前
Vibe Coding 全栈专业名词清单|设计模式·基础篇(创建型+结构型核心名词)
前端·设计模式·vibecoding
Scout-leaf3 天前
WPF新手村教程(三)—— 路由事件
c#·wpf
用户298698530143 天前
程序员效率工具:Spire.Doc如何助你一键搞定Word表格排版
后端·c#·.net
mudtools4 天前
搭建一套.net下能落地的飞书考勤系统
后端·c#·.net
玩泥巴的4 天前
搭建一套.net下能落地的飞书考勤系统
c#·.net·二次开发·飞书
唐宋元明清21884 天前
.NET 本地Db数据库-技术方案选型
windows·c#
郑州光合科技余经理4 天前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php