syeffort 2 years ago
parent 4861f7271c
commit ddfbee48f0
  1. 11
      DesignPatternGuru/DesignPatternGuru.sln
  2. 3
      DesignPatternGuru/Flyweight/Program.cs
  3. 17
      DesignPatternGuru/Proxy/Client.cs
  4. 22
      DesignPatternGuru/Proxy/Program.cs
  5. 13
      DesignPatternGuru/Proxy/Proxies/ISubject.cs
  6. 40
      DesignPatternGuru/Proxy/Proxies/Proxy.cs
  7. 16
      DesignPatternGuru/Proxy/Proxies/RealSubject.cs
  8. 10
      DesignPatternGuru/Proxy/Proxy.csproj

@ -25,9 +25,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Composite", "Composite\Comp
EndProject EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Decorator", "Decorator\Decorator.csproj", "{8AA3ECFD-4F9A-433D-884C-E76E5050AFBF}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Decorator", "Decorator\Decorator.csproj", "{8AA3ECFD-4F9A-433D-884C-E76E5050AFBF}"
EndProject 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 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 EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution 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}.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.ActiveCfg = Release|Any CPU
{D414F5DF-8FB2-4D77-A0C8-77B623A902DD}.Release|Any CPU.Build.0 = 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 EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE
@ -95,6 +101,7 @@ Global
{8AA3ECFD-4F9A-433D-884C-E76E5050AFBF} = {0B9333EC-FB73-4FEE-B600-9ECCD5A356B1} {8AA3ECFD-4F9A-433D-884C-E76E5050AFBF} = {0B9333EC-FB73-4FEE-B600-9ECCD5A356B1}
{7D6D306D-BC44-407A-9A39-104C90A3FA39} = {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} {D414F5DF-8FB2-4D77-A0C8-77B623A902DD} = {0B9333EC-FB73-4FEE-B600-9ECCD5A356B1}
{F0E63700-B550-4D57-B912-18C28B3FEAC6} = {0B9333EC-FB73-4FEE-B600-9ECCD5A356B1}
EndGlobalSection EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {3E5A9C6B-2E8A-466E-B5E0-4379902EFFAC} SolutionGuid = {3E5A9C6B-2E8A-466E-B5E0-4379902EFFAC}

@ -16,6 +16,7 @@ namespace Flyweight
); );
factory.ListFlyweights(); factory.ListFlyweights();
Console.WriteLine();
AddCarToPoliceDatabase(factory, new Car AddCarToPoliceDatabase(factory, new Car
{ {
Number = "CL234IR", Number = "CL234IR",
@ -25,6 +26,7 @@ namespace Flyweight
Color = "red" Color = "red"
}); });
Console.WriteLine();
AddCarToPoliceDatabase(factory, new Car AddCarToPoliceDatabase(factory, new Car
{ {
Number = "CL234IR", Number = "CL234IR",
@ -34,6 +36,7 @@ namespace Flyweight
Color = "red" Color = "red"
}); });
Console.WriteLine();
factory.ListFlyweights(); factory.ListFlyweights();
} }

@ -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();
}
}
}

@ -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);
}
}
}

@ -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();
}
}

@ -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}");
}
}
}

@ -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.");
}
}
}

@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
Loading…
Cancel
Save