Skip to content

Commit 0079eef

Browse files
committed
Proxy Pattern example
1 parent e917a17 commit 0079eef

File tree

3 files changed

+82
-0
lines changed

3 files changed

+82
-0
lines changed

DesignPatterns.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DecoratorPattern", "Decorat
2525
EndProject
2626
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FlyweightPattern", "FlyweightPattern\FlyweightPattern.csproj", "{FB1E171E-9C39-4EF1-9377-9786AF3C61DA}"
2727
EndProject
28+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ProxyPattern", "ProxyPattern\ProxyPattern.csproj", "{CE4E3751-C4D9-440E-9E25-3E7EA61F4A7D}"
29+
EndProject
2830
Global
2931
GlobalSection(SolutionConfigurationPlatforms) = preSolution
3032
Debug|Any CPU = Debug|Any CPU
@@ -75,6 +77,10 @@ Global
7577
{FB1E171E-9C39-4EF1-9377-9786AF3C61DA}.Debug|Any CPU.Build.0 = Debug|Any CPU
7678
{FB1E171E-9C39-4EF1-9377-9786AF3C61DA}.Release|Any CPU.ActiveCfg = Release|Any CPU
7779
{FB1E171E-9C39-4EF1-9377-9786AF3C61DA}.Release|Any CPU.Build.0 = Release|Any CPU
80+
{CE4E3751-C4D9-440E-9E25-3E7EA61F4A7D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
81+
{CE4E3751-C4D9-440E-9E25-3E7EA61F4A7D}.Debug|Any CPU.Build.0 = Debug|Any CPU
82+
{CE4E3751-C4D9-440E-9E25-3E7EA61F4A7D}.Release|Any CPU.ActiveCfg = Release|Any CPU
83+
{CE4E3751-C4D9-440E-9E25-3E7EA61F4A7D}.Release|Any CPU.Build.0 = Release|Any CPU
7884
EndGlobalSection
7985
GlobalSection(SolutionProperties) = preSolution
8086
HideSolutionNode = FALSE

ProxyPattern/Program.cs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
using System;
2+
3+
namespace ProxyPattern
4+
{
5+
interface IDoor
6+
{
7+
void Open();
8+
void Close();
9+
}
10+
11+
class LabDoor : IDoor
12+
{
13+
public void Close()
14+
{
15+
Console.WriteLine("Closing lab door");
16+
}
17+
18+
public void Open()
19+
{
20+
Console.WriteLine("Opening lab door");
21+
}
22+
}
23+
24+
class SecuredDoor
25+
{
26+
private IDoor mDoor;
27+
28+
public SecuredDoor(IDoor door)
29+
{
30+
mDoor = door ?? throw new ArgumentNullException("door", "door can not be null");
31+
}
32+
33+
public void Open(string password)
34+
{
35+
if (Authenticate(password))
36+
{
37+
mDoor.Open();
38+
}
39+
else
40+
{
41+
Console.WriteLine("Big no! It ain't possible.");
42+
}
43+
}
44+
45+
private bool Authenticate(string password)
46+
{
47+
return password == "$ecr@t" ? true : false;
48+
}
49+
50+
public void Close()
51+
{
52+
mDoor.Close();
53+
}
54+
}
55+
class Program
56+
{
57+
static void Main(string[] args)
58+
{
59+
var door = new SecuredDoor(new LabDoor());
60+
door.Open("invalid"); // Big no! It ain't possible.
61+
62+
door.Open("$ecr@t"); // Opening lab door
63+
door.Close(); // Closing lab door
64+
65+
Console.ReadLine();
66+
}
67+
}
68+
}

ProxyPattern/ProxyPattern.csproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp2.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
</Project>

0 commit comments

Comments
 (0)