Skip to content

Commit 0f7a828

Browse files
committed
doc: add document for dao-factory
1 parent 1e1c20e commit 0f7a828

19 files changed

+667
-365
lines changed

dao-factory/README.md

Lines changed: 334 additions & 1 deletion
Large diffs are not rendered by default.

dao-factory/etc/dao-factory.png

111 KB
Loading

dao-factory/etc/dao-factory.puml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@
22
package com.iluwatar.daofactory {
33
class App {
44
{static} void main(String[] args)
5-
{static} <T> void performCreateCustomer(CustomerDAO<T> customerDAO, \n List<Customer<T>> customerList)
6-
{static} <T> void performUpdateCustomer(CustomerDAO<T> customerDAO, \n Customer<T> customerUpdate)
7-
{static} <T> void performDeleteCustomer(CustomerDAO<T> customerDAO, \n T id)
8-
{static} void deleteSchema(CustomerDAO customerDAO)
95
}
106

117
class Customer<T> {

dao-factory/src/main/java/com/iluwatar/daofactory/App.java

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
@Slf4j
88
public class App {
99

10-
1110
public static void main(String[] args) {
1211
var daoFactory = DAOFactory.getDataSource(DataSourceType.H2);
1312
var customerDAO = daoFactory.createCustomerDAO();
@@ -23,8 +22,8 @@ public static void main(String[] args) {
2322
Customer<Long> customerUpdateInmemory = new Customer<>(1L, "Yellow");
2423

2524
LOGGER.debug("H2 - Create customer");
26-
performCreateCustomer(customerDAO,
27-
List.of(customerInmemory1, customerInmemory2, customerInmemory3));
25+
performCreateCustomer(
26+
customerDAO, List.of(customerInmemory1, customerInmemory2, customerInmemory3));
2827
LOGGER.debug("H2 - Update customer");
2928
performUpdateCustomer(customerDAO, customerUpdateInmemory);
3029
LOGGER.debug("H2 - Delete customer");
@@ -56,8 +55,8 @@ public static void main(String[] args) {
5655
Customer<Long> customerFlatFile3 = new Customer<>(3L, "Nhat");
5756
Customer<Long> customerUpdateFlatFile = new Customer<>(1L, "Thanh");
5857
LOGGER.debug("Flat file - Create customer");
59-
performCreateCustomer(customerDAO,
60-
List.of(customerFlatFile1, customerFlatFile2, customerFlatFile3));
58+
performCreateCustomer(
59+
customerDAO, List.of(customerFlatFile1, customerFlatFile2, customerFlatFile3));
6160
LOGGER.debug("Flat file - Update customer");
6261
performUpdateCustomer(customerDAO, customerUpdateFlatFile);
6362
LOGGER.debug("Flat file - Delete customer");
@@ -69,8 +68,8 @@ public static void deleteSchema(CustomerDAO<?> customerDAO) {
6968
customerDAO.deleteSchema();
7069
}
7170

72-
public static <T> void performCreateCustomer(CustomerDAO<T> customerDAO,
73-
List<Customer<T>> customerList) {
71+
public static <T> void performCreateCustomer(
72+
CustomerDAO<T> customerDAO, List<Customer<T>> customerList) {
7473
for (Customer<T> customer : customerList) {
7574
customerDAO.save(customer);
7675
}
@@ -80,21 +79,20 @@ public static <T> void performCreateCustomer(CustomerDAO<T> customerDAO,
8079
}
8180
}
8281

83-
public static <T> void performUpdateCustomer(CustomerDAO<T> customerDAO,
84-
Customer<T> customerUpdate) {
82+
public static <T> void performUpdateCustomer(
83+
CustomerDAO<T> customerDAO, Customer<T> customerUpdate) {
8584
customerDAO.update(customerUpdate);
8685
List<Customer<T>> customers = customerDAO.findAll();
8786
for (Customer<T> customer : customers) {
8887
LOGGER.debug(customer.toString());
8988
}
9089
}
9190

92-
public static <T> void performDeleteCustomer(CustomerDAO<T> customerDAO,
93-
T customerId) {
91+
public static <T> void performDeleteCustomer(CustomerDAO<T> customerDAO, T customerId) {
9492
customerDAO.delete(customerId);
9593
List<Customer<T>> customers = customerDAO.findAll();
9694
for (Customer<T> customer : customers) {
9795
LOGGER.debug(customer.toString());
9896
}
9997
}
100-
}
98+
}

dao-factory/src/main/java/com/iluwatar/daofactory/Customer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99

1010
/**
1111
* A customer generic POJO that represents the data that can be stored in any supported data source.
12-
* This class is designed t work with various ID types (e.g., Long, String, or ObjectId) through generic,
13-
* making it adaptable to different persistence system.
12+
* This class is designed t work with various ID types (e.g., Long, String, or ObjectId) through
13+
* generic, making it adaptable to different persistence system.
1414
*/
1515
@Getter
1616
@Setter

dao-factory/src/main/java/com/iluwatar/daofactory/CustomerDAO.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
import java.util.Optional;
55

66
/**
7-
* The Data Access Object (DAO) pattern provides an abstraction layer between the application
8-
* and the database. It encapsulates data access logic, allowing the application to work
9-
* with domain objects instead of direct database operations.
7+
* The Data Access Object (DAO) pattern provides an abstraction layer between the application and
8+
* the database. It encapsulates data access logic, allowing the application to work with domain
9+
* objects instead of direct database operations.
1010
*
11-
* <p>Implementations handle specific storage mechanisms (e.g., in-memory, databases)
12-
* while keeping client code unchanged.
11+
* <p>Implementations handle specific storage mechanisms (e.g., in-memory, databases) while keeping
12+
* client code unchanged.
1313
*
1414
* @see H2CustomerDAO
1515
* @see MongoCustomerDAO
@@ -53,8 +53,8 @@ public interface CustomerDAO<T> {
5353
Optional<Customer<T>> findById(T id);
5454

5555
/**
56-
* Delete the customer schema. After executing the statements,
57-
* this function will be called to clean up the data and delete the records.
56+
* Delete the customer schema. After executing the statements, this function will be called to
57+
* clean up the data and delete the records.
5858
*/
5959
void deleteSchema();
6060
}

dao-factory/src/main/java/com/iluwatar/daofactory/DAOFactory.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@
33
/**
44
* An abstract factory class that provides a way to create concrete DAO (Data Access Object)
55
* factories for different data sources types (e.g., H2, Mongo, FlatFile).
6-
* <p>
7-
* This class follows the Abstract Factory design pattern, allowing applications to retrieve
8-
* the approriate DAO implementation without being tightly coupled to a specific data source.
9-
* </p>
6+
*
7+
* <p>This class follows the Abstract Factory design pattern, allowing applications to retrieve the
8+
* approriate DAO implementation without being tightly coupled to a specific data source.
109
*
1110
* @see H2DataSourceFactory
1211
* @see MongoDataSourceFactory
@@ -16,8 +15,8 @@ public abstract class DAOFactory {
1615
/**
1716
* Returns a concrete {@link DAOFactory} intance based on the specified data source type.
1817
*
19-
* @param dataSourceType The type of data source for which a factory is needed.
20-
* Supported values: {@code H2}, {@code Mongo}, {@code FlatFile}
18+
* @param dataSourceType The type of data source for which a factory is needed. Supported values:
19+
* {@code H2}, {@code Mongo}, {@code FlatFile}
2120
* @return A {@link DAOFactory} implementation corresponding to the given data source type.
2221
* @throws IllegalArgumentException if the given data source type is not supported.
2322
*/

dao-factory/src/main/java/com/iluwatar/daofactory/DataSourceType.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package com.iluwatar.daofactory;
22

3-
/**
4-
* Enumerates the types of data sources supported by the application.
5-
*/
3+
/** Enumerates the types of data sources supported by the application. */
64
public enum DataSourceType {
75
H2,
86
Mongo,

dao-factory/src/main/java/com/iluwatar/daofactory/FlatFileCustomerDAO.java

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@
2424
public class FlatFileCustomerDAO implements CustomerDAO<Long> {
2525
private final Path filePath;
2626
private final Gson gson;
27-
Type customerListType = new TypeToken<List<Customer<Long>>>() {
28-
}.getType();
27+
Type customerListType = new TypeToken<List<Customer<Long>>>() {}.getType();
2928

3029
protected Reader createReader(Path filePath) throws IOException {
3130
return new FileReader(filePath.toFile());
@@ -35,9 +34,7 @@ protected Writer createWriter(Path filePath) throws IOException {
3534
return new FileWriter(filePath.toFile());
3635
}
3736

38-
/**
39-
* {@inheritDoc}
40-
*/
37+
/** {@inheritDoc} */
4138
@Override
4239
public void save(Customer<Long> customer) {
4340
List<Customer<Long>> customers = new LinkedList<>();
@@ -56,9 +53,7 @@ public void save(Customer<Long> customer) {
5653
}
5754
}
5855

59-
/**
60-
* {@inheritDoc}
61-
*/
56+
/** {@inheritDoc} */
6257
@Override
6358
public void update(Customer<Long> customer) {
6459
if (!filePath.toFile().exists()) {
@@ -71,25 +66,21 @@ public void update(Customer<Long> customer) {
7166
throw new RuntimeException("Failed to read customer data", ex);
7267
}
7368
customers.stream()
74-
.filter(c -> c.getId()
75-
.equals(customer.getId()))
69+
.filter(c -> c.getId().equals(customer.getId()))
7670
.findFirst()
7771
.ifPresentOrElse(
7872
c -> c.setName(customer.getName()),
7973
() -> {
8074
throw new RuntimeException("Customer not found with id: " + customer.getId());
81-
}
82-
);
75+
});
8376
try (Writer writer = createWriter(filePath)) {
8477
gson.toJson(customers, writer);
8578
} catch (IOException ex) {
8679
throw new RuntimeException("Failed to write customer data", ex);
8780
}
8881
}
8982

90-
/**
91-
* {@inheritDoc}
92-
*/
83+
/** {@inheritDoc} */
9384
@Override
9485
public void delete(Long id) {
9586
if (!filePath.toFile().exists()) {
@@ -101,9 +92,11 @@ public void delete(Long id) {
10192
} catch (IOException ex) {
10293
throw new RuntimeException("Failed to read customer data", ex);
10394
}
104-
Customer<Long> customerToRemove = customers.stream().filter(c -> c.getId().equals(id))
105-
.findFirst()
106-
.orElseThrow(() -> new RuntimeException("Customer not found with id: " + id));
95+
Customer<Long> customerToRemove =
96+
customers.stream()
97+
.filter(c -> c.getId().equals(id))
98+
.findFirst()
99+
.orElseThrow(() -> new RuntimeException("Customer not found with id: " + id));
107100
customers.remove(customerToRemove);
108101
try (Writer writer = createWriter(filePath)) {
109102
gson.toJson(customers, writer);
@@ -112,9 +105,7 @@ public void delete(Long id) {
112105
}
113106
}
114107

115-
/**
116-
* {@inheritDoc}
117-
*/
108+
/** {@inheritDoc} */
118109
@Override
119110
public List<Customer<Long>> findAll() {
120111
if (!filePath.toFile().exists()) {
@@ -129,9 +120,7 @@ public List<Customer<Long>> findAll() {
129120
return customers;
130121
}
131122

132-
/**
133-
* {@inheritDoc}
134-
*/
123+
/** {@inheritDoc} */
135124
@Override
136125
public Optional<Customer<Long>> findById(Long id) {
137126
if (!filePath.toFile().exists()) {
@@ -143,15 +132,10 @@ public Optional<Customer<Long>> findById(Long id) {
143132
} catch (IOException ex) {
144133
throw new RuntimeException("Failed to read customer data", ex);
145134
}
146-
return customers.stream()
147-
.filter(c -> c.getId()
148-
.equals(id))
149-
.findFirst();
135+
return customers.stream().filter(c -> c.getId().equals(id)).findFirst();
150136
}
151137

152-
/**
153-
* {@inheritDoc}
154-
*/
138+
/** {@inheritDoc} */
155139
@Override
156140
public void deleteSchema() {
157141
if (!filePath.toFile().exists()) {

dao-factory/src/main/java/com/iluwatar/daofactory/FlatFileDataSourceFactory.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,14 @@
55
import java.nio.file.Path;
66
import java.nio.file.Paths;
77

8-
/**
9-
* FlatFileDataSourceFactory concrete factory.
10-
*/
8+
/** FlatFileDataSourceFactory concrete factory. */
119
public class FlatFileDataSourceFactory extends DAOFactory {
1210
private final String FILE_PATH = System.getProperty("user.home") + "/Desktop/customer.json";
11+
1312
@Override
1413
public CustomerDAO createCustomerDAO() {
1514
Path filePath = Paths.get(FILE_PATH);
16-
Gson gson = new GsonBuilder()
17-
.setPrettyPrinting()
18-
.serializeNulls()
19-
.create();
15+
Gson gson = new GsonBuilder().setPrettyPrinting().serializeNulls().create();
2016
return new FlatFileCustomerDAO(filePath, gson);
2117
}
2218
}

0 commit comments

Comments
 (0)