Last active
March 9, 2016 08:54
-
-
Save dodalovic/99aff2a2e6407f196204 to your computer and use it in GitHub Desktop.
Strategy pattern - User persistence - java 8
This file contains hidden or 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
package rs.dodalovic.design_patterns.behavioral.strategy.user_persistence; | |
class User { | |
private final int id; | |
private final String username; | |
private final String location; | |
public User(int id, String username, String location) { | |
this.id = id; | |
this.username = username; | |
this.location = location; | |
} | |
@Override | |
public String toString() { | |
return String.format("User{id=%d, username='%s', location='%s'}", id, username, location); | |
} | |
} |
This file contains hidden or 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
package rs.dodalovic.design_patterns.behavioral.strategy.user_persistence; | |
import java.util.function.Consumer; | |
interface UserLoggers { | |
Consumer<User> CONSOLE = user -> System.out.format("Logging user [%s] to console...%n", user.toString()); | |
Consumer<User> SENTRY = user -> System.out.format("Logging user [%s] to sentry...%n", user.toString()); | |
} |
This file contains hidden or 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
package rs.dodalovic.design_patterns.behavioral.strategy.user_persistence; | |
import java.util.function.Consumer; | |
import java.util.function.Function; | |
class UserPersistenceDirector { | |
private final int userId; | |
private final Function<Integer, User> userProvider; | |
private final Consumer<User> userPersister; | |
private Consumer<User> userLogger; | |
public UserPersistenceDirector(int userId, Function<Integer, User> userProvider, Consumer<User> userPersister) { | |
this.userId = userId; | |
this.userProvider = userProvider; | |
this.userPersister = userPersister; | |
} | |
public UserPersistenceDirector(int userId, Function<Integer, User> userProvider, Consumer<User> userPersister, | |
Consumer<User> userLogger) { | |
this(userId, userProvider, userPersister); | |
this.userLogger = userLogger; | |
} | |
public void persist() { | |
final User user = userProvider.apply(userId); | |
userLogger.accept(user); | |
userPersister.accept(user); | |
} | |
} |
This file contains hidden or 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
package rs.dodalovic.design_patterns.behavioral.strategy.user_persistence; | |
import java.util.function.Consumer; | |
interface UserPersisters { | |
Consumer<User> MONGO = user -> System.out.format("Persisting user [%s] to Mongo DB...%n", user.toString()); | |
Consumer<User> MYSQL = user -> System.out.format("Persisting user [%s] to MySQL DB...%n", user.toString()); | |
} |
This file contains hidden or 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
package rs.dodalovic.design_patterns.behavioral.strategy.user_persistence; | |
import com.google.common.collect.ImmutableMap; | |
import java.util.Map; | |
import java.util.function.Consumer; | |
import java.util.function.Function; | |
public class UserPersistMain { | |
private static final Map<String, Function<Integer, User>> userProviderMapping = ImmutableMap.of( | |
"fs-provider", UserProviders.FILE_SYSTEM, | |
"web-service-provider", UserProviders.WEB_SERVICE | |
); | |
private static final Map<String, Consumer<User>> userPersisterMapping = ImmutableMap.of( | |
"mongo-persister", UserPersisters.MONGO, | |
"mysql-persister", UserPersisters.MYSQL | |
); | |
private static final Map<String, Consumer<User>> userLoggerMapping = ImmutableMap.of( | |
"console-logger", UserLoggers.CONSOLE, | |
"sentry-logger", UserLoggers.SENTRY | |
); | |
public static void main(String[] args) { | |
int userId = Integer.parseInt(args[0]); | |
final Function<Integer, User> userProvider = userProviderMapping.get(args[1]); | |
final Consumer<User> userPersister = userPersisterMapping.get(args[2]); | |
final Consumer<User> userLogger = args.length == 4 ? userLoggerMapping.get(args[3]) : UserLoggers.CONSOLE; | |
new UserPersistenceDirector(userId, userProvider, userPersister, userLogger).persist(); | |
} | |
} | |
This file contains hidden or 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
package rs.dodalovic.design_patterns.behavioral.strategy.user_persistence; | |
import java.util.function.Function; | |
interface UserProviders { | |
Function<Integer, User> FILE_SYSTEM = userId -> { | |
System.out.println("Retrieving user data from file system..."); | |
return new User(userId, "Mike", "United States"); | |
}; | |
Function<Integer, User> WEB_SERVICE = userId -> { | |
System.out.println("Retrieving user data from web service..."); | |
return new User(userId, "Jane", "Canada"); | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment