master
commit
fc9483df0d
@ -0,0 +1,41 @@
|
||||
# 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.
|
||||
@ -0,0 +1,32 @@
|
||||
<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>
|
||||
|
||||
<groupId>com.example</groupId>
|
||||
<artifactId>simple-factory-java</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<!-- Add any dependencies here if needed -->
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.8.1</version>
|
||||
<configuration>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
@ -0,0 +1,6 @@
|
||||
public class ConcreteProductA implements Product {
|
||||
@Override
|
||||
public void use() {
|
||||
System.out.println("Using Concrete Product A");
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,6 @@
|
||||
public class ConcreteProductB implements Product {
|
||||
@Override
|
||||
public void use() {
|
||||
System.out.println("Using Concrete Product B");
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,10 @@
|
||||
public class Factory {
|
||||
public static Product createProduct(String type) {
|
||||
if ("A".equalsIgnoreCase(type)) {
|
||||
return new ConcreteProductA();
|
||||
} else if ("B".equalsIgnoreCase(type)) {
|
||||
return new ConcreteProductB();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,3 @@
|
||||
public interface Product {
|
||||
void use();
|
||||
}
|
||||
Loading…
Reference in new issue