打游戏时,有三种游戏输入设备(键盘、Xbox手柄、PS4手柄)供玩家选择,玩家根据自身条件选择某一设备进行游戏:
// 游戏输入设备(抽象策略类):
// ------------------------
public abstract class InputMode
{
public abstract void Play();
}
// 具体游戏输入设备(具体策略类):
// ---------------------------
public class Keyboard : InputMode
{
public override void Play()
{
Console.WriteLine("使用 Keyboard 进行游戏!");
}
}
public class Xbox : InputMode
{
public override void Play()
{
Console.WriteLine("使用 Xbox 进行游戏!");
}
}
public class PS4 : InputMode
{
public override void Play()
{
Console.WriteLine("使用 PS4 进行游戏!");
}
}
// Context对象:随着策略对象改变而改变
// --------------------------------
public class PlayContext
{
//当前游戏输入设备(策略实现对象)
InputMode myInputMode;
//构造函数:初始化当前游戏输入设备
public PlayContext(InputMode inputMode)
{
this.myInputMode = inputMode;
}
//调用游戏输入设备类中的方法
public void PlayInTheMode()
{
myInputMode.Play();
}
}
// 玩家(客户端):
// --------------
class Player
{
static void Main(string[] args)
{
PlayContext playContext;
playContext = new PlayContext(new Keyboard());
playContext.PlayInTheMode(); //使用 Keyboard 进行游戏!
playContext = new PlayContext(new Xbox());
playContext.PlayInTheMode(); //使用 Xbox 进行游戏!
playContext = new PlayContext(new PS4());
playContext.PlayInTheMode(); //使用 PS4 进行游戏!
}
}
原文:https://www.cnblogs.com/SouthBegonia/p/11971521.html