https://github.com/joegasewicz/factory
Abstract factory pattern library
https://github.com/joegasewicz/factory
Last synced: 6 months ago
JSON representation
Abstract factory pattern library
- Host: GitHub
- URL: https://github.com/joegasewicz/factory
- Owner: joegasewicz
- Created: 2024-01-01T19:11:53.000Z (almost 2 years ago)
- Default Branch: master
- Last Pushed: 2024-01-01T19:29:09.000Z (almost 2 years ago)
- Last Synced: 2025-02-10T14:53:19.595Z (8 months ago)
- Language: Java
- Size: 2.93 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Factory
Abstract factory pattern library.### Usage
See the doc strings for full instructions.```java
interface Shape {
String draw();
}class Rectangle implements Shape {
@Override
public String draw() {
return "Rectangle.draw()";
}
}class Square implements Shape {
@Override
public String draw() {
return "Square.draw()";
}
}
```Abstract Creator
```java
class RectangleFactory extends AbstractCreator {
protected Shape factoryMethod() {
return new Rectangle();
}
}class SquareFactory extends AbstractCreator {
protected Shape factoryMethod() {
return new Square();
}
}RectangleFactory rectangleFactory = new RectangleFactory();
Rectangle rectangle = (Rectangle) rectangleFactory.getInstance();
```Creator
```java
class ShapeFactory extends Creator {@Override
protected Shape getInstance(String instanceType) {
if (instanceType == null) {
return null;
} else if (instanceType.equalsIgnoreCase("RECTANGLE")) {
return new Rectangle();
} else if (instanceType.equalsIgnoreCase("SQUARE")) {
return new Square();
} else {
return null;
}
}
}ShapeFactory shapeFactory = new ShapeFactory();
Shape rectangle = shapeFactory.getInstance("rectangle");
```