java-design-patterns icon indicating copy to clipboard operation
java-design-patterns copied to clipboard

Emdedded value design pattern

Open shivu2002a opened this issue 3 years ago • 5 comments

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 avatar Oct 25 '22 06:10 shivu2002a

@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 avatar Oct 25 '22 10:10 robertvolkmann

@robertvolkmann should I implement this using an in-memory database?

shivu2002a avatar Oct 27 '22 07:10 shivu2002a

@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.

iluwatar avatar Nov 05 '22 09:11 iluwatar

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
No Duplication information No Duplication information

sonarqubecloud[bot] avatar Nov 27 '22 04:11 sonarqubecloud[bot]

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.

stale[bot] avatar Jan 03 '23 10:01 stale[bot]

Closed due to inactivity. Thank you for your contributions.

stale[bot] avatar Feb 17 '23 11:02 stale[bot]