Created
May 26, 2020 09:54
-
-
Save SiAust/ab4ab7d3a4eb2608e610234edefa8f5b to your computer and use it in GitHub Desktop.
Example of a static factory.
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
class MotorStaticFactory { | |
/** | |
* It returns an initialized motor according to the specified type by the first character: | |
* 'P' or 'p' - pneumatic, 'H' or 'h' - hydraulic, 'E' or 'e' - electric, 'W' or 'w' - warp. | |
*/ | |
public static Motor make(char type, String model, long power) { | |
type = Character.toLowerCase(type); | |
switch (type) { | |
case 'p': | |
return new PneumaticMotor(model, power); | |
case 'h': | |
return new HydraulicMotor(model, power); | |
case 'e': | |
return new ElectricMotor(model, power); | |
case 'w': | |
return new WarpDrive(model, power); | |
default: | |
throw new RuntimeException("Invalid type."); | |
} | |
} | |
} | |
/* Do not change code below */ | |
class Motor { | |
String model; | |
long power; | |
public Motor(String model, long power) { | |
this.model = model; | |
this.power = power; | |
} | |
} | |
class PneumaticMotor extends Motor { | |
public PneumaticMotor(String model, long power) { | |
super(model, power); | |
} | |
} | |
class HydraulicMotor extends Motor { | |
public HydraulicMotor(String model, long power) { | |
super(model, power); | |
} | |
} | |
class ElectricMotor extends Motor { | |
public ElectricMotor(String model, long power) { | |
super(model, power); | |
} | |
} | |
class WarpDrive extends Motor { | |
public WarpDrive(String model, long power) { | |
super(model, power); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment