Skip to content

Commit c99cac3

Browse files
committed
add new Command pattern example
1 parent 4a78942 commit c99cac3

File tree

14 files changed

+256
-11
lines changed

14 files changed

+256
-11
lines changed

.vscode/settings.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"files.exclude": {
3+
"**/.git": true,
4+
"**/.svn": true,
5+
"**/.hg": true,
6+
"**/CVS": true,
7+
"**/.DS_Store": true,
8+
"**/Thumbs.db": true,
9+
"**/bin": true,
10+
"**/obj": true
11+
}
12+
}

CommandPattern3/CommandPattern3.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>netcoreapp3.1</TargetFramework>
5+
<TargetFramework>NET8</TargetFramework>
66
</PropertyGroup>
77

88
</Project>

CommandPattern3/Program.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ static void Main(string[] args)
2323
string input = Console.ReadLine();
2424
Command command = CommandFactory.CreateCommand(input.ToLower(), Session);
2525

26-
if (command == null) continue;
27-
2826
Session.ExecuteCommand(command);
2927

3028
name = command.Name;

CommandPattern3/Session.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace CommandPattern3
99
/// </summary>
1010
public class Session
1111
{
12-
protected readonly List<string> history = new List<string>();
12+
protected readonly List<string> history = new();
1313

1414
public Session(Terminal terminal)
1515
{

CommandPattern3/Terminal.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,20 @@
66
namespace CommandPattern3
77
{
88
/// <summary>
9-
/// Reciever, who recive commands and do some job
9+
/// Receiver, who receives commands and do some job
1010
/// </summary>
1111
public class Terminal : IDisposable
1212
{
13-
private readonly Stack<Command> stack;
13+
private readonly Stack<Command> stack;
1414

15-
public readonly Stopwatch Uptime;
15+
public readonly Stopwatch Uptime;
1616

1717
public Terminal()
1818
{
1919
Uptime = new Stopwatch();
2020
Uptime.Start();
21-
22-
stack = new Stack<Command>();
21+
22+
stack = new Stack<Command>();
2323
}
2424

2525
public Command PopCommand()
@@ -33,7 +33,7 @@ public Command PopCommand()
3333
{
3434
Console.WriteLine(e.Message);
3535
return null;
36-
}
36+
}
3737
}
3838

3939
public void PushCommand(Command cmd)
@@ -49,7 +49,7 @@ public string GetCommands()
4949

5050
public void Dispose()
5151
{
52-
Uptime.Stop();
52+
Uptime.Stop();
5353
}
5454
}
5555
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
</Project>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
namespace CommandPattern4.Commands;
2+
3+
using CommandPattern4.DataStore;
4+
5+
internal class AddUserCommand : Command
6+
{
7+
private readonly IUserStore _userStore;
8+
private readonly User _user;
9+
10+
public AddUserCommand(User user, CommandContext context)
11+
{
12+
_userStore = context.DataStore ?? throw new ArgumentNullException(nameof(context.DataStore));
13+
_user = user ?? throw new ArgumentNullException(nameof(user));
14+
}
15+
16+
public override void Execute()
17+
{
18+
_userStore.AddUser(_user);
19+
}
20+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using CommandPattern4.DataStore;
2+
3+
internal abstract class Command
4+
{
5+
public abstract void Execute();
6+
}
7+
8+
internal class CommandContext
9+
{
10+
public IUserStore DataStore { get; }
11+
12+
public CommandContext(IUserStore dataStore)
13+
{
14+
DataStore = dataStore;
15+
}
16+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
namespace CommandPattern4.Commands;
2+
3+
using CommandPattern4.DataStore;
4+
5+
internal class DisplayUsersCommand : Command
6+
{
7+
private readonly IUserStore _userStore;
8+
9+
public DisplayUsersCommand(CommandContext context)
10+
{
11+
_userStore = context.DataStore ?? throw new ArgumentNullException(nameof(context.DataStore));
12+
}
13+
14+
public override void Execute()
15+
{
16+
var users = _userStore.GetUsers();
17+
users.ToList().ForEach(u => Console.WriteLine(u.Username));
18+
}
19+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
namespace CommandPattern4.Commands;
2+
3+
using CommandPattern4.DataStore;
4+
5+
internal class RemoveUserCommand : Command
6+
{
7+
private readonly IUserStore _userStore;
8+
private readonly string _username;
9+
10+
public RemoveUserCommand(string username, CommandContext context)
11+
{
12+
ArgumentNullException.ThrowIfNull(username, nameof(username));
13+
_userStore = context.DataStore;
14+
_username = username;
15+
}
16+
17+
public override void Execute()
18+
{
19+
_userStore.RemoveUser(_username);
20+
}
21+
}

0 commit comments

Comments
 (0)