static void Main(string[] args) { Car a = new Car(); Console.WriteLine(a.CarType); a = new SuperCar(); Console.WriteLine(a.CarType); }
static void Main(string[] args) { Bus bus = new Bus("Bogdan"); Freight freight = new Freight("VW Transporter"); SuperCar super = new SuperCar("Ferrari"); super.Go(); bus.Go(); freight.Go(); }
/* * The role of the decorator pattern is to add the state and behavior to an object dynamically. * The object does not know that it is being decorated therefore it is useful pattern for evolving systems. * You pass the object to an instance of another class that the other class extends it (decorates it). * So the original object being passed passes the open/closed principle, where it is extended but not modified. * This is why this pattern is so popular. * This example was taken from http://www.dotnetforall.com/c-decorator-pattern/ a more realistic example can * be found at https://assist-software.net/blog/implementation-decorator-pattern-c */ public static void launchExample() { ICar traditionalCar = new Car(); traditionalCar.Drive(); traditionalCar.Brake(); //Pass the car object into the decorator SuperCar superCar = new SuperCar(traditionalCar); superCar.Drive(); superCar.Music(); superCar.Brake(); Console.Read(); }
public string AddCar(string type, string make, string model, string VIN, int horsePower) { ICar currentCar; if (type == "SuperCar") { currentCar = new SuperCar(make, model, VIN, horsePower); } else if (type == "TunedCar") { currentCar = new TunedCar(make, model, VIN, horsePower); } else { throw new ArgumentException("Invalid car type!"); } cars.Add(currentCar); return($"Successfully added car {make} {model} ({VIN})."); }
public string AddCar(string type, string make, string model, string VIN, int horsePower) { Car car = null; if (type == "SuperCar") { car = new SuperCar(make, model, VIN, horsePower); } else if (type == "TunedCar") { car = new TunedCar(make, model, VIN, horsePower); } else { throw new ArgumentException(InvalidCarType); } this.cars.Add(car); return(string.Format(SuccessfullyAddedCar, make, model, VIN)); }
public string AddCar(string type, string make, string model, string VIN, int horsePower) { Car car = default; if (type == "SuperCar") { car = new SuperCar(make, model, VIN, horsePower); } else if (type == "TunedCar") { car = new TunedCar(make, model, VIN, horsePower); } else { throw new ArgumentException(ExceptionMessages.InvalidCarType); } cars.Add(car); return(string.Format(OutputMessages.SuccessfullyAddedCar, make, model, VIN)); }
public string AddCar(string type, string make, string model, string VIN, int horsePower) { ICar car; switch (type) { case "SuperCar": car = new SuperCar(make, model, VIN, horsePower); break; case "TunedCar": car = new TunedCar(make, model, VIN, horsePower); break; default: throw new ArgumentException(ExceptionMessages.InvalidCarType); } cars.Add(car); return(string.Format(OutputMessages.SuccessfullyAddedCar, make, model, VIN)); }
public string AddCar(string type, string make, string model, string VIN, int horsePower) { if (type != nameof(SuperCar) && type != nameof(TunedCar)) { throw new ArgumentException("Invalid car type!"); } ICar car; if (type == nameof(SuperCar)) { car = new SuperCar(make, model, VIN, horsePower); } else { car = new TunedCar(make, model, VIN, horsePower); } this.cars.Add(car); return(string.Format(OutputMessages.SuccessfullyAddedCar, car.Make, car.Model, car.VIN)); }
public string AddCar(string type, string make, string model, string VIN, int horsePower) { if (type != "SuperCar" && type != "TunedCar") { throw new ArgumentException(ExceptionMessages.InvalidCarType); } if (type == "SuperCar") { SuperCar superCar = new SuperCar(make, model, VIN, horsePower); cars.Add(superCar); } else if (type == "TunedCar") { TunedCar tunedCar = new TunedCar(make, model, VIN, horsePower); cars.Add(tunedCar); } return($"Successfully added car {make} {model} ({VIN})."); }
public string AddCar(string type, string make, string model, string VIN, int horsePower) { if (type != "SuperCar" && type != "TunedCar") { throw new ArgumentException(ExceptionMessages.InvalidCarType); } ICar car = null; if (type == "SuperCar") { car = new SuperCar(make, model, VIN, horsePower); } else if (type == "TunedCar") { car = new TunedCar(make, model, VIN, horsePower); } this.carRepository.Add(car); return(string.Format(OutputMessages.SuccessfullyAddedCar, make, model, VIN)); }
public string AddCar(string type, string make, string model, string VIN, int horsePower) { string result; if (type == "SuperCar") { Car car = new SuperCar(make, model, VIN, horsePower); this.cars.Add(car); result = $"Successfully added car {make} {model} ({VIN})."; } else if (type == "TunedCar") { Car car = new TunedCar(make, model, VIN, horsePower); this.cars.Add(car); result = $"Successfully added car {make} {model} ({VIN})."; } else { throw new ArgumentException("Invalid car type!"); } return(result); }
// "copy" constructor public SuperCar(Car c) : base(c) { SuperCar sc = c as SuperCar; if(sc != null) { SuperCarProperty = sc.SuperCarProperty; } }