Factory
- C++ course
- Advanced
- Lesson 32/83
- 5 min
Learn the object creation method with the Factory pattern.
What is the Factory pattern?
Factory hides object creation behind a method. The calling code does not know which concrete class is produced; it only receives a base class pointer. This makes adding new types easy and keeps the code loosely coupled.
In C++ a Factory method usually returns std::unique_ptr<Base>. Example: std::unique_ptr<Shape> create("circle") returns a Circle object. To add a new shape you only add an if branch here; the calling code stays unchanged.
Abstract base and concrete classes
For a Factory you first define an abstract base class with at least one virtual method. Then each concrete type derives from it and overrides the virtual methods. The Factory method returns the abstract base to the caller.
Example: class Shape { public: virtual std::string name() const = 0; }; is the abstract base. class Circle : public Shape and class Square : public Shape are concrete. create("circle") returns a Circle but the caller only sees Shape*. This way the code does not depend on concrete types.
Practice Write a ShapeFactory
Define an abstract Shape base with virtual std::string name() const = 0;. Derive Circle and Square from it and override name() (Circle → "Circle", Square → "Square"). A create(const std::string& type) method returns Circle for "circle" and Square for "square". In main call create("circle") and print its name().
Starter code
#include <iostream>
#include <memory>
#include <string>
// Define the Shape abstract base and Circle/Square classes
// Add the create method
int main() {
// Call create("circle") and print name()
return 0;
}Sample solution
#include <iostream>
#include <memory>
#include <string>
class Shape {
public:
virtual ~Shape() = default;
virtual std::string name() const = 0;
};
class Circle : public Shape {
public:
std::string name() const override { return "Circle"; }
};
class Square : public Shape {
public:
std::string name() const override { return "Square"; }
};
std::unique_ptr<Shape> create(const std::string& type) {
if (type == "circle") return std::make_unique<Circle>();
if (type == "square") return std::make_unique<Square>();
return nullptr;
}
int main() {
auto s = create("circle");
std::cout << s->name();
return 0;
}Expected output
Circle
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Complete the create method
std::unique_ptr<Shape> ____(const std::string& type) {
if (type == "circle") return std::make_unique<Circle>();
return nullptr;
}
std::unique_ptr<Shape> create(const std::string& type) {
if (type == "circle") return std::make_unique<Circle>();
return nullptr;
}
Explanation: The create method returns an object based on the type.
2 What does a Factory method return?
- An empty object
- A base class pointer Correct answer
- An integer code
- The concrete class itself
Explanation: A Factory returns the concrete class as a base pointer.
3 Match each piece to its role
| Term | Meaning |
|---|---|
class Shape { virtual ... = 0; } |
Abstract base |
class Circle : public Shape |
Concrete class |
std::unique_ptr<Shape> |
Factory return type |
create("circle") |
Factory call |