Refactoring changes to DAO pattern. Renamed default dao implementation to InMemory and refined interface
This commit is contained in:
@@ -51,18 +51,18 @@ public class App {
|
||||
* @param args command line args.
|
||||
*/
|
||||
public static void main(final String[] args) {
|
||||
final CustomerDao customerDao = new CustomerDaoImpl(generateSampleCustomers());
|
||||
log.info("customerDao.getAllCustomers(): " + customerDao.getAllCustomers());
|
||||
log.info("customerDao.getCusterById(2): " + customerDao.getCustomerById(2));
|
||||
final CustomerDao customerDao = new InMemoryCustomerDao(generateSampleCustomers());
|
||||
log.info("customerDao.getAllCustomers(): " + customerDao.getAll());
|
||||
log.info("customerDao.getCusterById(2): " + customerDao.getById(2));
|
||||
final Customer customer = new Customer(4, "Dan", "Danson");
|
||||
customerDao.addCustomer(customer);
|
||||
log.info("customerDao.getAllCustomers(): " + customerDao.getAllCustomers());
|
||||
customerDao.add(customer);
|
||||
log.info("customerDao.getAllCustomers(): " + customerDao.getAll());
|
||||
customer.setFirstName("Daniel");
|
||||
customer.setLastName("Danielson");
|
||||
customerDao.updateCustomer(customer);
|
||||
log.info("customerDao.getAllCustomers(): " + customerDao.getAllCustomers());
|
||||
customerDao.deleteCustomer(customer);
|
||||
log.info("customerDao.getAllCustomers(): " + customerDao.getAllCustomers());
|
||||
customerDao.update(customer);
|
||||
log.info("customerDao.getAllCustomers(): " + customerDao.getAll());
|
||||
customerDao.delete(customer);
|
||||
log.info("customerDao.getAllCustomers(): " + customerDao.getAll());
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -22,7 +22,7 @@
|
||||
*/
|
||||
package com.iluwatar.dao;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -31,13 +31,13 @@ import java.util.List;
|
||||
*/
|
||||
public interface CustomerDao {
|
||||
|
||||
List<Customer> getAllCustomers();
|
||||
Stream<Customer> getAll();
|
||||
|
||||
Customer getCustomerById(int id);
|
||||
Customer getById(int id);
|
||||
|
||||
void addCustomer(Customer customer);
|
||||
boolean add(Customer customer);
|
||||
|
||||
void updateCustomer(Customer customer);
|
||||
boolean update(Customer customer);
|
||||
|
||||
void deleteCustomer(Customer customer);
|
||||
boolean delete(Customer customer);
|
||||
}
|
||||
|
||||
38
dao/src/main/java/com/iluwatar/dao/DBCustomerDao.java
Normal file
38
dao/src/main/java/com/iluwatar/dao/DBCustomerDao.java
Normal file
@@ -0,0 +1,38 @@
|
||||
package com.iluwatar.dao;
|
||||
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class DBCustomerDao implements CustomerDao {
|
||||
|
||||
@Override
|
||||
public Stream<Customer> getAll() {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Customer getById(int id) {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean add(Customer customer) {
|
||||
// TODO Auto-generated method stub
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean update(Customer customer) {
|
||||
// TODO Auto-generated method stub
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean delete(Customer customer) {
|
||||
// TODO Auto-generated method stub
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -22,7 +22,10 @@
|
||||
*/
|
||||
package com.iluwatar.dao;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -34,51 +37,44 @@ import java.util.List;
|
||||
* the DAO), from how these needs can be satisfied with a specific DBMS, database schema, etc.
|
||||
*
|
||||
*/
|
||||
public class CustomerDaoImpl implements CustomerDao {
|
||||
// TODO update the javadoc
|
||||
public class InMemoryCustomerDao implements CustomerDao {
|
||||
|
||||
// Represents the DB structure for our example so we don't have to managed it ourselves
|
||||
// Note: Normally this would be in the form of an actual database and not part of the Dao Impl.
|
||||
private List<Customer> customers;
|
||||
private Map<Integer, Customer> idToCustomer = new HashMap<>();
|
||||
|
||||
public CustomerDaoImpl(final List<Customer> customers) {
|
||||
this.customers = customers;
|
||||
public InMemoryCustomerDao(final List<Customer> customers) {
|
||||
customers.stream()
|
||||
.forEach((customer) -> idToCustomer.put(customer.getId(), customer));
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Customer> getAllCustomers() {
|
||||
return customers;
|
||||
public Stream<Customer> getAll() {
|
||||
return idToCustomer.values().stream();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Customer getCustomerById(final int id) {
|
||||
Customer customer = null;
|
||||
for (final Customer cus : getAllCustomers()) {
|
||||
if (cus.getId() == id) {
|
||||
customer = cus;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return customer;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addCustomer(final Customer customer) {
|
||||
if (getCustomerById(customer.getId()) == null) {
|
||||
customers.add(customer);
|
||||
public Customer getById(final int id) {
|
||||
return idToCustomer.get(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean add(final Customer customer) {
|
||||
if (getById(customer.getId()) != null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
idToCustomer.put(customer.getId(), customer);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void updateCustomer(final Customer customer) {
|
||||
if (getAllCustomers().contains(customer)) {
|
||||
final int index = getAllCustomers().indexOf(customer);
|
||||
getAllCustomers().set(index, customer);
|
||||
}
|
||||
public boolean update(final Customer customer) {
|
||||
return idToCustomer.replace(customer.getId(), customer) != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteCustomer(final Customer customer) {
|
||||
getAllCustomers().remove(customer);
|
||||
public boolean delete(final Customer customer) {
|
||||
return idToCustomer.remove(customer.getId()) != null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user