-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommands.cs
More file actions
95 lines (85 loc) · 2.45 KB
/
Commands.cs
File metadata and controls
95 lines (85 loc) · 2.45 KB
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
using System;
using System.Collections.Generic;
using System.Text;
namespace StateAndCommand
{
// Commands here, implementing ICommand for the Command Pattern :)
// They basically just call the Method of the Game, as the Error handling is implemented in the States from the earlier Exercise anyway.
public class BuyCommand : ICommand
{
private Game _game;
public BuyCommand(Game game)
{
_game = game;
}
public void Execute()
{
_game.BuyGame();
}
}
public class InstallCommand : ICommand
{
private Game _game;
public InstallCommand(Game game)
{
_game = game;
}
public void Execute()
{
_game.InstallGame();
}
}
public class DeleteCommand : ICommand
{
private Game _game;
public DeleteCommand(Game game)
{
_game = game;
}
public void Execute()
{
_game.DeleteGame();
}
}
public class PlayCommand : ICommand
{
private Game _game;
public PlayCommand(Game game)
{
_game = game;
}
public void Execute()
{
_game.StartGame();
}
}
// Composite Command "OneClickPlay" allows for one Execution Method to Buy, Install, AND Start a game.
// If the Game is already owned, buying will be skipped, as is the case for when it's already installed
public class OneClickPlay : CompositeCommand
{
private Game _game;
public OneClickPlay(Game game)
{
_game = game;
if (_game.State.GetType() == typeof(UnownedGame))
{
AddChild(new BuyCommand(_game));
AddChild(new InstallCommand(_game));
AddChild(new PlayCommand(_game));
}
else if (_game.State.GetType() == typeof(BoughtGame))
{
AddChild(new InstallCommand(_game));
AddChild(new PlayCommand(_game));
}
else if (_game.State.GetType() == typeof(InstalledGame))
{
AddChild(new PlayCommand(_game));
}
else
{
Console.WriteLine("The Game is already running. Nothing happens.");
}
}
}
}