Skip to content

Implement DAO Factory pattern #3282

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 10 commits into
base: master
Choose a base branch
from

Conversation

letdtcode
Copy link

Pull Request Template

What does this PR do?

This pull request implements the DAO Factory pattern, which demonstrates how the application should be switch between three different types of data sources (H2, Mongo, Json flat file). It includes code documented implementation, comprehensive tests, detailed documentation.

Key features

  • Customer: A model class presenting object that needs persistence in data source
  • DAOFactory: Abstract class provides a way to get the appropriate DAO based on the data source.
  • H2DataSourceFactory, MongoDataSourceFactory, FlatFileDataSourceFactory: Concrete factory which implement DAOFactory and return specific DAO Customer.
  • CustomerDAO: Define CRUD operation with Customer model.
  • FlatFileCustomerDAO, H2CustomerDAO, MongoCustomerDAO: Concrete DAO Customer which implement CRUD operation with data source.

Related Issue

Closes #1270

Copy link

github-actions bot commented May 17, 2025

PR Summary

This PR implements the DAO Factory pattern, providing flexible data access by switching between H2, Mongo, and JSON flat file data sources. It includes a Customer model, DAOFactory abstract class, concrete factories (H2DataSourceFactory, MongoDataSourceFactory, FlatFileDataSourceFactory), CustomerDAO interface, and concrete DAO implementations. Comprehensive tests and documentation are also included.

Changes

File Summary
dao-factory/README.md This file provides a comprehensive guide to the DAO Factory pattern, including its intent, real-world examples, a class diagram, a programmatic example in Java, usage scenarios, benefits, trade-offs, and related design patterns. It also includes references and credits.
dao-factory/etc/dao-factory.png New file: Class diagram for the DAO Factory pattern.
dao-factory/etc/dao-factory.puml New file: PlantUML class diagram for the DAO Factory pattern.
dao-factory/pom.xml New file: Maven project configuration file for the DAO Factory pattern implementation.
dao-factory/src/main/java/com/iluwatar/daofactory/App.java This Java class demonstrates the usage of the DAO Factory pattern by performing CRUD operations on a Customer object using three different data sources: H2, MongoDB, and a JSON flat file.
dao-factory/src/main/java/com/iluwatar/daofactory/CustomException.java New file: Custom exception class for handling errors in the DAO Factory pattern implementation.
dao-factory/src/main/java/com/iluwatar/daofactory/Customer.java New file: Represents a customer object with a generic ID and name, designed for persistence in various data sources.
dao-factory/src/main/java/com/iluwatar/daofactory/CustomerDAO.java New file: Defines the interface for Data Access Objects (DAOs) that handle CRUD operations for Customer objects.
dao-factory/src/main/java/com/iluwatar/daofactory/DAOFactory.java New file: Abstract class defining the interface for creating concrete DAO factories for different data sources.
dao-factory/src/main/java/com/iluwatar/daofactory/DAOFactoryProvider.java New file: Provides concrete DAOFactory implementations based on the specified data source type.
dao-factory/src/main/java/com/iluwatar/daofactory/DataSourceType.java New file: Enumerates the supported data source types (H2, Mongo, FlatFile).
dao-factory/src/main/java/com/iluwatar/daofactory/FlatFileCustomerDAO.java New file: Implements CustomerDAO using a JSON flat file for data persistence.
dao-factory/src/main/java/com/iluwatar/daofactory/FlatFileDataSourceFactory.java New file: Creates a FlatFileCustomerDAO instance.
dao-factory/src/main/java/com/iluwatar/daofactory/H2CustomerDAO.java New file: Implements CustomerDAO using an in-memory H2 database.
dao-factory/src/main/java/com/iluwatar/daofactory/H2DataSourceFactory.java New file: Creates an H2CustomerDAO instance.
dao-factory/src/main/java/com/iluwatar/daofactory/MongoCustomerDAO.java New file: Implements CustomerDAO using MongoDB for data persistence.
dao-factory/src/main/java/com/iluwatar/daofactory/MongoDataSourceFactory.java New file: Creates a MongoCustomerDAO instance.
dao-factory/src/main/resources/logback.xml New file: Logback configuration file for logging.
dao-factory/src/test/java/com/iluwatar/daofactory/AppTest.java New file: Unit tests for the App class, verifying CRUD operations.
dao-factory/src/test/java/com/iluwatar/daofactory/DAOFactoryTest.java New file: Unit tests for the DAOFactory, verifying creation of different DAO implementations.
dao-factory/src/test/java/com/iluwatar/daofactory/FlatFileCustomerDAOTest.java New file: Unit tests for FlatFileCustomerDAO, covering various scenarios including file operations and CRUD operations.
dao-factory/src/test/java/com/iluwatar/daofactory/H2CustomerDAOTest.java New file: Unit tests for H2CustomerDAO, covering various scenarios including database operations and CRUD operations.
dao-factory/src/test/java/com/iluwatar/daofactory/MongoCustomerDAOTest.java New file: Unit tests for MongoCustomerDAO, covering various scenarios including database operations and CRUD operations.
pom.xml The dao-factory module was added to the project.

autogenerated by presubmit.ai

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

Review Summary

Commits Considered (8)
  • f9263cc: fix SonarQube
  • 7c83fec: add license
  • 0a24bfd: fix: unit test pipeline
  • abd7ef8: fix: h2 inmemory database to pass CI
  • 525ad3f: doc: add document for dao-factory
  • 2960794: test: Add unit test dao-factory
  • fd0f855: feat: done implement dao factory
  • 50fbd10: feat: implement dao factory
