Skip to content

Latest commit

 

History

History
58 lines (47 loc) · 1.13 KB

factory.md

File metadata and controls

58 lines (47 loc) · 1.13 KB

Singleton

Description

Creating a specific class without the need to call the exact class. A factory class will take care of creating and initializing the class.

Diagram

Code

Check code here

public interface Shape {
    void draw();
}

public class Rectangle implements Shape {
    @Override
    public void draw() {
        System.out.println("Drawing a rectangle");
    }
}

public class Circle implements Shape {
    @Override
    public void draw() {
        System.out.println("Drawing a circle");
    }
}

public class ShapeFactory {

    public Shape get(String type) {
        switch (type) {
            case "CIRCLE":
                return new Circle();
            case "RECTANGLE":
                return new Rectangle();
            default:
                throw new UnsupportedOperationException();
        }
    }

}

To access the instances

ShapeFactory factory = new ShapeFactory();
Shape circle = factory.get("CIRCLE");
circle.draw();
Shape rectangle = factory.get("RECTANGLE");
rectangle.draw();