public static void addCarToPoliceDatabase(FlyweightFactory factory, FlyweightCar car) { Console.WriteLine("\nClient: Adding a car to database."); var flyweight = factory.GetFlyweight(new FlyweightCar { Color = car.Color, Model = car.Model, Company = car.Company }); // The client code either stores or calculates extrinsic state and // passes it to the flyweight's methods. flyweight.Operation(car); }
public static void Run() { // The client code usually creates a bunch of pre-populated // flyweights in the initialization stage of the application. var factory = new FlyweightFactory( new FlyweightCar { Company = "Chevrolet", Model = "Camaro2018", Color = "pink" }, new FlyweightCar { Company = "Mercedes Benz", Model = "C300", Color = "black" }, new FlyweightCar { Company = "Mercedes Benz", Model = "C500", Color = "red" }, new FlyweightCar { Company = "BMW", Model = "M5", Color = "red" }, new FlyweightCar { Company = "BMW", Model = "X6", Color = "white" } ); factory.ListFlyweights(); addCarToPoliceDatabase(factory, new FlyweightCar { Number = "CL234IR", Owner = "James Doe", Company = "BMW", Model = "M5", Color = "red" }); addCarToPoliceDatabase(factory, new FlyweightCar { Number = "CL234IR", Owner = "James Doe", Company = "BMW", Model = "X1", Color = "red" }); factory.ListFlyweights(); }