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 9 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. It includes dependencies for H2 database, MongoDB driver, Gson, and testing frameworks.
dao-factory/src/main/java/com/iluwatar/daofactory/App.java This Java class demonstrates the usage of the DAO Factory pattern. It performs CRUD operations on Customer objects using H2, MongoDB, and JSON flat file data sources.
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: A generic POJO class representing a customer, designed to work with various ID types for different persistence systems.
dao-factory/src/main/java/com/iluwatar/daofactory/CustomerDAO.java New file: Interface defining CRUD operations for the Customer model. Provides an abstraction layer for data access.
dao-factory/src/main/java/com/iluwatar/daofactory/DAOFactory.java New file: Abstract factory class for creating concrete DAO factories for different data sources.
dao-factory/src/main/java/com/iluwatar/daofactory/DAOFactoryProvider.java New file: Utility class providing concrete DAOFactory instances based on the data source type.
dao-factory/src/main/java/com/iluwatar/daofactory/DataSourceType.java New file: Enumeration defining supported data source types (H2, Mongo, FlatFile).
dao-factory/src/main/java/com/iluwatar/daofactory/FlatFileCustomerDAO.java New file: Implementation of CustomerDAO using JSON flat files for data persistence. Handles file I/O and JSON serialization.
dao-factory/src/main/java/com/iluwatar/daofactory/FlatFileDataSourceFactory.java New file: Concrete factory for creating FlatFileCustomerDAO instances.
dao-factory/src/main/java/com/iluwatar/daofactory/H2CustomerDAO.java New file: Implementation of CustomerDAO using an in-memory H2 database. Handles database interactions.
dao-factory/src/main/java/com/iluwatar/daofactory/H2DataSourceFactory.java New file: Concrete factory for creating H2CustomerDAO instances.
dao-factory/src/main/java/com/iluwatar/daofactory/MongoCustomerDAO.java New file: Implementation of CustomerDAO using MongoDB for data persistence. Handles MongoDB interactions.
dao-factory/src/main/java/com/iluwatar/daofactory/MongoDataSourceFactory.java New file: Concrete factory for creating MongoCustomerDAO instances.
dao-factory/src/main/resources/logback.xml New file: Logback configuration file for setting up 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 class, verifying the creation of different DAO implementations.
dao-factory/src/test/java/com/iluwatar/daofactory/FlatFileCustomerDAOTest.java New file: Unit tests for the FlatFileCustomerDAO class, covering various scenarios like saving, updating, deleting, and finding customers.
dao-factory/src/test/java/com/iluwatar/daofactory/H2CustomerDAOTest.java New file: Unit tests for the H2CustomerDAO class, covering various scenarios like saving, updating, deleting, and finding customers.
dao-factory/src/test/java/com/iluwatar/daofactory/MongoCustomerDAOTest.java New file: Unit tests for the MongoCustomerDAO class, covering various scenarios like saving, updating, deleting, and finding customers.
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)

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
1 participant