Skip to content

Commit f6a20c7

Browse files
committed
Refactoring changes to DAO pattern. Renamed default dao implementation to InMemory and refined interface
1 parent 528d179 commit f6a20c7

File tree

5 files changed

+113
-67
lines changed

5 files changed

+113
-67
lines changed

dao/src/main/java/com/iluwatar/dao/App.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,18 +51,18 @@ public class App {
5151
* @param args command line args.
5252
*/
5353
public static void main(final String[] args) {
54-
final CustomerDao customerDao = new CustomerDaoImpl(generateSampleCustomers());
55-
log.info("customerDao.getAllCustomers(): " + customerDao.getAllCustomers());
56-
log.info("customerDao.getCusterById(2): " + customerDao.getCustomerById(2));
54+
final CustomerDao customerDao = new InMemoryCustomerDao(generateSampleCustomers());
55+
log.info("customerDao.getAllCustomers(): " + customerDao.getAll());
56+
log.info("customerDao.getCusterById(2): " + customerDao.getById(2));
5757
final Customer customer = new Customer(4, "Dan", "Danson");
58-
customerDao.addCustomer(customer);
59-
log.info("customerDao.getAllCustomers(): " + customerDao.getAllCustomers());
58+
customerDao.add(customer);
59+
log.info("customerDao.getAllCustomers(): " + customerDao.getAll());
6060
customer.setFirstName("Daniel");
6161
customer.setLastName("Danielson");
62-
customerDao.updateCustomer(customer);
63-
log.info("customerDao.getAllCustomers(): " + customerDao.getAllCustomers());
64-
customerDao.deleteCustomer(customer);
65-
log.info("customerDao.getAllCustomers(): " + customerDao.getAllCustomers());
62+
customerDao.update(customer);
63+
log.info("customerDao.getAllCustomers(): " + customerDao.getAll());
64+
customerDao.delete(customer);
65+
log.info("customerDao.getAllCustomers(): " + customerDao.getAll());
6666
}
6767

6868
/**

dao/src/main/java/com/iluwatar/dao/CustomerDao.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
*/
2323
package com.iluwatar.dao;
2424

25-
import java.util.List;
25+
import java.util.stream.Stream;
2626

2727
/**
2828
*
@@ -31,13 +31,13 @@
3131
*/
3232
public interface CustomerDao {
3333

34-
List<Customer> getAllCustomers();
34+
Stream<Customer> getAll();
3535

36-
Customer getCustomerById(int id);
36+
Customer getById(int id);
3737

38-
void addCustomer(Customer customer);
38+
boolean add(Customer customer);
3939

40-
void updateCustomer(Customer customer);
40+
boolean update(Customer customer);
4141

42-
void deleteCustomer(Customer customer);
42+
boolean delete(Customer customer);
4343
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.iluwatar.dao;
2+
3+
import java.util.stream.Stream;
4+
5+
public class DBCustomerDao implements CustomerDao {
6+
7+
@Override
8+
public Stream<Customer> getAll() {
9+
// TODO Auto-generated method stub
10+
return null;
11+
}
12+
13+
@Override
14+
public Customer getById(int id) {
15+
// TODO Auto-generated method stub
16+
return null;
17+
}
18+
19+
@Override
20+
public boolean add(Customer customer) {
21+
// TODO Auto-generated method stub
22+
return false;
23+
}
24+
25+
@Override
26+
public boolean update(Customer customer) {
27+
// TODO Auto-generated method stub
28+
return false;
29+
}
30+
31+
@Override
32+
public boolean delete(Customer customer) {
33+
// TODO Auto-generated method stub
34+
return false;
35+
}
36+
37+
38+
}

dao/src/main/java/com/iluwatar/dao/CustomerDaoImpl.java renamed to dao/src/main/java/com/iluwatar/dao/InMemoryCustomerDao.java

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@
2222
*/
2323
package com.iluwatar.dao;
2424

25+
import java.util.HashMap;
2526
import java.util.List;
27+
import java.util.Map;
28+
import java.util.stream.Stream;
2629

2730
/**
2831
*
@@ -34,51 +37,44 @@
3437
* the DAO), from how these needs can be satisfied with a specific DBMS, database schema, etc.
3538
*
3639
*/
37-
public class CustomerDaoImpl implements CustomerDao {
40+
// TODO update the javadoc
41+
public class InMemoryCustomerDao implements CustomerDao {
3842

39-
// Represents the DB structure for our example so we don't have to managed it ourselves
40-
// Note: Normally this would be in the form of an actual database and not part of the Dao Impl.
41-
private List<Customer> customers;
43+
private Map<Integer, Customer> idToCustomer = new HashMap<>();
4244

43-
public CustomerDaoImpl(final List<Customer> customers) {
44-
this.customers = customers;
45+
public InMemoryCustomerDao(final List<Customer> customers) {
46+
customers.stream()
47+
.forEach((customer) -> idToCustomer.put(customer.getId(), customer));
4548
}
4649

4750
@Override
48-
public List<Customer> getAllCustomers() {
49-
return customers;
51+
public Stream<Customer> getAll() {
52+
return idToCustomer.values().stream();
5053
}
5154

5255
@Override
53-
public Customer getCustomerById(final int id) {
54-
Customer customer = null;
55-
for (final Customer cus : getAllCustomers()) {
56-
if (cus.getId() == id) {
57-
customer = cus;
58-
break;
59-
}
60-
}
61-
return customer;
56+
public Customer getById(final int id) {
57+
return idToCustomer.get(id);
6258
}
6359

6460
@Override
65-
public void addCustomer(final Customer customer) {
66-
if (getCustomerById(customer.getId()) == null) {
67-
customers.add(customer);
61+
public boolean add(final Customer customer) {
62+
if (getById(customer.getId()) != null) {
63+
return false;
6864
}
65+
66+
idToCustomer.put(customer.getId(), customer);
67+
return true;
6968
}
7069

7170

7271
@Override
73-
public void updateCustomer(final Customer customer) {
74-
if (getAllCustomers().contains(customer)) {
75-
final int index = getAllCustomers().indexOf(customer);
76-
getAllCustomers().set(index, customer);
77-
}
72+
public boolean update(final Customer customer) {
73+
return idToCustomer.replace(customer.getId(), customer) != null;
7874
}
7975

8076
@Override
81-
public void deleteCustomer(final Customer customer) {
82-
getAllCustomers().remove(customer);
77+
public boolean delete(final Customer customer) {
78+
return idToCustomer.remove(customer.getId()) != null;
8379
}
8480
}

dao/src/test/java/com/iluwatar/dao/CustomerDaoImplTest.java renamed to dao/src/test/java/com/iluwatar/dao/InMemoryCustomerDaoTest.java

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -22,50 +22,56 @@
2222
*/
2323
package com.iluwatar.dao;
2424

25-
import static org.junit.Assert.assertEquals;
26-
import static org.junit.Assert.assertNull;
27-
import static org.junit.Assert.assertTrue;
25+
import static org.junit.Assert.*;
2826

2927
import java.util.ArrayList;
3028
import java.util.List;
3129

30+
import org.junit.Assume;
3231
import org.junit.Before;
3332
import org.junit.Test;
3433

35-
public class CustomerDaoImplTest {
34+
public class InMemoryCustomerDaoTest {
3635

37-
private CustomerDaoImpl impl;
36+
private InMemoryCustomerDao dao;
3837
private List<Customer> customers;
3938
private static final Customer CUSTOMER = new Customer(1, "Freddy", "Krueger");
4039

4140
@Before
4241
public void setUp() {
4342
customers = new ArrayList<>();
4443
customers.add(CUSTOMER);
45-
impl = new CustomerDaoImpl(customers);
44+
dao = new InMemoryCustomerDao(customers);
4645
}
4746

4847
@Test
4948
public void deleteExistingCustomer() {
50-
assertEquals(1, impl.getAllCustomers().size());
51-
impl.deleteCustomer(CUSTOMER);
52-
assertTrue(impl.getAllCustomers().isEmpty());
49+
Assume.assumeTrue(dao.getAll().count() == 1);
50+
51+
boolean result = dao.delete(CUSTOMER);
52+
53+
assertTrue(result);
54+
assertTrue(dao.getAll().count() == 0);
5355
}
5456

5557
@Test
5658
public void deleteNonExistingCustomer() {
5759
final Customer nonExistingCustomer = new Customer(2, "Robert", "Englund");
58-
impl.deleteCustomer(nonExistingCustomer);
59-
assertEquals(1, impl.getAllCustomers().size());
60+
boolean result = dao.delete(nonExistingCustomer);
61+
62+
assertFalse(result);
63+
assertEquals(1, dao.getAll().count());
6064
}
6165

6266
@Test
6367
public void updateExistingCustomer() {
6468
final String newFirstname = "Bernard";
6569
final String newLastname = "Montgomery";
6670
final Customer customer = new Customer(CUSTOMER.getId(), newFirstname, newLastname);
67-
impl.updateCustomer(customer);
68-
final Customer cust = impl.getCustomerById(CUSTOMER.getId());
71+
boolean result = dao.update(customer);
72+
73+
assertTrue(result);
74+
final Customer cust = dao.getById(CUSTOMER.getId());
6975
assertEquals(newFirstname, cust.getFirstName());
7076
assertEquals(newLastname, cust.getLastName());
7177
}
@@ -76,38 +82,44 @@ public void updateNonExistingCustomer() {
7682
final String newFirstname = "Douglas";
7783
final String newLastname = "MacArthur";
7884
final Customer customer = new Customer(nonExistingId, newFirstname, newLastname);
79-
impl.updateCustomer(customer);
80-
assertNull(impl.getCustomerById(nonExistingId));
81-
final Customer existingCustomer = impl.getCustomerById(CUSTOMER.getId());
85+
boolean result = dao.update(customer);
86+
87+
assertFalse(result);
88+
assertNull(dao.getById(nonExistingId));
89+
final Customer existingCustomer = dao.getById(CUSTOMER.getId());
8290
assertEquals(CUSTOMER.getFirstName(), existingCustomer.getFirstName());
8391
assertEquals(CUSTOMER.getLastName(), existingCustomer.getLastName());
8492
}
8593

8694
@Test
8795
public void addCustomer() {
8896
final Customer newCustomer = new Customer(3, "George", "Patton");
89-
impl.addCustomer(newCustomer);
90-
assertEquals(2, impl.getAllCustomers().size());
97+
boolean result = dao.add(newCustomer);
98+
99+
assertTrue(result);
100+
assertEquals(2, dao.getAll().count());
91101
}
92102

93103
@Test
94104
public void addAlreadyAddedCustomer() {
95105
final Customer newCustomer = new Customer(3, "George", "Patton");
96-
impl.addCustomer(newCustomer);
97-
assertEquals(2, impl.getAllCustomers().size());
98-
impl.addCustomer(newCustomer);
99-
assertEquals(2, impl.getAllCustomers().size());
106+
dao.add(newCustomer);
107+
108+
boolean result = dao.add(newCustomer);
109+
assertFalse(result);
110+
assertEquals(2, dao.getAll().count());
100111
}
101112

102113
@Test
103114
public void getExistinCustomerById() {
104-
assertEquals(CUSTOMER, impl.getCustomerById(CUSTOMER.getId()));
115+
assertEquals(CUSTOMER, dao.getById(CUSTOMER.getId()));
105116
}
106117

107118
@Test
108-
public void getNonExistinCustomerById() {
119+
public void getNonExistingCustomerById() {
109120
final int nonExistingId = getNonExistingCustomerId();
110-
assertNull(impl.getCustomerById(nonExistingId));
121+
122+
assertNull(dao.getById(nonExistingId));
111123
}
112124

113125
/**

0 commit comments

Comments
 (0)