Skip to content

Implement DAO Factory pattern [Closed] #3275

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions dao-factory/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--

This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).

The MIT License
Copyright © 2014-2022 Ilkka Seppälä

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.iluwatar</groupId>
<artifactId>java-design-patterns</artifactId>
<version>1.26.0-SNAPSHOT</version>
</parent>

<artifactId>dao-factory</artifactId>

<properties>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver-legacy</artifactId>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

</project>
124 changes: 124 additions & 0 deletions dao-factory/src/main/java/com/iluwatar/daofactory/App.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/*
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
*
* The MIT License
* Copyright © 2014-2022 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.daofactory;

import java.io.Serializable;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.bson.types.ObjectId;

@Slf4j
public class App {

public static void main(String[] args) {
var daoFactory = DAOFactoryProvider.getDataSource(DataSourceType.H2);
CustomerDAO customerDAO = daoFactory.createCustomerDAO();

// Perform CRUD H2 Database
if (customerDAO instanceof H2CustomerDAO h2CustomerDAO) {
h2CustomerDAO.deleteSchema();
h2CustomerDAO.createSchema();
}
Customer<Long> customerInmemory1 = new Customer<>(1L, "Green");
Customer<Long> customerInmemory2 = new Customer<>(2L, "Red");
Customer<Long> customerInmemory3 = new Customer<>(3L, "Blue");
Customer<Long> customerUpdateInmemory = new Customer<>(1L, "Yellow");

LOGGER.debug("H2 - Create customer");
performCreateCustomer(
customerDAO, List.of(customerInmemory1, customerInmemory2, customerInmemory3));
LOGGER.debug("H2 - Update customer");
performUpdateCustomer(customerDAO, customerUpdateInmemory);
LOGGER.debug("H2 - Delete customer");
performDeleteCustomer(customerDAO, 3L);
deleteSchema(customerDAO);

// Perform CRUD MongoDb
daoFactory = DAOFactoryProvider.getDataSource(DataSourceType.MONGO);
customerDAO = daoFactory.createCustomerDAO();
ObjectId idCustomerMongo1 = new ObjectId();
ObjectId idCustomerMongo2 = new ObjectId();
Customer<ObjectId> customer4 = new Customer<>(idCustomerMongo1, "Masca");
Customer<ObjectId> customer5 = new Customer<>(idCustomerMongo2, "Elliot");
Customer<ObjectId> customerUpdateMongo = new Customer<>(idCustomerMongo2, "Henry");

LOGGER.debug("Mongo - Create customer");
performCreateCustomer(customerDAO, List.of(customer4, customer5));
LOGGER.debug("Mongo - Update customer");
performUpdateCustomer(customerDAO, customerUpdateMongo);
LOGGER.debug("Mongo - Delete customer");
performDeleteCustomer(customerDAO, idCustomerMongo2);
deleteSchema(customerDAO);

// Perform CRUD Flat file
daoFactory = DAOFactoryProvider.getDataSource(DataSourceType.FLAT_FILE);
customerDAO = daoFactory.createCustomerDAO();
Customer<Long> customerFlatFile1 = new Customer<>(1L, "Duc");
Customer<Long> customerFlatFile2 = new Customer<>(2L, "Quang");
Customer<Long> customerFlatFile3 = new Customer<>(3L, "Nhat");
Customer<Long> customerUpdateFlatFile = new Customer<>(1L, "Thanh");
LOGGER.debug("Flat file - Create customer");
performCreateCustomer(
customerDAO, List.of(customerFlatFile1, customerFlatFile2, customerFlatFile3));
LOGGER.debug("Flat file - Update customer");
performUpdateCustomer(customerDAO, customerUpdateFlatFile);
LOGGER.debug("Flat file - Delete customer");
performDeleteCustomer(customerDAO, 3L);
deleteSchema(customerDAO);
}

public static void deleteSchema(CustomerDAO customerDAO) {
customerDAO.deleteSchema();
}

public static <T extends Serializable> void performCreateCustomer(
CustomerDAO<T> customerDAO, List<Customer<T>> customerList) {
for (Customer<T> customer : customerList) {
customerDAO.save(customer);
}
List<Customer<T>> customers = customerDAO.findAll();
for (Customer<T> customer : customers) {
LOGGER.debug(customer.toString());
}
}

public static <T extends Serializable> void performUpdateCustomer(
CustomerDAO<T> customerDAO, Customer<T> customerUpdate) {
customerDAO.update(customerUpdate);
List<Customer<T>> customers = customerDAO.findAll();
for (Customer<T> customer : customers) {
LOGGER.debug(customer.toString());
}
}

public static <T extends Serializable> void performDeleteCustomer(
CustomerDAO<T> customerDAO, T customerId) {
customerDAO.delete(customerId);
List<Customer<T>> customers = customerDAO.findAll();
for (Customer<T> customer : customers) {
LOGGER.debug(customer.toString());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
*
* The MIT License
* Copyright © 2014-2022 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.daofactory;

/** Customer exception */
public class CustomException extends RuntimeException {
public CustomException(String message) {
super(message);
}

public CustomException(String message, Throwable cause) {
super(message, cause);
}
}
47 changes: 47 additions & 0 deletions dao-factory/src/main/java/com/iluwatar/daofactory/Customer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
*
* The MIT License
* Copyright © 2014-2022 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.daofactory;

import java.io.Serializable;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;

/**
* A customer generic POJO that represents the data that can be stored in any supported data source.
* This class is designed t work with various ID types (e.g., Long, String, or ObjectId) through
* generic, making it adaptable to different persistence system.
*/
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class Customer<T extends Serializable> implements Serializable {
private T id;
private String name;
}
85 changes: 85 additions & 0 deletions dao-factory/src/main/java/com/iluwatar/daofactory/CustomerDAO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
*
* The MIT License
* Copyright © 2014-2022 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.daofactory;

import java.io.Serializable;
import java.util.List;
import java.util.Optional;

/**
* The Data Access Object (DAO) pattern provides an abstraction layer between the application and
* the database. It encapsulates data access logic, allowing the application to work with domain
* objects instead of direct database operations.
*
* <p>Implementations handle specific storage mechanisms (e.g., in-memory, databases) while keeping
* client code unchanged.
*
* @see H2CustomerDAO
* @see MongoCustomerDAO
* @see FlatFileCustomerDAO
*/
public interface CustomerDAO<T extends Serializable> {
/**
* Persist the given customer
*
* @param customer the customer to persist
*/
void save(Customer<T> customer);

/**
* Update the given customer
*
* @param customer the customer to update
*/
void update(Customer<T> customer);

/**
* Delete the customer with the given id
*
* @param id the id of the customer to delete
*/
void delete(T id);

/**
* Find all customers
*
* @return a list of customers
*/
List<Customer<T>> findAll();

/**
* Find the customer with the given id
*
* @param id the id of the customer to find
* @return the customer with the given id
*/
Optional<Customer<T>> findById(T id);

/**
* Delete the customer schema. After executing the statements, this function will be called to
* clean up the data and delete the records.
*/
void deleteSchema();
}
Loading
Loading