Algo-Sim › Design Patterns

Factory: create without naming classes

Easy · Design Patterns · step through it in Java, Python and C++ on your own input

One place decides which concrete class to build; callers ask for a kind and use the result through its interface.

Problem

Callers ask for a shape by kind (square, triangle or hexagon) and a side length, and use it only through the shape interface, here to compute its perimeter. One factory decides which concrete class to build, so adding a new shape changes only the factory.

Constraints

Edge cases to test

Follow-up questions

Simple factory, factory method or abstract factory?
A simple factory is one function with a switch; a factory method lets subclasses choose; an abstract factory builds families of related objects.
How do you add kinds without editing the factory?
Register constructors in a map from kind to supplier, so a new kind is one registration.

Solution

Java

interface Shape {
    int perimeter();
}

class Square implements Shape {
    private final int side;
    Square(int side) { this.side = side; }
    public int perimeter() { return 4 * side; }
}

class Triangle implements Shape {
    private final int side;
    Triangle(int side) { this.side = side; }
    public int perimeter() { return 3 * side; }
}

class Hexagon implements Shape {
    private final int side;
    Hexagon(int side) { this.side = side; }
    public int perimeter() { return 6 * side; }
}

class ShapeFactory {
    // Callers name what they want; only the factory knows the classes.
    public static Shape create(String type, int side) {
        switch (type) {
            case "square": return new Square(side);
            case "triangle": return new Triangle(side);
            default: return new Hexagon(side);
        }
    }
}

Python

class Square:
    def __init__(self, side):
        self.side = side

    def perimeter(self):
        return 4 * self.side


class Triangle:
    def __init__(self, side):
        self.side = side

    def perimeter(self):
        return 3 * self.side


class Hexagon:
    def __init__(self, side):
        self.side = side

    def perimeter(self):
        return 6 * self.side


class ShapeFactory:
    # Callers name what they want; only the factory knows the classes.
    @staticmethod
    def create(type, side):
        if type == "square":
            return Square(side)
        if type == "triangle":
            return Triangle(side)
        return Hexagon(side)

C++

#include <bits/stdc++.h>
using namespace std;
struct Shape {
    virtual int perimeter() const = 0;
    virtual ~Shape() = default;
};
struct Square : Shape {
    int side;
    explicit Square(int s)
        : side(s) {}
    int perimeter() const override {
        return 4 * side;
    }
};
struct Triangle : Shape {
    int side;
    explicit Triangle(int s)
        : side(s) {}
    int perimeter() const override {
        return 3 * side;
    }
};
struct Hexagon : Shape {
    int side;
    explicit Hexagon(int s)
        : side(s) {}
    int perimeter() const override {
        return 6 * side;
    }
};
struct ShapeFactory {
    static unique_ptr<Shape> create(const string &type, int side) {
        if (type == "square")
            return make_unique<Square>(side);
        if (type == "triangle")
            return make_unique<Triangle>(side);
        return make_unique<Hexagon>(side);
    }
};

More Design Patterns problems