You just need a standard server type. You don’t care how it’s built; you just tell the factory what “tier” you want.
- The interface
public interface EC2Instance {
void start();
}
- The implementations
public class MicroInstance implements EC2Instance { /*...*/ }
public class ComputeOptimizedInstance implements EC2Instance { /*...*/ }
- The Factory (Making the decision)
public class InstanceFactory {
public static EC2Instance createInstance(String tier) {
return switch (tier.toLowerCase()) {
case "free-tier" -> new MicroInstance();
case "high-load" -> new ComputeOptimizedInstance();
default -> throw new IllegalArgumentException("Unknown tier");
};
}
}
- Usage: One line, decision made for you.
EC2Instance server = InstanceFactory.createInstance("free-tier");