Modularity and Decoupling, The Right Way

Modularity and Decoupling, The Right Way

Creating and maintaining a Java codebase is not an easy task, especially with a codebase with thousands of tests. We often try to solve this problem with new libraries, frameworks and even new languages that can create a false sense of decoupling and modularity.
But the answer is not in the libraries, nor in the frameworks. The answer is in the core of the Java language itself and it has been there since the early stages.
In this talk, you will learn how to organize your codebase in such a way that your code is really decoupled and modularized, and so that you don't have to modify your tests when you decide to change some implementation details (like choosing a different HTTP library or a different database abstraction layer).

66059d4f17b6b7a6888d62caa736a663?s=128

Carlos Chacin

July 10, 2019
Tweet

Transcript

  1. 2.

    • Replace your http client changing only 1 or 2

    lines? • Rename an interface method changing only 1 or 2 lines? • Have unit tests without a mocking framework? • Rename an impl without changing mocks in tests? • Hide your implementation details? • Instantiate your application without a DI framework? Can you...
  2. 6.

    package com.example.app.domain; public class CacheImpl implements Cache { @Inject public

    Cache(final Jedis jedis) { } } @ExtendWith(MockitoExtension.class) class CacheImplTest { @Test void test(@Mock Jedis jedis) throws Exception { when(jedis.get(“a”)).thenReturn(“b”); assertThat(cache.get(“a”)).isEqualTo(“b”); verify(jedis).get(“a”); } }
  3. 13.
  4. 19.

    View Logic Cache Persistence Redis File System PostgreSQL Json API

    CustomerDTO @Inject @Inject @Inject @Inject @Inject @Inject
  5. 20.

    Device Logic Cache Persistence Redis File System PostgreSQL Json API

    CustomerDTO @Inject @Inject @Inject @Inject @Inject @Inject
  6. 23.
  7. 24.
  8. 25.
  9. 26.

    Device Logic Cache Persistence Redis File System PostgreSQL Json API

    CustomerDTO @Inject @Inject @Inject @Inject @Inject @Inject
  10. 28.

    public interface CustomerCache { void saveToCache(final Customer customer); Optional<Customer> getFromCache(final

    UUID id); void method1(); String method2(); } @Alternative public class FileSystemCache implements CustomerCache { @Override public void saveToCache(Customer customer) { System.out.println("SAVED TO FILE SYSTEM"); } @Override public Optional<Customer> getFromCache(UUID id) { System.out.println("RETRIEVED FROM FILE SYSTEM"); return Optional.empty(); } @Override public void method1() {} @Override public String method2() { return null; } }
  11. 29.

    Device Logic Cache Persistence Redis File System PostgreSQL Json API

    CustomerDTO @Inject @Inject @Inject @Inject @Inject @Inject
  12. 30.

    public interface CustomerRepository { public void saveToDb(final Customer customer); public

    Optional<Customer> getFromDb(final UUID id); public void method1(); public String method2(); }
  13. 31.

    public interface CustomerRepository { public void saveToDb(final Customer customer); public

    Optional<Customer> getFromDb(final UUID id); public void method1(); public String method2(); } @Alternative public class DbRepository implements CustomerRepository { @Override public void saveToDb(Customer customer) { System.out.println("SAVED TO DATABASE"); } @Override public Optional<Customer> getFromDb(UUID id) { System.out.println("RETRIEVED FROM DATABASE"); return Optional.empty(); } @Override public void method1() {} @Override public String method2() { return null; } }
  14. 32.

    Device Logic Cache Persistence Redis File System PostgreSQL Json API

    CustomerDTO @Inject @Inject @Inject @Inject @Inject @Inject
  15. 33.

    public Optional<Customer> find(final UUID id) { Optional<Customer> cachedCustomer = this.cache.getFromCache(id);

    if (cachedCustomer.isPresent()) { return cachedCustomer; } else { Optional<Customer> dbCustomer = this.repository.getFromDb(id); if (dbCustomer.isPresent()) { this.cache.saveToCache(dbCustomer.get()); return dbCustomer; } } return Optional.empty(); }
  16. 34.

    public class CustomerEndpoint { private final CustomerService service; @Inject public

    CustomerEndpoint(final CustomerService service) { this.service = service; } public void create(Customer customer) { service.create(customer); } public Optional<Customer> find(UUID id) { return service.find(id); } }
  17. 35.

    @Test @DisplayName("return customer from db when not present in cache")

    void return_customer_from_db_when_not_present_in_cache( @Mock CustomerRepository repository, @Mock CustomerCache cache) throws Exception { // Given var customer = Customer.create(id, id.toString()); when(cache.getFromCache(id)).thenReturn(Optional.empty()); when(repository.getFromDb(id)).thenReturn(Optional.of(customer)); // When var sut = new CustomerService(repository, cache); // Then assertThat(sut.find(id)).contains(Customer.create(id, id.toString())); verify(cache).saveToCache(customer); }
  18. 36.
  19. 37.
  20. 38.
  21. 39.
  22. 40.
  23. 41.
  24. 43.
  25. 44.
  26. 45.
  27. 46.
  28. 47.
  29. 67.
  30. 68.
  31. 69.
  32. 70.
  33. 71.
  34. 72.
  35. 74.

    public interface Cache {} public final class Customer {} public

    final class CustomerEndpoint {} public interface CustomerEndpointFactory {} public final class DbRepository {} public final class FileSystemCache {} public final class HttpRepository {} public final class RedisCache {} public interface Repository {} public final class Service {}
  36. 75.

    public interface Cache {} public final class Customer {} public

    final class CustomerEndpoint {} public interface CustomerEndpointFactory {} public final class DbRepository {} public final class FileSystemCache {} public final class HttpRepository {} public final class RedisCache {} public interface Repository {} public final class Service {}
  37. 76.

    public interface Cache {} public final class Customer {} public

    final class CustomerEndpoint {} public interface CustomerEndpointFactory {} public final class DbRepository {} public final class FileSystemCache {} public final class HttpRepository {} public final class RedisCache {} public interface Repository {} public final class Service {}
  38. 77.

    public interface Cache {} public final class Customer {} public

    final class CustomerEndpoint {} public interface CustomerEndpointFactory {} public final class DbRepository {} public final class FileSystemCache {} public final class HttpRepository {} public final class RedisCache {} public interface Repository {} public final class Service {}
  39. 78.
  40. 80.
  41. 81.

    public class CustomerService { private final CustomerRepository repository; private final

    CustomerCache cache; @Inject public CustomerService(final CustomerRepository repository, final CustomerCache cache) { this.repository = repository; this.cache = cache; } public void create(final Customer customer) { this.repository.saveToDb(customer); this.cache.saveToCache(customer); } public Optional<Customer> find(final UUID id) { final Optional<Customer> cachedCustomer = this.cache.getFromCache(id); if (cachedCustomer.isPresent()) { return cachedCustomer; } else { final Optional<Customer> dbCustomer = this.repository.getFromDb(id); if (dbCustomer.isPresent()) { this.cache.saveToCache(dbCustomer.get()); return dbCustomer; } } return Optional.empty(); } }
  42. 82.

    public class CustomerService { private final CustomerRepository repository; private final

    CustomerCache cache; @Inject public CustomerService(final CustomerRepository repository, final CustomerCache cache) { this.repository = repository; this.cache = cache; } public void create(final Customer customer) { this.repository.saveToDb(customer); this.cache.saveToCache(customer); } public Optional<Customer> find(final UUID id) { final Optional<Customer> cachedCustomer = this.cache.getFromCache(id); if (cachedCustomer.isPresent()) { return cachedCustomer; } else { final Optional<Customer> dbCustomer = this.repository.getFromDb(id); if (dbCustomer.isPresent()) { this.cache.saveToCache(dbCustomer.get()); return dbCustomer; } } return Optional.empty(); } }
  43. 83.

    public class CustomerService { private final CustomerRepository repository; private final

    CustomerCache cache; @Inject public CustomerService(final CustomerRepository repository, final CustomerCache cache) { this.repository = repository; this.cache = cache; } public void create(final Customer customer) { this.repository.saveToDb(customer); this.cache.saveToCache(customer); } public Optional<Customer> find(final UUID id) { final Optional<Customer> cachedCustomer = this.cache.getFromCache(id); if (cachedCustomer.isPresent()) { return cachedCustomer; } else { final Optional<Customer> dbCustomer = this.repository.getFromDb(id); if (dbCustomer.isPresent()) { this.cache.saveToCache(dbCustomer.get()); return dbCustomer; } } return Optional.empty(); } }
  44. 84.

    @Test @DisplayName("return customer from db when not present in cache")

    void return_customer_from_db_when_not_present_in_cache( @Mock CustomerRepository repository, @Mock CustomerCache cache) throws Exception { // Given var customer = Customer.create(id, id.toString()); when(cache.getFromCache(id)).thenReturn(Optional.empty()); when(repository.getFromDb(id)).thenReturn(Optional.of(customer)); // When var sut = new CustomerService(repository, cache); // Then assertThat(sut.find(id)).contains(Customer.create(id, id.toString())); verify(cache).saveToCache(customer); }
  45. 85.

    @Test @DisplayName("return customer from db when not present in cache")

    void return_customer_from_db_when_not_present_in_cache( @Mock CustomerRepository repository, @Mock CustomerCache cache) throws Exception { // Given var customer = Customer.create(id, id.toString()); when(cache.getFromCache(id)).thenReturn(Optional.empty()); when(repository.getFromDb(id)).thenReturn(Optional.of(customer)); // When var sut = new CustomerService(repository, cache); // Then assertThat(sut.find(id)).contains(Customer.create(id, id.toString())); verify(cache).saveToCache(customer); }
  46. 86.
  47. 88.

    @Test @DisplayName("return customer from db when not present in cache")

    void return_customer_from_db_when_not_present_in_cache( @Mock CustomerRepository repository, @Mock CustomerCache cache) throws Exception { // Given var customer = Customer.create(id, id.toString()); when(cache.getFromCache(id)).thenReturn(Optional.empty()); when(repository.getFromDb(id)).thenReturn(Optional.of(customer)); // When var sut = new CustomerService(repository, cache); // Then assertThat(sut.find(id)).contains(Customer.create(id, id.toString())); verify(cache).saveToCache(customer); }
  48. 89.

    @Test @DisplayName("return customer from db when not present in cache")

    void return_customer_from_db_when_not_present_in_cache( @Mock CustomerRepository repository, @Mock CustomerCache cache) throws Exception { // Given var customer = Customer.create(id, id.toString()); when(cache.getFromCache(id)).thenReturn(Optional.empty()); when(repository.getFromDb(id)).thenReturn(Optional.of(customer)); // When var sut = new CustomerService(repository, cache); // Then assertThat(sut.find(id)).contains(Customer.create(id, id.toString())); verify(cache).saveToCache(customer); }
  49. 90.

    var cache = new Cache() { @Override public void saveToCache(Customer

    customer) {} @Override public Optional<Customer> getFromCache(UUID id) { return Optional.empty(); } @Override public void method1() {} @Override public String method2() { return null; } }; @Test @DisplayName("return customer from db when not present in cache") void return_customer_from_db_when_not_present_in_cache( @Mock CustomerRepository repository, @Mock CustomerCache cache) throws Exception { // Given var customer = Customer.create(id, id.toString()); when(cache.getFromCache(id)).thenReturn(Optional.empty()); when(repository.getFromDb(id)).thenReturn(Optional.of(customer)); // When var sut = new CustomerService(repository, cache); // Then assertThat(sut.find(id)).contains(Customer.create(id, id.toString())); verify(cache).saveToCache(customer); }
  50. 91.

    var cache = new Cache() { @Override public void saveToCache(Customer

    customer) {} @Override public Optional<Customer> getFromCache(UUID id) { return Optional.empty(); } @Override public void method1() {} @Override public String method2() { return null; } }; @Test @DisplayName("return customer from db when not present in cache") void return_customer_from_db_when_not_present_in_cache( @Mock CustomerRepository repository, @Mock CustomerCache cache) throws Exception { // Given var customer = Customer.create(id, id.toString()); when(cache.getFromCache(id)).thenReturn(Optional.empty()); when(repository.getFromDb(id)).thenReturn(Optional.of(customer)); // When var sut = new CustomerService(repository, cache); // Then assertThat(sut.find(id)).contains(Customer.create(id, id.toString())); verify(cache).saveToCache(customer); }
  51. 93.
  52. 94.
  53. 95.

    SAM

  54. 96.

    public interface Function<T, R> { R apply(T t); } public

    interface Consumer<T> { void accept(T t); } public interface Supplier<T> { T get(); }
  55. 97.

    public interface Function<T, R> { R apply(T t); } public

    interface Consumer<T> { void accept(T t); } public interface Supplier<T> { T get(); } Function<UUID, Optional<Customer>> getFromCache Function<UUID, Optional<Customer>> getFromDb Consumer<Customer> saveToCache Consumer<Customer> saveToDb
  56. 98.

    package java.util.function; public interface Function<T, R> { R apply(T t);

    } public interface Consumer<T> { void accept(T t); } public interface Supplier<T> { T get(); } Function<UUID, Optional<Customer>> getFromCache Function<UUID, Optional<Customer>> getFromDb Consumer<Customer> saveToCache Consumer<Customer> saveToDb
  57. 99.

    @Inject public CustomerService( CustomerRepository repository, CustomerCache cache) { this.repository =

    repository; this.cache = cache; } What is really required to build this class?
  58. 100.

    @Inject CustomerService( Function<UUID, Optional<Customer>> getFromCache, Function<UUID, Optional<Customer>> getFromDb, Consumer<Customer> saveToDb,

    Consumer<Customer> saveToCache) { // Omitted } • No More Coupling with Repository / Cache • We know explicitly what is required from this class
  59. 103.

    var sut = new Service( uuid -> Optional.empty(), uuid ->

    Optional.of(customer), cust -> {}, cust -> {} ); • No More Coupling with Repository / Cache • No more mocks • No more tests refactors because renames in production code when(cache.getFromCache(id)) .thenReturn(Optional.empty()); when(repo.getFromDb(id)) .thenReturn(Optional.of(cust));
  60. 104.

    var sut = new Service( uuid -> Optional.empty(), uuid ->

    Optional.of(customer), cust -> {}, cust -> {} ); • No More Coupling with Repository / Cache • No more mocks • No more tests refactors because renames in production code when(cache.getFromCache(id)) .thenReturn(Optional.empty()); when(repo.getFromDb(id)) .thenReturn(Optional.of(cust));
  61. 105.

    var sut = new Service( uuid -> Optional.empty(), uuid ->

    Optional.of(customer), cust -> {}, cust -> {} ); • No More Coupling with Repository / Cache • No more mocks • No more tests refactors because renames in production code when(cache.getFromCache(id)) .thenReturn(Optional.empty()); when(repo.getFromDb(id)) .thenReturn(Optional.of(cust));
  62. 106.

    var sut = new Service( uuid -> Optional.empty(), uuid ->

    Optional.of(customer), cust -> {}, cust -> {} ); • No More Coupling with Repository / Cache • No more mocks • No more tests refactors because renames in production code when(cache.getFromCache(id)) .thenReturn(Optional.empty()); when(repo.getFromDb(id)) .thenReturn(Optional.of(cust));
  63. 107.

    public interface AppFactory { static CustomerEndpoint endpoint() { var dbRepository

    = new DbRepository(); var fileSystemCache = new FileSystemCache(); var service = new Service( fileSystemCache::getFromCache, dbRepository::getFromDb, dbRepository::saveToDb, fileSystemCache::saveToCache ); return new CustomerEndpoint( service::find, service::create ); } }
  64. 108.
  65. 109.
  66. 110.
  67. 111.
  68. 112.
  69. 113.

    • Higher modularity • Easier code navigation • Higher level

    of abstraction • Separates both features and layers • More readable and maintainable structure • More cohesion • Much easier to scale • Less chance to accidentally modify unrelated classes or files • Much easier to add or remove application features By looking at the structure you can already tell what the app is all about
  70. 114.

    ★ Replace your http client changing only 1 or 2

    lines ★ Rename an interface method changing only 1 or 2 lines ★ Have unit tests without a mocking framework ★ Rename an impl without changing mocks in tests ★ Hide your implementation details ★ Instantiate your application without a DI framework Yes you can...
  71. 116.