BAEL-2789 - The Dependency Inversion Principle in Java (#6613)

* Initial Commit

* Added dip dipmodular modules to pom.xml

* Delete pom.xml

* Add pom.xml

* Update pom.xml

* Update pom.xml

* Update pom.xml

* Update Application.java

* Update CustomerDaoUnitTest.java

* Update CustomerServiceUnitTest.java

* Update Application.java

* Update CustomerDaoUnitTest.java

* Update CustomerServiceUnitTest.java

* Update CustomerDaoUnitTest.java

* Update CustomerServiceUnitTest.java
This commit is contained in:
Alejandro Gervasio 2019-03-30 13:01:00 -03:00 committed by KevinGilmore
parent 86bded12be
commit b67a006020
9 changed files with 236 additions and 3 deletions

39
patterns/dip/pom.xml Normal file
View File

@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung.dip</groupId>
<artifactId>dip</artifactId>
<name>dip</name>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>patterns</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>..</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.12.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
</project>

View File

@ -0,0 +1,18 @@
package com.baeldung.dip.application;
import com.baeldung.dip.daoimplementations.SimpleCustomerDao;
import com.baeldung.dip.entities.Customer;
import com.baeldung.dip.services.CustomerService;
import java.util.Map;
import java.util.HashMap;
public class Application {
public static void main(String[] args) {
Map<Integer, Customer> 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);
}
}

View File

@ -0,0 +1,28 @@
package com.baeldung.dip.daoimplementations;
import com.baeldung.dip.entities.Customer;
import com.baeldung.dip.daointerfaces.CustomerDao;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
public class SimpleCustomerDao implements CustomerDao {
private Map<Integer, Customer> customers = new HashMap<>();
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());
}
}

View File

@ -0,0 +1,13 @@
package com.baeldung.dip.daointerfaces;
import com.baeldung.dip.entities.Customer;
import java.util.List;
import java.util.Optional;
public interface CustomerDao {
Optional<Customer> findById(int id);
List<Customer> findAll();
}

View File

@ -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 + '}';
}
}

View File

@ -0,0 +1,23 @@
package com.baeldung.dip.services;
import com.baeldung.dip.daointerfaces.CustomerDao;
import com.baeldung.dip.entities.Customer;
import java.util.List;
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();
}
}

View File

@ -0,0 +1,46 @@
package com.baeldung.dip.tests;
import com.baeldung.dip.daoimplementations.SimpleCustomerDao;
import com.baeldung.dip.daointerfaces.CustomerDao;
import com.baeldung.dip.entities.Customer;
import java.util.Map;
import java.util.HashMap;
import java.util.List;
import java.util.Optional;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Before;
import org.junit.Test;
public class CustomerDaoUnitTest {
private CustomerDao customerDao;
@Before
public void setUpCustomerDaoInstance() {
Map<Integer, Customer> customers = new HashMap<>();
customers.put(1, new Customer("John"));
customers.put(2, new Customer("Susan"));
customerDao = new SimpleCustomerDao(customers);
}
@Test
public void givenCustomerDaoInstance_whenCalledFindById_thenCorrect() {
assertThat(customerDao.findById(1)).isInstanceOf(Optional.class);
}
@Test
public void givenCustomerDaoInstance_whenCalledFindAll_thenCorrect() {
assertThat(customerDao.findAll()).isInstanceOf(List.class);
}
@Test
public void givenCustomerDaoInstance_whenCalledFindByIdWithNullCustomer_thenCorrect() {
Map<Integer, Customer> customers = new HashMap<Integer, Customer>();
customers.put(1, null);
CustomerDao customerDaoObject = new SimpleCustomerDao(customers);
Customer customer = customerDaoObject.findById(1).orElseGet(() -> new Customer("Non-existing customer"));
assertThat(customer.getName()).isEqualTo("Non-existing customer");
}
}

View File

@ -0,0 +1,46 @@
package com.baeldung.dip.tests;
import com.baeldung.dip.daoimplementations.SimpleCustomerDao;
import com.baeldung.dip.entities.Customer;
import com.baeldung.dip.services.CustomerService;
import java.util.Map;
import java.util.HashMap;
import java.util.List;
import java.util.Optional;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Before;
import org.junit.Test;
public class CustomerServiceUnitTest {
private CustomerService customerService;
@Before
public void setUpCustomerServiceInstance() {
Map<Integer, Customer> customers = new HashMap<>();
customers.put(1, new Customer("John"));
customers.put(2, new Customer("Susan"));
customerService = new CustomerService(new SimpleCustomerDao(customers));
}
@Test
public void givenCustomerServiceInstance_whenCalledFindById_thenCorrect() {
assertThat(customerService.findById(1)).isInstanceOf(Optional.class);
}
@Test
public void givenCustomerServiceInstance_whenCalledFindAll_thenCorrect() {
assertThat(customerService.findAll()).isInstanceOf(List.class);
}
@Test
public void givenCustomerServiceInstance_whenCalledFindByIdWithNullCustomer_thenCorrect() {
Map<Integer, Customer> customers = new HashMap<>();
customers.put(1, null);
customerService = new CustomerService(new SimpleCustomerDao(customers));
Customer customer = customerService.findById(1).orElseGet(() -> new Customer("Non-existing customer"));
assertThat(customer.getName()).isEqualTo("Non-existing customer");
}
}

View File

@ -19,6 +19,7 @@
<module>design-patterns</module>
<module>design-patterns-2</module>
<module>solid</module>
<module>dip</module>
</modules>
<dependencyManagement>