Skip to content

Instantly share code, notes, and snippets.

@JustinK
Last active August 29, 2015 14:08
Show Gist options
  • Save JustinK/51e907d8432ee24fc8de to your computer and use it in GitHub Desktop.
Save JustinK/51e907d8432ee24fc8de to your computer and use it in GitHub Desktop.
SOLID - Open Closed Principle
//NOT SO GOOD
public class Car
{
public bool IsBus {get; set;}
public bool HasTurbo {get; set;}
public int NumberOfPistons {get; set;}
public double TotalWeight {get; set;}
abstract public double GetHorsePower(){
if (HasTurbo){
return TotalWeight / NumberOfPistons + 250
}else if(IsBus){
return TotalWeight / NumberOfPistons - 400
}else{
return TotalWeight / NumberOfPistons
}
//this could get messy fast and we would need to come in and change this class every time we have a new car or calculation
}
}
//GOOD
abstract class Car
{
public int NumberOfPistons {get; set;}
public double TotalWeight {get; set;}
abstract public double GetHorsePower();
}
//extend abstract base class Car
class MyCar : Car
{
public int TurboHorsePower {get; set}
//provide own implementation of GetHorsepower method
public override double GetHorsePower(){
return TotalWeight / NumberOfPistons + TurboHorsePower
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment