-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
88 lines (71 loc) · 1.79 KB
/
Program.cs
File metadata and controls
88 lines (71 loc) · 1.79 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
public interface IDocument
{
void Preview();
}
internal class Document : IDocument
{
private readonly string _filename;
public Document(string fileName)
{
_filename = fileName;
LongOperationLoadDocument();
}
private void LongOperationLoadDocument()
{
Thread.Sleep(1000);
}
public void Preview()
{
Console.WriteLine($"Preview document {_filename}");
}
}
internal class DocumentLazyLoad : IDocument
{
private readonly string _filename;
private readonly Lazy<IDocument> _document;
public DocumentLazyLoad(string fileName)
{
_filename = fileName;
_document = new Lazy<IDocument>(() => new Document(_filename));
}
public void Preview()
{
Console.WriteLine($"Preview document {_filename}");
}
}
internal class DocumentProtectedLazyLoad : IDocument
{
private readonly string _filename;
private readonly string _role;
private readonly IDocument _document;
public DocumentProtectedLazyLoad(string fileName, string role)
{
_filename = fileName;
_role = role;
_document = new DocumentLazyLoad(_filename);
}
public void Preview()
{
Console.WriteLine($"Entering Preview() in {nameof(DocumentProtectedLazyLoad)}");
if (_role != "Viewer")
{
throw new UnauthorizedAccessException();
}
else
{
_document.Preview();
}
}
}
internal class Program
{
public static void Main(string[] args)
{
var doc1 = new Document("Document1.pdf");
doc1.Preview();
var doc2 = new DocumentLazyLoad("Document2.pdf");
doc2.Preview();
var doc3 = new DocumentProtectedLazyLoad("Document3.pdf", "Viewer");
doc3.Preview();
}
}