Initial Commit (#6614)

This commit is contained in:
Alejandro Gervasio 2019-03-30 13:01:53 -03:00 committed by KevinGilmore
parent b67a006020
commit 5754bb3310
10 changed files with 128 additions and 0 deletions

View File

@ -0,0 +1,30 @@
package com.baeldung.dip.daoimplementations;
import com.baeldung.dip.daos.CustomerDao;
import com.baeldung.dip.entities.Customer;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
public class SimpleCustomerDao implements CustomerDao {
private Map<Integer, Customer> customers = new HashMap<>();
public SimpleCustomerDao() {
}
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,6 @@
module com.baeldung.dip.daoimplementations {
requires com.baeldung.dip.entities;
requires com.baeldung.dip.daos;
provides com.baeldung.dip.daos.CustomerDao with com.baeldung.dip.daoimplementations.SimpleCustomerDao;
exports com.baeldung.dip.daoimplementations;
}

View File

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

View File

@ -0,0 +1,4 @@
module com.baeldung.dip.daos {
requires com.baeldung.dip.entities;
exports com.baeldung.dip.daos;
}

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,3 @@
module com.baeldung.dip.entities {
exports com.baeldung.dip.entities;
}

View File

@ -0,0 +1,17 @@
package com.baeldung.dip.mainapp;
import com.baeldung.dip.daoimplementations.MapCustomerDao;
import com.baeldung.dip.entities.Customer;
import com.baeldung.dip.services.CustomerService;
import java.util.HashMap;
public class MainApplication {
public static void main(String args[]) {
var customers = new HashMap<Integer, Customer>();
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,7 @@
module com.baeldung.dip.mainapp {
requires com.baeldung.dip.entities;
requires com.baeldung.dip.daos;
requires com.baeldung.dip.daoimplementations;
requires com.baeldung.dip.services;
exports com.baeldung.dip.mainapp;
}

View File

@ -0,0 +1,23 @@
package com.baeldung.dip.services;
import com.baeldung.dip.daos.CustomerDao;
import com.baeldung.dip.entities.Customer;
import java.util.Map;
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,6 @@
module com.baeldung.dip.services {
requires com.baeldung.dip.entities;
requires com.baeldung.dip.daos;
uses com.baeldung.dip.daos.CustomerDao;
exports com.baeldung.dip.services;
}