Skip to content

Commit a290fe4

Browse files
committed
Add Dependency Inversion Principle (DIP) example (1/3)
* Add the first stage or iteration of our code. A `UserSearcher` use case instantiating the `HardcodedInMemoryUsersRepository` collaborator while initializing its class attributes. This is how we illustrate high coupling between classes.
1 parent 6d2886e commit a290fe4

4 files changed

Lines changed: 94 additions & 0 deletions

File tree

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package tv.codely.solid_principles.dependency_inversion_principle;
2+
3+
import java.util.Objects;
4+
5+
final public class User {
6+
private Integer id;
7+
private String name;
8+
9+
public User(Integer id, String name) {
10+
this.id = id;
11+
this.name = name;
12+
}
13+
14+
@Override
15+
public boolean equals(Object other) {
16+
if (this == other) return true;
17+
18+
if (!(other instanceof User)) return false;
19+
20+
User otherUser = (User) other;
21+
return Objects.equals(id, otherUser.id) && Objects.equals(name, otherUser.name);
22+
}
23+
24+
@Override
25+
public int hashCode() {
26+
return Objects.hash(id, name);
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package tv.codely.solid_principles.dependency_inversion_principle.stage_1_instantiating_from_clients;
2+
3+
import tv.codely.solid_principles.dependency_inversion_principle.User;
4+
5+
import java.util.Collections;
6+
import java.util.HashMap;
7+
import java.util.Map;
8+
import java.util.Optional;
9+
10+
final class HardcodedInMemoryUsersRepository {
11+
private Map<Integer, User> users = Collections.unmodifiableMap(new HashMap<Integer, User>() {
12+
{
13+
put(1, new User(1, "Rafa"));
14+
put(2, new User(2, "Javi"));
15+
}
16+
});
17+
18+
public Optional<User> search(Integer id) {
19+
return Optional.ofNullable(users.get(id));
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package tv.codely.solid_principles.dependency_inversion_principle.stage_1_instantiating_from_clients;
2+
3+
import tv.codely.solid_principles.dependency_inversion_principle.User;
4+
5+
import java.util.Optional;
6+
7+
final class UserSearcher {
8+
private HardcodedInMemoryUsersRepository usersRepository = new HardcodedInMemoryUsersRepository();
9+
10+
public Optional<User> search(Integer id) {
11+
return usersRepository.search(id);
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package tv.codely.solid_principles.dependency_inversion_principle.stage_1_instantiating_from_clients;
2+
3+
import org.junit.jupiter.api.Test;
4+
import tv.codely.solid_principles.dependency_inversion_principle.User;
5+
6+
import java.util.Optional;
7+
8+
import static org.junit.jupiter.api.Assertions.assertEquals;
9+
10+
final class UserSearcherShould {
11+
@Test
12+
void find_existing_users() {
13+
// We would be coupled to the actual HardcodedInMemoryUsersRepository implementation.
14+
// We don't have the option to set test users as we would have to do if we had a real database repository.
15+
Integer existingUserId = 1;
16+
UserSearcher userSearcher = new UserSearcher();
17+
18+
Optional<User> expectedUser = Optional.of(new User(1, "Rafa"));
19+
20+
assertEquals(expectedUser, userSearcher.search(existingUserId));
21+
}
22+
23+
@Test
24+
void not_find_non_existing_users() {
25+
Integer nonExistingUserId = 5;
26+
UserSearcher userSearcher = new UserSearcher();
27+
28+
Optional<User> expectedEmptyResult = Optional.empty();
29+
30+
assertEquals(expectedEmptyResult, userSearcher.search(nonExistingUserId));
31+
}
32+
}

0 commit comments

Comments
 (0)