I’m trying to implement a simple App that let me choose extras in a car.
Each extra item is a decorator for a base class Car.
Let’s say i do the following
Car p = new FordFusion();
p = new ElectricWindows(p);
p = new LeatherSeatings(p);
p = new Airbags(p);
then my object p will be a Ford Fusion with Electric Windows, Leather Seatings and Airbags.
I need to let the user remove the decorators out of order, like, let’s say, remove the Electric Windows without removing Leather Seatings and Airbags
I would probably tackle this problem with Chain of responsibility. I would create a handler interface
Then i’d create handler implementaions i.e. ElectricWindowHandler, LeatherHandler etc, for example CanHandle() => if option == UserOption.electric then Handle() => car.Accessories.Add( new ElectricWindow);
Every time the user changes the options, i’d clear the car accessories collection, do a foreach on selected user options and have the car object run through the chain. By the end of the chain process, you car will have correct accessories.