Skip to content

Instantly share code, notes, and snippets.

@codinko
Last active December 29, 2015 18:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codinko/d7a7be875d9b87e434e5 to your computer and use it in GitHub Desktop.
Save codinko/d7a7be875d9b87e434e5 to your computer and use it in GitHub Desktop.
FactoryMethodPattern
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