-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathProgram.cs
68 lines (58 loc) · 1.13 KB
/
Program.cs
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
using System;
namespace ProxyPattern
{
interface IDoor
{
void Open();
void Close();
}
class LabDoor : IDoor
{
public void Close()
{
Console.WriteLine("Closing lab door");
}
public void Open()
{
Console.WriteLine("Opening lab door");
}
}
class SecuredDoor
{
private IDoor mDoor;
public SecuredDoor(IDoor door)
{
mDoor = door ?? throw new ArgumentNullException("door", "door can not be null");
}
public void Open(string password)
{
if (Authenticate(password))
{
mDoor.Open();
}
else
{
Console.WriteLine("Big no! It ain't possible.");
}
}
private bool Authenticate(string password)
{
return password == "$ecr@t" ? true : false;
}
public void Close()
{
mDoor.Close();
}
}
class Program
{
static void Main(string[] args)
{
var door = new SecuredDoor(new LabDoor());
door.Open("invalid"); // Big no! It ain't possible.
door.Open("$ecr@t"); // Opening lab door
door.Close(); // Closing lab door
Console.ReadLine();
}
}
}