add an excercise to proxys

This commit is contained in:
Andreas Zweili 2018-01-27 10:10:59 +01:00
parent a37fcfa4f0
commit 54d24c9f62
6 changed files with 100 additions and 0 deletions

View File

@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace design_patterns
{
class Car : ICar
{
public void MoveCar(Driver _driver)
{
Console.WriteLine("I'm driving...");
}
}
}

View File

@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace design_patterns
{
class Driver
{
public int age { get; set; }
public Driver(int s)
{
this.age = s;
}
}
}

View File

@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace design_patterns
{
interface ICar
{
void MoveCar(Driver _driver);
}
}

View File

@ -20,6 +20,22 @@ namespace design_patterns
EagerSingleton.Instance.value = "Hello from EagerSingleton";
Console.WriteLine(EagerSingleton.Instance.value);
Console.ReadKey();
Console.WriteLine("--------");
Console.WriteLine();
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("Proxys");
Console.WriteLine("--------");
Driver max = new Driver(17);
Driver heinz = new Driver(12);
ProxyCar ferarri = new ProxyCar();
Console.WriteLine("Let max drive");
ferarri.MoveCar(max);
Console.WriteLine();
Console.WriteLine("Let heinz drive");
ferarri.MoveCar(heinz);
Console.ReadKey();
}
}
}

View File

@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace design_patterns
{
class ProxyCar : ICar
{
Car realCar = null;
public void MoveCar(Driver _driver)
{
if (realCar == null)
{
realCar = new Car();
}
Console.WriteLine("The proxy is checking the driver age.");
if (_driver.age > 16)
{
Console.WriteLine("Check passed.");
realCar.MoveCar(_driver);
}
else
{
Console.WriteLine("You're not old enough");
}
}
}
}

View File

@ -42,10 +42,14 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Driver.cs" />
<Compile Include="EagerSingleton.cs" />
<Compile Include="ICar.cs" />
<Compile Include="LazySingleton.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Car.cs" />
<Compile Include="ProxyCar.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />