CSharp: Command Pattern in donet 6

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
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("--------------------------------------------------");
    }
}

  

调用:

1
2
3
4
5
6
7
using ConsoleApp1;
 
 
// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, geovindu!");
//
StockExecutor.Execute();

  

输出:

1
2
3
4
5
6
Hello, geovindu!
 
股票 DEMO
--------------------------------------------------
股票 [ 名称: GeovinDu 股票, 数量: 1285 ] 是买入.
股票 [ 名称: GeovinDu 股票, 数量: 1285 ] 是卖出.

  

posted @   ®Geovin Du Dream Park™  阅读(23)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
历史上的今天:
2009-11-02 如何使用 Cdosys.dll 库使用ASP发送邮件带附件
2009-11-02 javascript 操作日期 测试(注意浏览器兼容问题)
2009-11-02 如何使用 Cdosys.dll 库使用 Visual C# 中发送电子邮件带有附件接收邮件
2009-11-02 Locale ID (LCID) Chart 区域设置ID
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示