Skip to content

Commit b67a006

Browse files
alejandrogervasioKevinGilmore
authored andcommitted
BAEL-2789 - The Dependency Inversion Principle in Java (eugenp#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
1 parent 86bded1 commit b67a006

File tree

9 files changed

+236
-3
lines changed

9 files changed

+236
-3
lines changed

patterns/dip/pom.xml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<groupId>com.baeldung.dip</groupId>
7+
<artifactId>dip</artifactId>
8+
<name>dip</name>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<parent>
12+
<groupId>com.baeldung</groupId>
13+
<artifactId>patterns</artifactId>
14+
<version>1.0.0-SNAPSHOT</version>
15+
<relativePath>..</relativePath>
16+
</parent>
17+
18+
<dependencies>
19+
<dependency>
20+
<groupId>junit</groupId>
21+
<artifactId>junit</artifactId>
22+
<version>4.12</version>
23+
<scope>test</scope>
24+
</dependency>
25+
<dependency>
26+
<groupId>org.assertj</groupId>
27+
<artifactId>assertj-core</artifactId>
28+
<version>3.12.1</version>
29+
<scope>test</scope>
30+
</dependency>
31+
</dependencies>
32+
33+
<properties>
34+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
35+
<maven.compiler.source>11</maven.compiler.source>
36+
<maven.compiler.target>11</maven.compiler.target>
37+
</properties>
38+
39+
</project>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.baeldung.dip.application;
2+
3+
import com.baeldung.dip.daoimplementations.SimpleCustomerDao;
4+
import com.baeldung.dip.entities.Customer;
5+
import com.baeldung.dip.services.CustomerService;
6+
import java.util.Map;
7+
import java.util.HashMap;
8+
9+
public class Application {
10+
11+
public static void main(String[] args) {
12+
Map<Integer, Customer> customers = new HashMap<>();
13+
customers.put(1, new Customer("John"));
14+
customers.put(2, new Customer("Susan"));
15+
CustomerService customerService = new CustomerService(new SimpleCustomerDao(customers));
16+
customerService.findAll().forEach(System.out::println);
17+
}
18+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.baeldung.dip.daoimplementations;
2+
3+
import com.baeldung.dip.entities.Customer;
4+
import com.baeldung.dip.daointerfaces.CustomerDao;
5+
import java.util.ArrayList;
6+
import java.util.HashMap;
7+
import java.util.List;
8+
import java.util.Map;
9+
import java.util.Optional;
10+
11+
public class SimpleCustomerDao implements CustomerDao {
12+
13+
private Map<Integer, Customer> customers = new HashMap<>();
14+
15+
public SimpleCustomerDao(Map<Integer, Customer> customers) {
16+
this.customers = customers;
17+
}
18+
19+
@Override
20+
public Optional<Customer> findById(int id) {
21+
return Optional.ofNullable(customers.get(id));
22+
}
23+
24+
@Override
25+
public List<Customer> findAll() {
26+
return new ArrayList<>(customers.values());
27+
}
28+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.baeldung.dip.daointerfaces;
2+
3+
import com.baeldung.dip.entities.Customer;
4+
import java.util.List;
5+
import java.util.Optional;
6+
7+
public interface CustomerDao {
8+
9+
Optional<Customer> findById(int id);
10+
11+
List<Customer> findAll();
12+
13+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.baeldung.dip.entities;
2+
3+
public class Customer {
4+
5+
private final String name;
6+
7+
public Customer(String name) {
8+
this.name = name;
9+
}
10+
11+
public String getName() {
12+
return name;
13+
}
14+
15+
@Override
16+
public String toString() {
17+
return "Customer{" + "name=" + name + '}';
18+
}
19+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.baeldung.dip.services;
2+
3+
import com.baeldung.dip.daointerfaces.CustomerDao;
4+
import com.baeldung.dip.entities.Customer;
5+
import java.util.List;
6+
import java.util.Optional;
7+
8+
public class CustomerService {
9+
10+
private final CustomerDao customerDao;
11+
12+
public CustomerService(CustomerDao customerDao) {
13+
this.customerDao = customerDao;
14+
}
15+
16+
public Optional<Customer> findById(int id) {
17+
return customerDao.findById(id);
18+
}
19+
20+
public List<Customer> findAll() {
21+
return customerDao.findAll();
22+
}
23+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.baeldung.dip.tests;
2+
3+
import com.baeldung.dip.daoimplementations.SimpleCustomerDao;
4+
import com.baeldung.dip.daointerfaces.CustomerDao;
5+
import com.baeldung.dip.entities.Customer;
6+
import java.util.Map;
7+
import java.util.HashMap;
8+
import java.util.List;
9+
import java.util.Optional;
10+
import static org.assertj.core.api.Assertions.assertThat;
11+
import org.junit.Before;
12+
import org.junit.Test;
13+
14+
public class CustomerDaoUnitTest {
15+
16+
private CustomerDao customerDao;
17+
18+
@Before
19+
public void setUpCustomerDaoInstance() {
20+
Map<Integer, Customer> customers = new HashMap<>();
21+
customers.put(1, new Customer("John"));
22+
customers.put(2, new Customer("Susan"));
23+
customerDao = new SimpleCustomerDao(customers);
24+
}
25+
26+
@Test
27+
public void givenCustomerDaoInstance_whenCalledFindById_thenCorrect() {
28+
assertThat(customerDao.findById(1)).isInstanceOf(Optional.class);
29+
}
30+
31+
@Test
32+
public void givenCustomerDaoInstance_whenCalledFindAll_thenCorrect() {
33+
assertThat(customerDao.findAll()).isInstanceOf(List.class);
34+
}
35+
36+
@Test
37+
public void givenCustomerDaoInstance_whenCalledFindByIdWithNullCustomer_thenCorrect() {
38+
Map<Integer, Customer> customers = new HashMap<Integer, Customer>();
39+
customers.put(1, null);
40+
CustomerDao customerDaoObject = new SimpleCustomerDao(customers);
41+
42+
Customer customer = customerDaoObject.findById(1).orElseGet(() -> new Customer("Non-existing customer"));
43+
44+
assertThat(customer.getName()).isEqualTo("Non-existing customer");
45+
}
46+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.baeldung.dip.tests;
2+
3+
import com.baeldung.dip.daoimplementations.SimpleCustomerDao;
4+
import com.baeldung.dip.entities.Customer;
5+
import com.baeldung.dip.services.CustomerService;
6+
import java.util.Map;
7+
import java.util.HashMap;
8+
import java.util.List;
9+
import java.util.Optional;
10+
import static org.assertj.core.api.Assertions.assertThat;
11+
import org.junit.Before;
12+
import org.junit.Test;
13+
14+
public class CustomerServiceUnitTest {
15+
16+
private CustomerService customerService;
17+
18+
@Before
19+
public void setUpCustomerServiceInstance() {
20+
Map<Integer, Customer> customers = new HashMap<>();
21+
customers.put(1, new Customer("John"));
22+
customers.put(2, new Customer("Susan"));
23+
customerService = new CustomerService(new SimpleCustomerDao(customers));
24+
}
25+
26+
@Test
27+
public void givenCustomerServiceInstance_whenCalledFindById_thenCorrect() {
28+
assertThat(customerService.findById(1)).isInstanceOf(Optional.class);
29+
}
30+
31+
@Test
32+
public void givenCustomerServiceInstance_whenCalledFindAll_thenCorrect() {
33+
assertThat(customerService.findAll()).isInstanceOf(List.class);
34+
}
35+
36+
@Test
37+
public void givenCustomerServiceInstance_whenCalledFindByIdWithNullCustomer_thenCorrect() {
38+
Map<Integer, Customer> customers = new HashMap<>();
39+
customers.put(1, null);
40+
customerService = new CustomerService(new SimpleCustomerDao(customers));
41+
42+
Customer customer = customerService.findById(1).orElseGet(() -> new Customer("Non-existing customer"));
43+
44+
assertThat(customer.getName()).isEqualTo("Non-existing customer");
45+
}
46+
}

patterns/pom.xml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
44
<modelVersion>4.0.0</modelVersion>
55
<artifactId>patterns</artifactId>
66
<packaging>pom</packaging>
7-
<name>patterns</name>
7+
<name>patterns</name>
88

99
<parent>
1010
<groupId>com.baeldung</groupId>
@@ -19,6 +19,7 @@
1919
<module>design-patterns</module>
2020
<module>design-patterns-2</module>
2121
<module>solid</module>
22+
<module>dip</module>
2223
</modules>
2324

2425
<dependencyManagement>
@@ -55,4 +56,4 @@
5556
<jetty-maven-plugin.version>9.4.0.v20161208</jetty-maven-plugin.version>
5657
</properties>
5758

58-
</project>
59+
</project>

0 commit comments

Comments
 (0)