zoukankan      html  css  js  c++  java
  • 设计模式》命令模式

    例子

    
        public class CommandLineArgs
        {
            public CommandLineArgs(string command, params string[] args)
            {
                Command = command;
                Args = args;
            }
    
            public string Command { get; }
    
            public string[] Args { get; }
        }
    
    
        public interface IConsoleCommand
        {
            void ExecuteAsync(CommandLineArgs commandLineArgs);
        }
    
    
        public class GodConsoleCommand : IConsoleCommand
        {
            public void ExecuteAsync(CommandLineArgs commandLineArgs)
            {
                Console.WriteLine($"给{commandLineArgs.Args[0]}增加无敌BUFF");
            }
        }
    
        public class KillConsoleCommand : IConsoleCommand
        {
            public void ExecuteAsync(CommandLineArgs commandLineArgs)
            {
                Console.WriteLine($"杀死{commandLineArgs.Args[0]}");
            }
        }
    
        public class ReviveConsoleCommand : IConsoleCommand
        {
            public void ExecuteAsync(CommandLineArgs commandLineArgs)
            {
                Console.WriteLine($"复活{commandLineArgs.Args[0]}");
            }
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                var arg = args[1];
                var common = new CommandLineArgs(args[0], arg);
                if (args[0] == "god")
                {
                    new GodConsoleCommand().ExecuteAsync(common);
                }
    
                if (args[0] == "kill")
                {
                    new KillConsoleCommand().ExecuteAsync(common);
                }
    
                if (args[0] == "revive")
                {
                    new ReviveConsoleCommand().ExecuteAsync(common);
                }
            }
        }
    
    

    控制台命令:

    • CommandPattern god uid 无敌
    • CommandPattern kill uid 杀死
    • CommandPattern revive uid 复活
  • 相关阅读:
    Python Web学习笔记之Python多线程基础
    Python入门之python可变对象与不可变对象
    Python Web学习笔记之SOCK_STREAM和SOCK_DGRAM
    background和background-position相关笔记
    自定义switch开关
    获取浏览器类型和版本号
    随机生成字符串
    white-space详解
    文件选择按钮随笔
    mouse的各种事件
  • 原文地址:https://www.cnblogs.com/icxldd/p/15803712.html
Copyright © 2011-2022 走看看