This project demonstrates the implementation of several design patterns in Java, including Singleton, Factory, Dependency Injection, and MVC-like structure.
The Singleton pattern ensures a class has only one instance and provides a global point of access to it.
The Factory pattern provides an interface for creating objects but lets subclasses decide which class to instantiate.
DI pattern implements inversion of control for resolving dependencies.
- Model: Product class representing data
- Service: ProductService handling business logic
- Controller: (if applicable) handling HTTP requests
- Java JDK 11 or higher
- Maven
mvn clean install
mvn exec:java
mvn test
// Create dependencies
ProductRepository repository = new ProductRepository();
ProductValidator validator = ProductValidatorFactory.createValidator("electronics");
// Initialize service with dependencies
ProductService productService = new ProductService(repository, validator);
// Create and save product
Product product = new Product("Laptop", "electronics", 999.99);
productService.saveProduct(product);
ConfigurationManager config = ConfigurationManager.getInstance();
String setting = config.getSetting("key");
src/
├── main/java/com/demo/
│ ├── config/
│ │ └── ConfigurationManager.java
│ ├── factory/
│ │ └── ProductValidator.java
│ │ └── ProductValidatorFactory.java
│ ├── model/
│ │ └── Product.java
│ ├── service/
│ │ └── ProductService.java
│ └── Main.java
└── test/java/com/demo/
└── ProductServiceTest.java
-
Singleton Pattern
- Ensures single instance of configuration
- Provides global access point
- Controls resource usage
-
Factory Pattern
- Encapsulates object creation
- Provides flexibility in creating different validators
- Makes code more maintainable
-
Dependency Injection
- Reduces coupling between classes
- Improves testability
- Makes code more modular
-
MVC-like Structure
- Separates concerns
- Improves maintainability
- Makes code more organized
Realized using Mermaid.