diff --git a/DesignPatternGuru/Flyweight/Flyweight.csproj b/DesignPatternGuru/Flyweight/Flyweight.csproj index 74abf5c..7d211a9 100644 --- a/DesignPatternGuru/Flyweight/Flyweight.csproj +++ b/DesignPatternGuru/Flyweight/Flyweight.csproj @@ -7,4 +7,8 @@ enable + + + + diff --git a/DesignPatternGuru/Flyweight/Flyweights/Flyweight.cs b/DesignPatternGuru/Flyweight/Flyweights/Flyweight.cs index 6a5819c..935b979 100644 --- a/DesignPatternGuru/Flyweight/Flyweights/Flyweight.cs +++ b/DesignPatternGuru/Flyweight/Flyweights/Flyweight.cs @@ -1,4 +1,6 @@ -using System; +using Flyweight.Objects; +using Newtonsoft.Json; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -8,6 +10,18 @@ namespace Flyweight.Flyweights { internal class Flyweight { + private Car _sharedState; + public Flyweight(Car car) + { + _sharedState = car; + } + + public void Operation(Car uniqueState) + { + string s = JsonConvert.SerializeObject(_sharedState); + string u = JsonConvert.SerializeObject(uniqueState); + Console.WriteLine($"Flyweight: Displaying shared {s} and unique {u} state."); + } } } diff --git a/DesignPatternGuru/Flyweight/Flyweights/FlyweightFactory.cs b/DesignPatternGuru/Flyweight/Flyweights/FlyweightFactory.cs new file mode 100644 index 0000000..1903288 --- /dev/null +++ b/DesignPatternGuru/Flyweight/Flyweights/FlyweightFactory.cs @@ -0,0 +1,67 @@ +using Flyweight.Objects; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Flyweight.Flyweights +{ + internal class FlyweightFactory + { + private List> _flyweights = new List>(); + + public FlyweightFactory(params Car[] args) + { + foreach (var elem in args) + { + _flyweights.Add(new Tuple(new Flyweight(elem), GetKey(elem))); + } + } + + public string GetKey(Car car) + { + List elements = new List(); + + elements.Add(car.Model); + elements.Add(car.Color); + elements.Add(car.Company); + + if (car.Owner != null && car.Number != null) + { + elements.Add(car.Number); + elements.Add(car.Owner); + } + + elements.Sort(); + + return String.Join("_", elements); + } + + public Flyweight GetFlyweight(Car sharedState) + { + string key = GetKey(sharedState); + if (_flyweights.Where(t => t.Item2 == key).Count() == 0) + { + Console.WriteLine("FlyweightFactory: Can't find a flyweight, creating new one."); + _flyweights.Add(new Tuple(new Flyweight(sharedState), key)); + } + else + { + Console.WriteLine("FlyweightFactory: Reusing existing flyweight."); + } + + return _flyweights.Where(t => t.Item2 == key).FirstOrDefault().Item1; + } + + public void ListFlyweights() + { + int count = _flyweights.Count; + Console.WriteLine($"FlyweightFactory: I have {count} flyweights"); + foreach (var flyweight in _flyweights) + { + Console.WriteLine(flyweight.Item2); + } + } + } +} diff --git a/DesignPatternGuru/Flyweight/Program.cs b/DesignPatternGuru/Flyweight/Program.cs index 22c232b..5bd92c8 100644 --- a/DesignPatternGuru/Flyweight/Program.cs +++ b/DesignPatternGuru/Flyweight/Program.cs @@ -1,10 +1,53 @@ -namespace Flyweight +using Flyweight.Flyweights; +using Flyweight.Objects; + +namespace Flyweight { internal class Program { static void Main(string[] args) { - Console.WriteLine("Hello, World!"); + var factory = new FlyweightFactory( + new Car { Company = "Chevrolet", Model = "Camaro2018", Color = "pink" }, + new Car { Company = "Mercedes Benz", Model = "C300", Color = "black" }, + new Car { Company = "Mercedes Benz", Model = "C500", Color = "red" }, + new Car { Company = "BMW", Model = "M5", Color = "red" }, + new Car { Company = "BMW", Model = "X6", Color = "white" } + ); + factory.ListFlyweights(); + + AddCarToPoliceDatabase(factory, new Car + { + Number = "CL234IR", + Owner = "James Doe", + Company = "BMW", + Model = "M5", + Color = "red" + }); + + AddCarToPoliceDatabase(factory, new Car + { + Number = "CL234IR", + Owner = "James Doe", + Company = "BMW", + Model = "X1", + Color = "red" + }); + + factory.ListFlyweights(); + } + + public static void AddCarToPoliceDatabase(FlyweightFactory factory, Car car) + { + Console.WriteLine("Client: Adding a car to database."); + var flyweight = factory.GetFlyweight(new Car + { + Color = car.Color, + Model = car.Model, + Company = car.Company, + }); + + flyweight.Operation(car); } } } \ No newline at end of file