Emdedded value design pattern
Embedded Value Pattern
- Implementation of Embedded value pattern
- #1295
Description
- Implementation of Embedded value design pattern
- Complete pattern implementation along with class diagram and README
- Implementation of test-cases
- Used H2 database
- I couldn't find enough resources for real-world examples and no Wikipedia page describing this pattern
@shivu2002a The whole JDBC and H2 stuff complicates this simple pattern. What about using a custom inmemory database, that is only a wrapper around an ArrayList?
@iluwatar What's your opinion?
Sketch:
public class OrderStore {
private final ArrayList<OrderItem> store;
public OrderStore() {
store = new ArrayList<>();
}
public Order insert(Order order) {
var item = new OrderItem( /* values from order */ )
if (store.add(item)) {
return new Order(store.size() - 1, /* other values from order */);
}
return null;
}
public List<Order> getAll() {
return store.stream()
.filter(Objects::nonNull)
.map( /* map to Order */ )
.collect(Collectors.toList());
}
public void remove(int id) {
store[id] = null;
}
private static class OrderItem {
/* properties */
}
}
@robertvolkmann should I implement this using an in-memory database?
@shivu2002a The whole JDBC and H2 stuff complicates this simple pattern. What about using a custom inmemory database, that is only a wrapper around an ArrayList?
@iluwatar What's your opinion?
Sketch:
public class OrderStore { private final ArrayList<OrderItem> store; public OrderStore() { store = new ArrayList<>(); } public Order insert(Order order) { var item = new OrderItem( /* values from order */ ) if (store.add(item)) { return new Order(store.size() - 1, /* other values from order */); } return null; } public List<Order> getAll() { return store.stream() .filter(Objects::nonNull) .map( /* map to Order */ ) .collect(Collectors.toList()); } public void remove(int id) { store[id] = null; } private static class OrderItem { /* properties */ } }
We can do it either way. You are right that this would make the code simpler but on the other this is a data access pattern and using a real(ish) database is justified.
This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.
Closed due to inactivity. Thank you for your contributions.







