Skip to content

Commit d03ac39

Browse files
committed
Update command pattern examples
1 parent 117df78 commit d03ac39

27 files changed

+99
-183
lines changed

src/BehavioralPatterns/Command/CommandLibrary/EmailExample/Commands/Common/ICommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
public interface ICommand
44
{
55
void Execute();
6-
}
6+
}

src/BehavioralPatterns/Command/CommandLibrary/EmailExample/Commands/DeleteCommand.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,5 @@ public DeleteCommand(Email email)
1111
_email = email;
1212
}
1313

14-
public void Execute()
15-
{
16-
_email.Delete();
17-
}
18-
}
14+
public void Execute() => _email.Delete();
15+
}

src/BehavioralPatterns/Command/CommandLibrary/EmailExample/Commands/ForwardCommand.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,5 @@ public ForwardCommand(Email email)
1111
_email = email;
1212
}
1313

14-
public void Execute()
15-
{
16-
_email.Forward();
17-
}
18-
}
14+
public void Execute() => _email.Forward();
15+
}

src/BehavioralPatterns/Command/CommandLibrary/EmailExample/Commands/ReadCommand.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,5 @@ public ReadCommand(Email email)
1111
_email = email;
1212
}
1313

14-
public void Execute()
15-
{
16-
_email.Read();
17-
}
18-
}
14+
public void Execute() => _email.Read();
15+
}

src/BehavioralPatterns/Command/CommandLibrary/EmailExample/Email.cs

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,9 @@
22

33
public class Email
44
{
5-
public void Delete()
6-
{
7-
Console.WriteLine("Email has been deleted.");
8-
}
5+
public void Delete() => Console.WriteLine("Email has been deleted.");
96

10-
public void Forward()
11-
{
12-
Console.WriteLine("Email has been forwarded.");
13-
}
7+
public void Forward() => Console.WriteLine("Email has been forwarded.");
148

15-
public void Read()
16-
{
17-
Console.WriteLine("Email has been read.");
18-
}
19-
}
9+
public void Read() => Console.WriteLine("Email has been read.");
10+
}

src/BehavioralPatterns/Command/CommandLibrary/EmailExample/EmailExecutor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@ public static void Execute()
2121
toolbar.ForwardEmail();
2222
toolbar.DeleteEmail();
2323
}
24-
}
24+
}

src/BehavioralPatterns/Command/CommandLibrary/EmailExample/Toolbar.cs

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,9 @@ public Toolbar(ICommand readCommand, ICommand forwardCommand, ICommand deleteCom
1515
_deleteCommand = deleteCommand;
1616
}
1717

18-
public void ReadEmail()
19-
{
20-
_readCommand.Execute();
21-
}
18+
public void ReadEmail() => _readCommand.Execute();
2219

23-
public void ForwardEmail()
24-
{
25-
_forwardCommand.Execute();
26-
}
20+
public void ForwardEmail() => _forwardCommand.Execute();
2721

28-
public void DeleteEmail()
29-
{
30-
_deleteCommand.Execute();
31-
}
32-
}
22+
public void DeleteEmail() => _deleteCommand.Execute();
23+
}

src/BehavioralPatterns/Command/CommandLibrary/Executor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@ public override void Execute()
1515
ShoppingCartExecutor.Execute();
1616
StockExecutor.Execute();
1717
}
18-
}
18+
}

src/BehavioralPatterns/Command/CommandLibrary/ShoppingCartExample/CommandManager.cs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,27 @@ namespace CommandLibrary.ShoppingCartExample;
44

55
public class CommandManager
66
{
7-
private readonly Stack<ICommand> _commands = new Stack<ICommand>();
7+
private readonly Stack<ICommand> _commands = new();
88

99
public void Invoke(ICommand command)
1010
{
11-
if (command.CanExecute())
11+
if (!command.CanExecute())
1212
{
13-
_commands.Push(command);
14-
command.Execute();
13+
return;
1514
}
15+
16+
_commands.Push(command);
17+
command.Execute();
1618
}
1719

1820
public void Undo()
1921
{
22+
if (!_commands.Any())
23+
{
24+
return;
25+
}
26+
2027
var command = _commands.Pop();
2128
command.Undo();
2229
}
23-
}
30+
}

src/BehavioralPatterns/Command/CommandLibrary/ShoppingCartExample/Commands/AddToCartCommand.cs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,7 @@ public AddToCartCommand(
2020
_shoppingCartRepository = shoppingCartRepository;
2121
}
2222

23-
public bool CanExecute()
24-
{
25-
if (_product == null)
26-
{
27-
return false;
28-
}
29-
30-
return _productRepository.GetStock(_product.ProductId) > 0;
31-
}
23+
public bool CanExecute() => _productRepository.GetStock(_product.ProductId) > 0;
3224

3325
public void Execute()
3426
{
@@ -43,4 +35,4 @@ public void Undo()
4335
_productRepository.IncreaseStock(_product.ProductId, lineItem.Quantity);
4436
_shoppingCartRepository.Remove(_product.ProductId);
4537
}
46-
}
38+
}

0 commit comments

Comments
 (0)