Promoting Extensibility

Think of switch statements as missed opportunities to be polymorphic.  Instead of changing or extending a switch (or nested if-else statements), use Polymorphic Dispatch to invert the downstream dependencies.  This is easily accomplished by replacing the argument of the switch with an abstract class containing a method that corresponds to the operation of the switch.  Then simply create derived classes and implement that method for each of the cases.  This will eliminate the need to recompile and redeploy that module (or possibly the entire application) any time a case is added or changed.  This approach provides alignment with the Open/Closed Principle — greatly enhancing maintainability, adaptability, extensibility, and agility.

;