Files Processed (24)
  • dao-factory/README.md (1 hunk)
  • dao-factory/etc/dao-factory.png (0 hunks)
  • dao-factory/etc/dao-factory.puml (1 hunk)
  • dao-factory/pom.xml (1 hunk)
  • dao-factory/src/main/java/com/iluwatar/daofactory/App.java (1 hunk)
  • dao-factory/src/main/java/com/iluwatar/daofactory/CustomException.java (1 hunk)
  • dao-factory/src/main/java/com/iluwatar/daofactory/Customer.java (1 hunk)
  • dao-factory/src/main/java/com/iluwatar/daofactory/CustomerDAO.java (1 hunk)
  • dao-factory/src/main/java/com/iluwatar/daofactory/DAOFactory.java (1 hunk)
  • dao-factory/src/main/java/com/iluwatar/daofactory/DAOFactoryProvider.java (1 hunk)
  • dao-factory/src/main/java/com/iluwatar/daofactory/DataSourceType.java (1 hunk)
  • dao-factory/src/main/java/com/iluwatar/daofactory/FlatFileCustomerDAO.java (1 hunk)
  • dao-factory/src/main/java/com/iluwatar/daofactory/FlatFileDataSourceFactory.java (1 hunk)
  • dao-factory/src/main/java/com/iluwatar/daofactory/H2CustomerDAO.java (1 hunk)
  • dao-factory/src/main/java/com/iluwatar/daofactory/H2DataSourceFactory.java (1 hunk)
  • dao-factory/src/main/java/com/iluwatar/daofactory/MongoCustomerDAO.java (1 hunk)
  • dao-factory/src/main/java/com/iluwatar/daofactory/MongoDataSourceFactory.java (1 hunk)
  • dao-factory/src/main/resources/logback.xml (1 hunk)
  • dao-factory/src/test/java/com/iluwatar/daofactory/AppTest.java (1 hunk)
  • dao-factory/src/test/java/com/iluwatar/daofactory/DAOFactoryTest.java (1 hunk)
  • dao-factory/src/test/java/com/iluwatar/daofactory/FlatFileCustomerDAOTest.java (1 hunk)
  • dao-factory/src/test/java/com/iluwatar/daofactory/H2CustomerDAOTest.java (1 hunk)
  • dao-factory/src/test/java/com/iluwatar/daofactory/MongoCustomerDAOTest.java (1 hunk)
  • pom.xml (2 hunks)
Actionable Comments (0)
Skipped Comments (0)

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

Review Summary

Commits Considered (1)
Files Processed (24)
  • dao-factory/README.md (1 hunk)
  • dao-factory/etc/dao-factory.png (0 hunks)
  • dao-factory/etc/dao-factory.puml (1 hunk)
  • dao-factory/pom.xml (1 hunk)
  • dao-factory/src/main/java/com/iluwatar/daofactory/App.java (1 hunk)
  • dao-factory/src/main/java/com/iluwatar/daofactory/CustomException.java (1 hunk)
  • dao-factory/src/main/java/com/iluwatar/daofactory/Customer.java (1 hunk)
  • dao-factory/src/main/java/com/iluwatar/daofactory/CustomerDAO.java (1 hunk)
  • dao-factory/src/main/java/com/iluwatar/daofactory/DAOFactory.java (1 hunk)
  • dao-factory/src/main/java/com/iluwatar/daofactory/DAOFactoryProvider.java (1 hunk)
  • dao-factory/src/main/java/com/iluwatar/daofactory/DataSourceType.java (1 hunk)
  • dao-factory/src/main/java/com/iluwatar/daofactory/FlatFileCustomerDAO.java (1 hunk)
  • dao-factory/src/main/java/com/iluwatar/daofactory/FlatFileDataSourceFactory.java (1 hunk)
  • dao-factory/src/main/java/com/iluwatar/daofactory/H2CustomerDAO.java (1 hunk)
  • dao-factory/src/main/java/com/iluwatar/daofactory/H2DataSourceFactory.java (1 hunk)
  • dao-factory/src/main/java/com/iluwatar/daofactory/MongoCustomerDAO.java (1 hunk)
  • dao-factory/src/main/java/com/iluwatar/daofactory/MongoDataSourceFactory.java (1 hunk)
  • dao-factory/src/main/resources/logback.xml (1 hunk)
  • dao-factory/src/test/java/com/iluwatar/daofactory/AppTest.java (1 hunk)
  • dao-factory/src/test/java/com/iluwatar/daofactory/DAOFactoryTest.java (1 hunk)
  • dao-factory/src/test/java/com/iluwatar/daofactory/FlatFileCustomerDAOTest.java (1 hunk)
  • dao-factory/src/test/java/com/iluwatar/daofactory/H2CustomerDAOTest.java (1 hunk)
  • dao-factory/src/test/java/com/iluwatar/daofactory/MongoCustomerDAOTest.java (1 hunk)
  • pom.xml (1 hunk)
Actionable Comments (0)
Skipped Comments (0)

return switch (dataSourceType) {
case H2 -> new H2DataSourceFactory();
case MONGO -> new MongoDataSourceFactory();
case FLAT_FILE -> new FlatFileDataSourceFactory();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As per the provided documentation -
default -> throw new IllegalArgumentException("Unsupported data source type");

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As per the provided documentation - default -> throw new IllegalArgumentException("Unsupported data source type");

Thanks, I fixed it

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

Review Summary

Commits Considered (1)
  • 2a66d97: fix: throw exception + refactor
Files Processed (2)
  • dao-factory/src/main/java/com/iluwatar/daofactory/DAOFactoryProvider.java (1 hunk)
  • pom.xml (1 hunk)
Actionable Comments (0)
Skipped Comments (0)

Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

DAO Factory pattern
2 participants