This commit is contained in:
Jonathan Cook
2019-10-23 15:01:44 +02:00
parent db85c8f275
commit 684ec0d2e3
20486 changed files with 1642483 additions and 0 deletions
+3
View File
@@ -0,0 +1,3 @@
## Relevant Articles:
- [The Dependency Inversion Principle in Java](https://www.baeldung.com/java-dependency-inversion-principle)
@@ -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<Integer, Customer> customers = new HashMap<>();
public SimpleCustomerDao() {
}
public SimpleCustomerDao(Map<Integer, Customer> customers) {
this.customers = customers;
}
@Override
public Optional<Customer> findById(int id) {
return Optional.ofNullable(customers.get(id));
}
@Override
public List<Customer> findAll() {
return new ArrayList<>(customers.values());
}
}
@@ -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;
}
@@ -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<Customer> findById(int id);
List<Customer> findAll();
}
@@ -0,0 +1,4 @@
module com.baeldung.dip.daos {
requires com.baeldung.dip.entities;
exports com.baeldung.dip.daos;
}
@@ -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 + '}';
}
}
@@ -0,0 +1,3 @@
module com.baeldung.dip.entities {
exports com.baeldung.dip.entities;
}
@@ -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<Integer, Customer>();
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);
}
}
@@ -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;
}
@@ -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<Customer> findById(int id) {
return customerDao.findById(id);
}
public List<Customer> findAll() {
return customerDao.findAll();
}
}
@@ -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;
}