diff --git a/DesignPatternGuru/DesignPatternGuru.sln b/DesignPatternGuru/DesignPatternGuru.sln index 11e4e00..fa55c50 100644 --- a/DesignPatternGuru/DesignPatternGuru.sln +++ b/DesignPatternGuru/DesignPatternGuru.sln @@ -25,9 +25,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Composite", "Composite\Comp EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Decorator", "Decorator\Decorator.csproj", "{8AA3ECFD-4F9A-433D-884C-E76E5050AFBF}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Facade", "Facade\Facade.csproj", "{7D6D306D-BC44-407A-9A39-104C90A3FA39}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Facade", "Facade\Facade.csproj", "{7D6D306D-BC44-407A-9A39-104C90A3FA39}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Flyweight", "Flyweight\Flyweight.csproj", "{D414F5DF-8FB2-4D77-A0C8-77B623A902DD}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Flyweight", "Flyweight\Flyweight.csproj", "{D414F5DF-8FB2-4D77-A0C8-77B623A902DD}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Proxy", "Proxy\Proxy.csproj", "{F0E63700-B550-4D57-B912-18C28B3FEAC6}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -79,6 +81,10 @@ Global {D414F5DF-8FB2-4D77-A0C8-77B623A902DD}.Debug|Any CPU.Build.0 = Debug|Any CPU {D414F5DF-8FB2-4D77-A0C8-77B623A902DD}.Release|Any CPU.ActiveCfg = Release|Any CPU {D414F5DF-8FB2-4D77-A0C8-77B623A902DD}.Release|Any CPU.Build.0 = Release|Any CPU + {F0E63700-B550-4D57-B912-18C28B3FEAC6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {F0E63700-B550-4D57-B912-18C28B3FEAC6}.Debug|Any CPU.Build.0 = Debug|Any CPU + {F0E63700-B550-4D57-B912-18C28B3FEAC6}.Release|Any CPU.ActiveCfg = Release|Any CPU + {F0E63700-B550-4D57-B912-18C28B3FEAC6}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -95,6 +101,7 @@ Global {8AA3ECFD-4F9A-433D-884C-E76E5050AFBF} = {0B9333EC-FB73-4FEE-B600-9ECCD5A356B1} {7D6D306D-BC44-407A-9A39-104C90A3FA39} = {0B9333EC-FB73-4FEE-B600-9ECCD5A356B1} {D414F5DF-8FB2-4D77-A0C8-77B623A902DD} = {0B9333EC-FB73-4FEE-B600-9ECCD5A356B1} + {F0E63700-B550-4D57-B912-18C28B3FEAC6} = {0B9333EC-FB73-4FEE-B600-9ECCD5A356B1} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {3E5A9C6B-2E8A-466E-B5E0-4379902EFFAC} diff --git a/DesignPatternGuru/Flyweight/Program.cs b/DesignPatternGuru/Flyweight/Program.cs index 5bd92c8..dd62039 100644 --- a/DesignPatternGuru/Flyweight/Program.cs +++ b/DesignPatternGuru/Flyweight/Program.cs @@ -16,6 +16,7 @@ namespace Flyweight ); factory.ListFlyweights(); + Console.WriteLine(); AddCarToPoliceDatabase(factory, new Car { Number = "CL234IR", @@ -25,6 +26,7 @@ namespace Flyweight Color = "red" }); + Console.WriteLine(); AddCarToPoliceDatabase(factory, new Car { Number = "CL234IR", @@ -34,6 +36,7 @@ namespace Flyweight Color = "red" }); + Console.WriteLine(); factory.ListFlyweights(); } diff --git a/DesignPatternGuru/Proxy/Client.cs b/DesignPatternGuru/Proxy/Client.cs new file mode 100644 index 0000000..297ee14 --- /dev/null +++ b/DesignPatternGuru/Proxy/Client.cs @@ -0,0 +1,17 @@ +using Proxy.Proxies; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Proxy +{ + internal class Client + { + public void ClientCode(ISubject subject) + { + subject.Request(); + } + } +} diff --git a/DesignPatternGuru/Proxy/Program.cs b/DesignPatternGuru/Proxy/Program.cs new file mode 100644 index 0000000..a112e77 --- /dev/null +++ b/DesignPatternGuru/Proxy/Program.cs @@ -0,0 +1,22 @@ +using Proxy.Proxies; + +namespace Proxy +{ + internal class Program + { + static void Main(string[] args) + { + Client client = new Client(); + + Console.WriteLine("Client: Executing the client code with a real subject:"); + RealSubject realSubject = new RealSubject(); + client.ClientCode(realSubject); + + Console.WriteLine(); + + Console.WriteLine("Client: Executing the same client code with a proxy"); + Proxies.Proxy proxy = new Proxies.Proxy(realSubject); + client.ClientCode(proxy); + } + } +} \ No newline at end of file diff --git a/DesignPatternGuru/Proxy/Proxies/ISubject.cs b/DesignPatternGuru/Proxy/Proxies/ISubject.cs new file mode 100644 index 0000000..a39a986 --- /dev/null +++ b/DesignPatternGuru/Proxy/Proxies/ISubject.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Proxy.Proxies +{ + internal interface ISubject + { + void Request(); + } +} diff --git a/DesignPatternGuru/Proxy/Proxies/Proxy.cs b/DesignPatternGuru/Proxy/Proxies/Proxy.cs new file mode 100644 index 0000000..e5dfaa3 --- /dev/null +++ b/DesignPatternGuru/Proxy/Proxies/Proxy.cs @@ -0,0 +1,40 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Proxy.Proxies +{ + internal class Proxy : ISubject + { + private RealSubject _realSubject; + + public Proxy(RealSubject realSubject) + { + _realSubject = realSubject; + } + + public void Request() + { + if (!CheckAccess()) + return; + + _realSubject.Request(); + + LogAccess(); + } + + public bool CheckAccess() + { + Console.WriteLine("Proxy: Checking access prior to firing a real request."); + + return true; + } + + public void LogAccess() + { + Console.WriteLine($"Proxy: Logging the time of request@{DateTime.Now}"); + } + } +} diff --git a/DesignPatternGuru/Proxy/Proxies/RealSubject.cs b/DesignPatternGuru/Proxy/Proxies/RealSubject.cs new file mode 100644 index 0000000..fb35af6 --- /dev/null +++ b/DesignPatternGuru/Proxy/Proxies/RealSubject.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Proxy.Proxies +{ + internal class RealSubject : ISubject + { + public void Request() + { + Console.WriteLine("RealSubject: Handling Request."); + } + } +} diff --git a/DesignPatternGuru/Proxy/Proxy.csproj b/DesignPatternGuru/Proxy/Proxy.csproj new file mode 100644 index 0000000..74abf5c --- /dev/null +++ b/DesignPatternGuru/Proxy/Proxy.csproj @@ -0,0 +1,10 @@ + + + + Exe + net6.0 + enable + enable + + +