标签:reg ace final com username rri red return int
public class CQRS {
/**
* Command Query Responsibility Segregation【命令查询职责分离】:
* 查询操作是幂等的,可以放心使用;但是使用命令操作时需要加倍小心。
* CQRS 一般和Event Sourcing【事件溯源】一起在 Domain Driven Design【领域驱动设计】中使用。
*/
@Test
public void all() {
final UserCommand userCommand = new UserCommandImpl();
userCommand.add(new User("zxd", 1));
userCommand.add(new User("kristy", 2));
final UserQuery userQuery = new UserQueryImpl();
final User user = userQuery.getByName("zxd");
assertNotNull(user);
userCommand.update(new User("zxd", 3));
userCommand.delete("zxd");
}
}
@Data
class User{
private final String name;
private final int order;
}
interface UserCommand{
void add(User user);
void update(User user);
void delete(String username);
}
interface UserQuery{
User getByName(String name);
}
class UserCommandImpl implements UserCommand{
@Override
public void add(User user) {
SharedSource.USERS.putIfAbsent(user.getName(), user);
}
@Override
public void update(User user) {
SharedSource.USERS.putIfAbsent(user.getName(), user);
}
@Override
public void delete(String username) {
SharedSource.USERS.remove(username);
}
}
class UserQueryImpl implements UserQuery{
@Override
public User getByName(String name) {
return SharedSource.USERS.get(name);
}
}
interface SharedSource {
ConcurrentMap<String, User> USERS = new ConcurrentHashMap<>();
}
标签:reg ace final com username rri red return int
原文地址:https://www.cnblogs.com/zhuxudong/p/10192859.html