Skip to content

Commit aa48986

Browse files
committed
#Feature Add Proxy Design Pattern
1 parent 2c78bae commit aa48986

3 files changed

Lines changed: 97 additions & 9 deletions

File tree

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,47 @@
11
@page "/proxy"
2+
@using DesignPatterns.Structural.Proxy
23
@rendermode InteractiveServer
34

45
<PageTitle>Proxy</PageTitle>
6+
7+
8+
<h3>🧩 Proxy Pattern Demo - Lazy Image Loader</h3>
9+
10+
<p>The Proxy Pattern delays object creation until needed — great for performance optimization.</p>
11+
12+
<div class="card p-3 shadow-sm">
13+
<div class="mb-3">
14+
<label class="form-label">Image File Name:</label>
15+
<input type="text" class="form-control" @bind="FileName" placeholder="e.g., sunset.jpg" />
16+
</div>
17+
<div class="d-flex flex-row justify-content-center">
18+
<button class="btn btn-outline-primary m-1" @onclick="CreateProxy">Create Proxy</button>
19+
<button class="btn btn-outline-success m-1" @onclick="DisplayImage" disabled="@(_imageProxy == null)">Display Image</button>
20+
</div>
21+
@if (!string.IsNullOrEmpty(Message))
22+
{
23+
<div class="alert alert-info mt-3">
24+
@Message
25+
</div>
26+
}
27+
</div>
28+
29+
@code {
30+
private string FileName = "sunset.jpg";
31+
private IImage? _imageProxy;
32+
private string? Message;
33+
34+
private void CreateProxy()
35+
{
36+
_imageProxy = new ProxyImage(FileName);
37+
Message = $"Proxy for '{FileName}' created (image not loaded yet).";
38+
}
39+
40+
private void DisplayImage()
41+
{
42+
if (_imageProxy != null)
43+
{
44+
Message = _imageProxy.Display();
45+
}
46+
}
47+
}

SRC/DesignPatterns.Structural/DesignPatterns.Structural.csproj

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,4 @@
66
<Nullable>enable</Nullable>
77
</PropertyGroup>
88

9-
<ItemGroup>
10-
<Folder Include="Bridge\" />
11-
<Folder Include="Composite\" />
12-
<Folder Include="Decorator\" />
13-
<Folder Include="Facade\" />
14-
<Folder Include="Flyweight\" />
15-
<Folder Include="Proxy\" />
16-
</ItemGroup>
17-
189
</Project>
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
namespace DesignPatterns.Structural.Proxy;
2+
3+
// Subject interface
4+
public interface IImage
5+
{
6+
string Display();
7+
}
8+
9+
// The Real Subject
10+
public class RealImage : IImage
11+
{
12+
private readonly string _fileName;
13+
14+
public RealImage(string fileName)
15+
{
16+
_fileName = fileName;
17+
LoadFromDisk(); // Simulate expensive operation
18+
}
19+
20+
private void LoadFromDisk()
21+
{
22+
Thread.Sleep(1000); // Simulate delay
23+
Console.WriteLine($"Loading image {_fileName} from disk...");
24+
}
25+
26+
public string Display()
27+
{
28+
return $"🖼️ Displaying image: {_fileName}";
29+
}
30+
}
31+
32+
// The Proxy class
33+
public class ProxyImage : IImage
34+
{
35+
private readonly string _fileName;
36+
private RealImage? _realImage;
37+
38+
public ProxyImage(string fileName)
39+
{
40+
_fileName = fileName;
41+
}
42+
43+
public string Display()
44+
{
45+
// Lazy loading: only load when needed
46+
if (_realImage == null)
47+
{
48+
_realImage = new RealImage(_fileName);
49+
}
50+
51+
return _realImage.Display();
52+
}
53+
}
54+

0 commit comments

Comments
 (0)