Factory Method Header

namespace factory_method_pattern

Namespace for the Factory Method pattern.

class ConcreteCreatorA : public factory_method_pattern::Creator
#include <factory_method.hpp>

Concrete implementation of the Creator.

Overrides the factory method to create ConcreteProductA.

Public Functions

virtual std::unique_ptr<Product> createProduct() const override

Factory method to create ConcreteProductA.

Factory method implementation in ConcreteCreatorA.

ConcreteCreatorA creates and returns an instance of ConcreteProductA. The returned product is encapsulated in a std::unique_ptr for memory safety.

Returns:

A unique pointer to a ConcreteProductA instance.

Returns:

A unique pointer to a newly created ConcreteProductA.

class ConcreteCreatorB : public factory_method_pattern::Creator
#include <factory_method.hpp>

Concrete implementation of the Creator.

Overrides the factory method to create ConcreteProductB.

Public Functions

virtual std::unique_ptr<Product> createProduct() const override

Factory method to create ConcreteProductB.

Factory method implementation in ConcreteCreatorB.

ConcreteCreatorB creates and returns an instance of ConcreteProductB. The returned product is encapsulated in a std::unique_ptr for memory safety.

Returns:

A unique pointer to a ConcreteProductB instance.

Returns:

A unique pointer to a newly created ConcreteProductB.

class ConcreteProductA : public factory_method_pattern::Product
#include <factory_method.hpp>

Concrete implementation of the Product interface.

Represents a specific type of product (ConcreteProductA).

Public Functions

virtual std::string use() const override

Implements the behavior of ConcreteProductA.

Implements the behavior for ConcreteProductA.

ConcreteProductA is a specific implementation of the Product interface. This method provides its unique behavior.

Returns:

A string describing the use of ConcreteProductA.

Returns:

A string describing the use of ConcreteProductA.

class ConcreteProductB : public factory_method_pattern::Product
#include <factory_method.hpp>

Concrete implementation of the Product interface.

Represents a specific type of product (ConcreteProductB).

Public Functions

virtual std::string use() const override

Implements the behavior of ConcreteProductB.

Implements the behavior for ConcreteProductB.

ConcreteProductB is a specific implementation of the Product interface. This method provides its unique behavior.

Returns:

A string describing the use of ConcreteProductB.

Returns:

A string describing the use of ConcreteProductB.

class Creator
#include <factory_method.hpp>

Abstract Creator (Factory) class.

Declares a factory method to create products. Subclasses will override the createProduct method to specify the type of product to create.

Subclassed by factory_method_pattern::ConcreteCreatorA, factory_method_pattern::ConcreteCreatorB

Public Functions

virtual ~Creator() = default
virtual std::unique_ptr<Product> createProduct() const = 0

Factory method to create a Product.

Returns:

A unique pointer to a newly created Product.

class Product
#include <factory_method.hpp>

Interface for the Product.

Defines the interface for all products created by the factory.

Subclassed by factory_method_pattern::ConcreteProductA, factory_method_pattern::ConcreteProductB

Public Functions

virtual ~Product() = default
virtual std::string use() const = 0

A method that each concrete product will implement to define its behavior.

Returns:

A string describing the product’s behavior.

Inner Classes

Inner Namespaces