Design Pattern Examples
Overview of object-oriented design patterns
Proxy_Class.cs
Go to the documentation of this file.
1
6
7using System;
8
9
11{
18 public interface IWorkByProxy
19 {
25 string DoWork(string someArgument);
26 }
27
28
29 //========================================================================
30 //========================================================================
31
32
40 {
49 private class Real_Class : IWorkByProxy
50 {
52 // Implementation of the IWorkByProxy interface
54 string IWorkByProxy.DoWork(string someArgument)
55 {
56 return String.Format("Real class received '{0}'", someArgument);
57 }
58 }
59
60 //====================================================================
61 //====================================================================
62
63
67 private class Proxy_Class : IWorkByProxy
68 {
74
83 {
84 if (_realClassInstance == null)
85 {
86 Console.WriteLine(" --> Creating instance of real class...");
88 }
89 return _realClassInstance;
90 }
91
92
94 // Implementation of the IWorkByProxy interface
96
109 string IWorkByProxy.DoWork(string someArgument)
110 {
111 Console.WriteLine(" --> proxy class DoWork() in");
112 IWorkByProxy realClass = _GetRealClass();
113 Console.WriteLine(" --> Forwarding DoWork() call to real class...");
114 return realClass.DoWork(someArgument);
115 }
116 }
117
118
119 //====================================================================
120 //====================================================================
121
122
124 //---------------Public Methods ------------------------------------//
126
131 static public IWorkByProxy CreateProxy()
132 {
133 Console.WriteLine(" --> Creating instance of proxy class...");
134 return new Proxy_Class();
135 }
136 }
137}
The proxy class that implements the IWorkByProxy.
Definition: Proxy_Class.cs:68
IWorkByProxy _GetRealClass()
Helper method to retrieve the one and only instance of the real class. This hides the details of inst...
Definition: Proxy_Class.cs:82
IWorkByProxy? _realClassInstance
The one and only instance of the real class associated with this proxy class instance.
Definition: Proxy_Class.cs:73
The real class object that does all the work.
Definition: Proxy_Class.cs:50
For the purposes of this example, this class encapsulates the real class and proxy class to hide them...
Definition: Proxy_Class.cs:40
static IWorkByProxy CreateProxy()
Retrieve a new instance of the proxy class.
Definition: Proxy_Class.cs:131
Represents what can be done on the proxy object. This same interface is implemented on the real objec...
Definition: Proxy_Class.cs:19
string DoWork(string someArgument)
Does some work on the given argument and returns a new string.
The namespace containing all Design Pattern Examples implemented in C#.