Created
October 22, 2012 10:29
-
-
Save raulraja/3930813 to your computer and use it in GitHub Desktop.
Sample Java Abstract 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
interface PersistenceService { | |
void save(); | |
} | |
class DatabasePersistenceService implements PersistenceService { | |
public void save() { | |
// save to database | |
} | |
} | |
class MemoryPersistenceService implements PersistenceService { | |
public void save() { | |
// save to memory | |
} | |
} | |
class ServiceFactory { | |
private static PersistenceService persistenceService; | |
public static PersistenceService getPersistenceService() { | |
return persistenceService; | |
} | |
public static void setPersistenceService(PersistenceService persistenceService) { | |
ServiceFactory.persistenceService = persistenceService; | |
} | |
} | |
public class Application { | |
public static void main(String[] args) { | |
ServiceFactory.setPersistenceService(new DatabasePersistenceService()); | |
PersistenceService persistenceService = ServiceFactory.getPersistenceService(); | |
persistenceService.save(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment