From 5754bb33100da75623c3774233fa9eab73e367c8 Mon Sep 17 00:00:00 2001 From: Alejandro Gervasio Date: Sat, 30 Mar 2019 13:01:53 -0300 Subject: [PATCH] Initial Commit (#6614) --- .../daoimplementations/SimpleCustomerDao.java | 30 +++++++++++++++++++ .../module-info.java | 6 ++++ .../com/baeldung/dip/daos/CustomerDao.java | 13 ++++++++ .../com.baeldung.dip.daos/module-info.java | 4 +++ .../com/baeldung/dip/entities/Customer.java | 19 ++++++++++++ .../module-info.java | 3 ++ .../baeldung/dip/mainapp/MainApplication.java | 17 +++++++++++ .../com.baeldung.dip.mainapp/module-info.java | 7 +++++ .../dip/services/CustomerService.java | 23 ++++++++++++++ .../module-info.java | 6 ++++ 10 files changed, 128 insertions(+) create mode 100644 patterns/dipmodular/com.baeldung.dip.daoimplementations/com/baeldung/dip/daoimplementations/SimpleCustomerDao.java create mode 100644 patterns/dipmodular/com.baeldung.dip.daoimplementations/module-info.java create mode 100644 patterns/dipmodular/com.baeldung.dip.daos/com/baeldung/dip/daos/CustomerDao.java create mode 100644 patterns/dipmodular/com.baeldung.dip.daos/module-info.java create mode 100644 patterns/dipmodular/com.baeldung.dip.entities/com/baeldung/dip/entities/Customer.java create mode 100644 patterns/dipmodular/com.baeldung.dip.entities/module-info.java create mode 100644 patterns/dipmodular/com.baeldung.dip.mainapp/com/baeldung/dip/mainapp/MainApplication.java create mode 100644 patterns/dipmodular/com.baeldung.dip.mainapp/module-info.java create mode 100644 patterns/dipmodular/com.baeldung.dip.services/com/baeldung/dip/services/CustomerService.java create mode 100644 patterns/dipmodular/com.baeldung.dip.services/module-info.java diff --git a/patterns/dipmodular/com.baeldung.dip.daoimplementations/com/baeldung/dip/daoimplementations/SimpleCustomerDao.java b/patterns/dipmodular/com.baeldung.dip.daoimplementations/com/baeldung/dip/daoimplementations/SimpleCustomerDao.java new file mode 100644 index 0000000000..ef6482ecc7 --- /dev/null +++ b/patterns/dipmodular/com.baeldung.dip.daoimplementations/com/baeldung/dip/daoimplementations/SimpleCustomerDao.java @@ -0,0 +1,30 @@ +package com.baeldung.dip.daoimplementations; + +import com.baeldung.dip.daos.CustomerDao; +import com.baeldung.dip.entities.Customer; +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; + +public class SimpleCustomerDao implements CustomerDao { + + private Map customers = new HashMap<>(); + + public SimpleCustomerDao() { + + } + + public SimpleCustomerDao(Map customers) { + this.customers = customers; + } + + @Override + public Optional findById(int id) { + return Optional.ofNullable(customers.get(id)); + } + + @Override + public List findAll() { + return new ArrayList<>(customers.values()); + } +} diff --git a/patterns/dipmodular/com.baeldung.dip.daoimplementations/module-info.java b/patterns/dipmodular/com.baeldung.dip.daoimplementations/module-info.java new file mode 100644 index 0000000000..1df40ce3b8 --- /dev/null +++ b/patterns/dipmodular/com.baeldung.dip.daoimplementations/module-info.java @@ -0,0 +1,6 @@ +module com.baeldung.dip.daoimplementations { + requires com.baeldung.dip.entities; + requires com.baeldung.dip.daos; + provides com.baeldung.dip.daos.CustomerDao with com.baeldung.dip.daoimplementations.SimpleCustomerDao; + exports com.baeldung.dip.daoimplementations; +} diff --git a/patterns/dipmodular/com.baeldung.dip.daos/com/baeldung/dip/daos/CustomerDao.java b/patterns/dipmodular/com.baeldung.dip.daos/com/baeldung/dip/daos/CustomerDao.java new file mode 100644 index 0000000000..b0080dd15e --- /dev/null +++ b/patterns/dipmodular/com.baeldung.dip.daos/com/baeldung/dip/daos/CustomerDao.java @@ -0,0 +1,13 @@ +package com.baeldung.dip.daos; + +import com.baeldung.dip.entities.Customer; +import java.util.Map; +import java.util.Optional; + +public interface CustomerDao { + + Optional findById(int id); + + List findAll(); + +} diff --git a/patterns/dipmodular/com.baeldung.dip.daos/module-info.java b/patterns/dipmodular/com.baeldung.dip.daos/module-info.java new file mode 100644 index 0000000000..897c6168a1 --- /dev/null +++ b/patterns/dipmodular/com.baeldung.dip.daos/module-info.java @@ -0,0 +1,4 @@ +module com.baeldung.dip.daos { + requires com.baeldung.dip.entities; + exports com.baeldung.dip.daos; +} diff --git a/patterns/dipmodular/com.baeldung.dip.entities/com/baeldung/dip/entities/Customer.java b/patterns/dipmodular/com.baeldung.dip.entities/com/baeldung/dip/entities/Customer.java new file mode 100644 index 0000000000..f8d5d82102 --- /dev/null +++ b/patterns/dipmodular/com.baeldung.dip.entities/com/baeldung/dip/entities/Customer.java @@ -0,0 +1,19 @@ +package com.baeldung.dip.entities; + +public class Customer { + + private final String name; + + public Customer(String name) { + this.name = name; + } + + public String getName() { + return name; + } + + @Override + public String toString() { + return "Customer{" + "name=" + name + '}'; + } +} diff --git a/patterns/dipmodular/com.baeldung.dip.entities/module-info.java b/patterns/dipmodular/com.baeldung.dip.entities/module-info.java new file mode 100644 index 0000000000..da8c1ccaee --- /dev/null +++ b/patterns/dipmodular/com.baeldung.dip.entities/module-info.java @@ -0,0 +1,3 @@ +module com.baeldung.dip.entities { + exports com.baeldung.dip.entities; +} diff --git a/patterns/dipmodular/com.baeldung.dip.mainapp/com/baeldung/dip/mainapp/MainApplication.java b/patterns/dipmodular/com.baeldung.dip.mainapp/com/baeldung/dip/mainapp/MainApplication.java new file mode 100644 index 0000000000..8972dc3994 --- /dev/null +++ b/patterns/dipmodular/com.baeldung.dip.mainapp/com/baeldung/dip/mainapp/MainApplication.java @@ -0,0 +1,17 @@ +package com.baeldung.dip.mainapp; + +import com.baeldung.dip.daoimplementations.MapCustomerDao; +import com.baeldung.dip.entities.Customer; +import com.baeldung.dip.services.CustomerService; +import java.util.HashMap; + +public class MainApplication { + + public static void main(String args[]) { + var customers = new HashMap(); + customers.put(1, new Customer("John")); + customers.put(2, new Customer("Susan")); + CustomerService customerService = new CustomerService(new SimpleCustomerDao(customers)); + customerService.findAll().forEach(System.out::println); + } +} diff --git a/patterns/dipmodular/com.baeldung.dip.mainapp/module-info.java b/patterns/dipmodular/com.baeldung.dip.mainapp/module-info.java new file mode 100644 index 0000000000..466306334a --- /dev/null +++ b/patterns/dipmodular/com.baeldung.dip.mainapp/module-info.java @@ -0,0 +1,7 @@ +module com.baeldung.dip.mainapp { + requires com.baeldung.dip.entities; + requires com.baeldung.dip.daos; + requires com.baeldung.dip.daoimplementations; + requires com.baeldung.dip.services; + exports com.baeldung.dip.mainapp; +} diff --git a/patterns/dipmodular/com.baeldung.dip.services/com/baeldung/dip/services/CustomerService.java b/patterns/dipmodular/com.baeldung.dip.services/com/baeldung/dip/services/CustomerService.java new file mode 100644 index 0000000000..39955abdaf --- /dev/null +++ b/patterns/dipmodular/com.baeldung.dip.services/com/baeldung/dip/services/CustomerService.java @@ -0,0 +1,23 @@ +package com.baeldung.dip.services; + +import com.baeldung.dip.daos.CustomerDao; +import com.baeldung.dip.entities.Customer; +import java.util.Map; +import java.util.Optional; + +public class CustomerService { + + private final CustomerDao customerDao; + + public CustomerService(CustomerDao customerDao) { + this.customerDao = customerDao; + } + + public Optional findById(int id) { + return customerDao.findById(id); + } + + public List findAll() { + return customerDao.findAll(); + } +} diff --git a/patterns/dipmodular/com.baeldung.dip.services/module-info.java b/patterns/dipmodular/com.baeldung.dip.services/module-info.java new file mode 100644 index 0000000000..8e59cd7abe --- /dev/null +++ b/patterns/dipmodular/com.baeldung.dip.services/module-info.java @@ -0,0 +1,6 @@ +module com.baeldung.dip.services { + requires com.baeldung.dip.entities; + requires com.baeldung.dip.daos; + uses com.baeldung.dip.daos.CustomerDao; + exports com.baeldung.dip.services; +}