Skip to content

Implement DAO Factory pattern #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

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
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
360 changes: 360 additions & 0 deletions dao-factory/README.md

Large diffs are not rendered by default.

Binary file added dao-factory/etc/dao-factory.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
74 changes: 74 additions & 0 deletions dao-factory/etc/dao-factory.puml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
@startuml
package com.iluwatar.daofactory {
class App {
{static} void main(String[] args)
}

class Customer<T> {
T id
String name
}

interface CustomerDAO<ID> {
void save(Customer<ID> customer)
void update(Customer<ID> customer)
void delete(ID id)
List<Customer<ID>> findAll()
Optional<Customer<ID>> findById(ID id)
void deleteSchema()
}

abstract class DAOFactory {
{static} DAOFactory getDataSource(DataSourceType dataType)
{abstract} CustomerDAO createCustomerDAO()
}

enum DataSourceType {
H2
Mongo
FlatFile
}

class FlatFileCustomerDAO implements CustomerDAO<Long> {
void save(Customer<Long> customer)
void update(Customer<Long> customer)
void delete(Long id)
List<Customer<Long>> findAll()
Optional<Customer<Long>> findById(Long id)
void deleteSchema()
}

class H2CustomerDAO implements CustomerDAO<Long> {
void save(Customer<Long> customer)
void update(Customer<Long> customer)
void delete(Long id)
List<Customer<Long>> findAll()
Optional<Customer<Long>> findById(Long id)
void deleteSchema()
}

class FlatFileDataSourceFactory extends DAOFactory {
CustomerDAO createCustomerDAO()
}

class H2DataSourceFactory extends DAOFactory {
CustomerDAO createCustomerDAO()
}

class MongoCustomerDAO implements CustomerDAO<ObjectId> {
void save(Customer<ObjectId> customer)
void update(Customer<ObjectId> customer)
void delete(ObjectId id)
List<Customer<ObjectId>> findAll()
Optional<Customer<ObjectId>> findById(ObjectId id)
void deleteSchema()
}
class MongoDataSourceFactory extends DAOFactory {
CustomerDAO createCustomerDAO()
}

DataSourceType ..+ DAOFactory
DAOFactory ..+ App
App --> Customer
}
@enduml
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;
}
Loading
Loading