Skip to main content

Posts

Showing posts from August, 2017

Generic Service Locator in Java for Game Development

ServiceLocator is a very useful decoupling pattern to avoid too many Singletons. It's structure is always the same, so I thought it should be possible to create a generic one, to decrease code duplication. Well, here is what I've come up with,  guess someone with more knowledge about generics could create something better, but it is better than nothing. PS: I thought it must be possible to have a static method generating NulServices / Stub Services in the Classes implementing IService, but I couldn't find out how to demand a static method createNullService in an interface. Tell me if you have an idea. Here ist the abstract ServiceLocator import java.util.HashMap; import java.util.Map; /** * IServiceLocator * * @author Georg Eckert 2017 */ public abstract class ServiceLocator { private final Map<Class<? extends IService>, IService> services, nullServices; public ServiceLocator() { services = new HashMap<>(); null