CSharp: Command Pattern in donet 6
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApp1; /// <summary> /// 股票 /// </summary> public class Stock { /// <summary> /// 名称 /// </summary> private readonly string _name; /// <summary> /// 数量 /// </summary> private readonly int _quantity; /// <summary> /// /// </summary> /// <param name="name"></param> /// <param name="quantity"></param> public Stock(string name, int quantity) { _name = name; _quantity = quantity; } /// <summary> /// 买 /// </summary> public void Buy() => Console.WriteLine($"股票 [ 名称: {_name}, 数量: {_quantity} ] 是买入."); /// <summary> /// 卖 /// </summary> public void Sell() => Console.WriteLine($"股票 [ 名称: {_name}, 数量: {_quantity} ] 是卖出."); } using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApp1; /// <summary> /// 经纪人 /// </summary> public class Broker { /// <summary> /// /// </summary> private readonly List<IOrderCommand> _orders = new(); /// <summary> /// /// </summary> /// <param name="order"></param> public void TakeOrder(IOrderCommand order) => _orders.Add(order); /// <summary> /// /// </summary> public void ProcessOrders() { foreach (var order in _orders) { order.Execute(); } _orders.Clear(); } } using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApp1; /// <summary> /// /// </summary> public interface IOrderCommand { /// <summary> /// 处理 /// </summary> void Execute(); } using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApp1; /// <summary> /// 买 /// </summary> public class BuyStockCommand : IOrderCommand { /// <summary> /// /// </summary> private readonly Stock _stock; /// <summary> /// /// </summary> /// <param name="stock"></param> public BuyStockCommand(Stock stock) { _stock = stock; } /// <summary> /// /// </summary> public void Execute() => _stock.Buy(); } using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApp1; /// <summary> /// 卖 /// </summary> public class SellStockCommand : IOrderCommand { /// <summary> /// /// </summary> private readonly Stock _stock; /// <summary> /// /// </summary> /// <param name="stock"></param> public SellStockCommand(Stock stock) { _stock = stock; } /// <summary> /// /// </summary> public void Execute() => _stock.Sell(); } using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApp1; /// <summary> /// /// </summary> public static class StockExecutor { /// <summary> /// Command Pattern /// geovindu,Geovin Du,涂聚文 /// /// </summary> public static void Execute() { ConsoleExtension.WriteSeparator("股票 DEMO"); // var stock = new Stock("GeovinDu 股票", 1285); // var buyStock = new BuyStockCommand(stock); var sellStock = new SellStockCommand(stock); var broker = new Broker(); broker.TakeOrder(buyStock); broker.TakeOrder(sellStock); broker.ProcessOrders(); } } using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApp1; /// <summary> /// /// </summary> public static class ConsoleExtension { /// <summary> /// 显标题 /// </summary> /// <param name="title"></param> public static void WriteSeparator(string title) { Console.WriteLine(); Console.WriteLine(title); Console.WriteLine("--------------------------------------------------"); } }
调用:
using ConsoleApp1; // See https://aka.ms/new-console-template for more information Console.WriteLine("Hello, geovindu!"); // StockExecutor.Execute();
输出:
Hello, geovindu! 股票 DEMO -------------------------------------------------- 股票 [ 名称: GeovinDu 股票, 数量: 1285 ] 是买入. 股票 [ 名称: GeovinDu 股票, 数量: 1285 ] 是卖出.
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)