Skip to content

Commit 704c079

Browse files
committed
Proxy is done
1 parent daefd90 commit 704c079

File tree

10 files changed

+180
-1
lines changed

10 files changed

+180
-1
lines changed

Proxy/.vscode/launch.json

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
// Use IntelliSense to find out which attributes exist for C# debugging
3+
// Use hover for the description of the existing attributes
4+
// For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": ".NET Core Launch (console)",
9+
"type": "coreclr",
10+
"request": "launch",
11+
"preLaunchTask": "build",
12+
// If you have changed target frameworks, make sure to update the program path.
13+
"program": "${workspaceFolder}/bin/Debug/netcoreapp2.0/Proxy.dll",
14+
"args": [],
15+
"cwd": "${workspaceFolder}",
16+
// For more information about the 'console' field, see https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md#console-terminal-window
17+
"console": "internalConsole",
18+
"stopAtEntry": false,
19+
"internalConsoleOptions": "openOnSessionStart"
20+
},
21+
{
22+
"name": ".NET Core Attach",
23+
"type": "coreclr",
24+
"request": "attach",
25+
"processId": "${command:pickProcess}"
26+
}
27+
]
28+
}

Proxy/.vscode/tasks.json

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"version": "2.0.0",
3+
"tasks": [
4+
{
5+
"taskName": "build",
6+
"command": "dotnet",
7+
"type": "process",
8+
"args": [
9+
"build",
10+
"${workspaceFolder}/Proxy.csproj"
11+
],
12+
"problemMatcher": "$msCompile"
13+
}
14+
]
15+
}

Proxy/Classes/BookStore.cs

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System.Linq;
2+
3+
namespace Proxy.Classes
4+
{
5+
public class BookStore : IBook
6+
{
7+
PageContext db;
8+
public BookStore()
9+
{
10+
db = new PageContext();
11+
}
12+
public Page GetPage(int number)
13+
{
14+
return db.Pages.FirstOrDefault(p => p.Number == number);
15+
}
16+
17+
public void Dispose()
18+
{
19+
// dispose
20+
}
21+
}
22+
}

Proxy/Classes/BookStoreProxy.cs

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
4+
namespace Proxy.Classes
5+
{
6+
public class BookStoreProxy : IBook
7+
{
8+
List<Page> pages;
9+
BookStore bookStore;
10+
public BookStoreProxy()
11+
{
12+
pages = new List<Page>();
13+
}
14+
public Page GetPage(int number)
15+
{
16+
Page page = pages.FirstOrDefault(p => p.Number == number);
17+
if (page == null)
18+
{
19+
if (bookStore == null)
20+
bookStore = new BookStore();
21+
page = bookStore.GetPage(number);
22+
pages.Add(page);
23+
}
24+
return page;
25+
}
26+
27+
public void Dispose()
28+
{
29+
if (bookStore != null)
30+
bookStore.Dispose();
31+
}
32+
}
33+
}

Proxy/Classes/IBook.cs

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using System;
2+
3+
namespace Proxy.Classes
4+
{
5+
public interface IBook : IDisposable
6+
{
7+
Page GetPage(int number);
8+
}
9+
}

Proxy/Classes/Page.cs

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace Proxy.Classes
2+
{
3+
public class Page
4+
{
5+
public int Id { get; set; }
6+
public int Number { get; set; }
7+
public string Text { get; set; }
8+
}
9+
}

Proxy/Classes/PageContext.cs

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System.Collections.Generic;
2+
3+
namespace Proxy.Classes
4+
{
5+
public class PageContext
6+
{
7+
public List<Page> Pages { get; set; } = new List<Page>
8+
{
9+
new Page
10+
{
11+
Id = 0,
12+
Number = 0,
13+
Text = "Text 1"
14+
},
15+
new Page
16+
{
17+
Id = 1,
18+
Number = 1,
19+
Text = "Text 2"
20+
},
21+
new Page
22+
{
23+
Id = 2,
24+
Number = 2,
25+
Text = "Text 3"
26+
}
27+
};
28+
}
29+
}

Proxy/Program.cs

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System;
2+
using Proxy.Classes;
3+
4+
namespace Proxy
5+
{
6+
class Program
7+
{
8+
static void Main(string[] args)
9+
{
10+
using (IBook book = new BookStoreProxy())
11+
{
12+
// read the first page
13+
Page page1 = book.GetPage(1);
14+
Console.WriteLine(page1.Text);
15+
// read the second page
16+
Page page2 = book.GetPage(2);
17+
Console.WriteLine(page2.Text);
18+
// return back the first page
19+
page1 = book.GetPage(1);
20+
Console.WriteLine(page1.Text);
21+
}
22+
23+
Console.Read();
24+
}
25+
}
26+
}

Proxy/Proxy.csproj

+8
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>

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ My realizations of design patterns:
1414
- [x] #Decorator
1515
- [x] #Facade
1616
- [x] #Flyweight
17-
- [ ] #Proxy
17+
- [x] #Proxy
1818

1919
- Behavioral Patterns
2020
- [x] #Chain of Resp.

0 commit comments

Comments
 (0)