Last active
December 29, 2015 18:48
-
-
Save codinko/d7a7be875d9b87e434e5 to your computer and use it in GitHub Desktop.
FactoryMethodPattern
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
public enum CarType { | |
SMALL, LUXURY | |
} | |
public Interface class Car { // abstract class or Interface. | |
} | |
public class CarFactory { | |
public static Car buildCar(CarType model) { | |
Car car = null; | |
switch (model) { | |
case SMALL: | |
car = new SmallCar(); | |
break; | |
case LUXURY: | |
car = new LuxuryCar(); | |
break; | |
default: | |
// throw some exception | |
break; | |
} | |
return car; | |
} | |
} | |
//Client code | |
CarFactory.buildCar(CarType.SMALL) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment