Last active
August 29, 2015 14:08
-
-
Save JustinK/51e907d8432ee24fc8de to your computer and use it in GitHub Desktop.
SOLID - Open Closed Principle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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