Implement the Factory Method Pattern

Medium Design Patterns & Final Challenges
Solve in Playground →

Problem Statement

Create a `Shape` base class with a virtual `draw()` method. Derive `Circle` and `Square`. Create a `ShapeFactory` class with a static method `Shape* createShape(std::string type)`. Read a shape name, get it from the factory, and call `draw()`. Remember to `delete` the pointer.

Input Format

A string representing the shape type ('Circle' or 'Square').

Output Format

Print 'Drawing Circle' or 'Drawing Square'.

Constraints

None

Sample Input

Circle

Sample Output

Drawing Circle

Explanation

The Factory Method pattern abstracts the instantiation logic, returning a derived object via a base class pointer based on an input string or condition.

Starter Code

#include <iostream>
#include <string>

class Shape {
public:
    virtual void draw() = 0;
    virtual ~Shape() {}
};

// Implement Circle, Square, and ShapeFactory

int main() {
    // Read input, create shape via factory, draw, and delete
    return 0;
}

Limits

  • Time Limit: 1s
  • Memory Limit: 256MB

Embedded C Programming

Updated: March 15, 2026
Intermediate

Embedded systems rely on efficient low-level programming to interact directly with hardware. In this course, you will learn how to write practical Embedded C programs used in real microcontroller-based systems. Rather than focusing only on theory, this course follows a practice-driven approach. Each lesson includes hands-on coding exercises that simulate real firmware development tasks used