You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
41 lines
1.7 KiB
41 lines
1.7 KiB
# Simple Factory Pattern in Java
|
|
|
|
This project demonstrates the Simple Factory design pattern in Java. It includes an abstract product interface, concrete product implementations, and a factory class to create product instances based on specified types.
|
|
|
|
## Project Structure
|
|
|
|
```
|
|
simple-factory-java
|
|
├── src
|
|
│ └── main
|
|
│ └── java
|
|
│ ├── Product.java
|
|
│ ├── ConcreteProductA.java
|
|
│ ├── ConcreteProductB.java
|
|
│ └── Factory.java
|
|
├── pom.xml
|
|
└── README.md
|
|
```
|
|
|
|
## Files Description
|
|
|
|
- **Product.java**: Defines the `Product` interface with basic methods that all products must implement, such as `use()`.
|
|
|
|
- **ConcreteProductA.java**: Implements the `Product` interface, defining the behavior of Concrete Product A and overriding the `use()` method.
|
|
|
|
- **ConcreteProductB.java**: Implements the `Product` interface, defining the behavior of Concrete Product B and overriding the `use()` method.
|
|
|
|
- **Factory.java**: Defines the `Factory` class, which contains a static method `createProduct(String type)` that returns the appropriate product instance based on the provided type.
|
|
|
|
- **pom.xml**: Maven configuration file that defines the project's dependencies and build settings.
|
|
|
|
## Usage
|
|
|
|
To use this project, you can call the `Factory.createProduct(String type)` method with either "A" or "B" to get an instance of `ConcreteProductA` or `ConcreteProductB`, respectively. Then, you can invoke the `use()` method on the product instance to see its behavior.
|
|
|
|
## Build and Run
|
|
|
|
1. Ensure you have Maven installed on your machine.
|
|
2. Navigate to the project directory.
|
|
3. Run `mvn clean install` to build the project.
|
|
4. Use the generated classes in your Java application as needed. |