> For the complete documentation index, see [llms.txt](https://y2kot.gitbook.io/untitled/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://y2kot.gitbook.io/untitled/patterns/creationals-patterns/factory-method/realizacii-na-s++.md).

# Реализации на С++

## Общая реализация на языке С++

{% tabs %}
{% tab title="Car" %}
{% code fullWidth="true" %}

```cpp
class Car
{
public:
    virtual ~Car() = default;
    virtual void drive() = 0;
};


class Sedan : public Car
{
public:
    Sedan() 
    { 
        cout << "Sedan constructor called" << endl; 
    }
    
    ~Sedan() override 
    { 
        cout << "Sedan destructor called" << endl; 
    }

    void drive() override 
    { 
        cout << "Driving sedan" << endl; 
    }
};

class SUV : public Car 
{
public:
    SUV() 
    {
        cout << "Calling the SUV constructor;" << endl;
    }
    
    ~SUV() override 
    { 
        cout << "Calling the SUV destructor;" << endl; 
    }

    void drive() override 
    { 
        cout << "Driving SUV;" << endl; 
    }
};
```

{% endcode %}
{% endtab %}

{% tab title="Concepts" %}
{% code fullWidth="true" %}

```cpp
template <typename Derived, typename Base>
concept Derivative = is_abstract_v<Base> && is_base_of_v<Base, Derived>;

template <typename Type>
concept NotAbstract = !is_abstract_v<Type>;
```

{% endcode %}
{% endtab %}

{% tab title="CarCreator" %}
{% code fullWidth="true" %}

```cpp
class CarCreator
{
public:
    virtual ~CarCreator() = default;
    virtual unique_ptr<Car> createCar() const = 0;
};


template <Derivative<Car> TCar>
requires NotAbstract<TCar>
class ConcreteCarCreator : public CarCreator
{
public:
    unique_ptr<Car> createCar() const override 
    {
        return make_unique<TCar>();
    }
};
```

{% endcode %}
{% endtab %}

{% tab title="CarCreatorMaker" %}
{% code fullWidth="true" %}

```cpp
class CarCreatorMaker
{
public:
    template <Derivative<Car> TCar>
    NotAbstract<TCar>
    static unique_ptr<CarCreator> createCarCreator() 
    {
        return make_unique<ConcreteCarCreator<TCar>>();
    }
};
```

{% endcode %}
{% endtab %}

{% tab title="User" %}
{% code fullWidth="true" %}

```cpp
class User
{
public:
    void use(const shared_ptr<CarCreator>& creator)
    {
        if (!creator) throw runtime_error("The creator is missing!");

        shared_ptr<Car> car = creator->createCar();
        car->drive();
    }
};
```

{% endcode %}
{% endtab %}
{% endtabs %}

{% hint style="info" %}
Класс Solution выполняет роль посредника между клиентским кодом и классами создателей продуктов. Он отвечает за регистрацию методов создания объектов для каждого типа продукта и предоставляет методы для создания объектов по их идентификаторам.
{% endhint %}

{% tabs %}
{% tab title="VehicleSolution" %}
{% code fullWidth="true" %}

```cpp
class VehicleSolution
{
public:
    using CreateCarMaker = unique_ptr<CarCreator>(&)();
    using CallBackMap = map<size_t, CreateCarCreator>;

public:
    VehicleSolution() = default;
    VehicleSolution(initializer_list<pair<size_t, CreateCarCreator>> list);

    bool registrate(size_t id, CreateCarCreator createfun);
    bool check(size_t id) 
    { 
        return callbacks.erase(id) == 1; 
    }

    unique_ptr<CarCreator> create(size_t id);

private:
    CallBackMap callbacks;
};
```

{% endcode %}
{% endtab %}

{% tab title="Methods" %}
{% code fullWidth="true" %}

```cpp
# pragma region Solution
VehicleSolution::VehicleSolution(initializer_list<pair<size_t, CreateCarCreator>> list)
{
    for (auto&& elem : list)
        this->registrate(elem.first, elem.second);
}

bool VehicleSolution::registrate(size_t id, CreateCarCreator createfun)
{
    return callbacks.insert(CallBackMap::value_type(id, createfun)).second;
}

unique_ptr<CarCreator> VehicleSolution::create(size_t id)
{
    CallBackMap::const_iterator it = callbacks.find(id);

    return it != callbacks.end() ? unique_ptr<CarCreator>(it->second()) : nullptr;
}

shared_ptr<VehicleSolution> make_solution(
    initializer_list<pair<size_t, 
    VehicleSolution::CreateCarCreator>> list
)
{
    return shared_ptr<VehicleSolution>(new VehicleSolution(list));
}
# pragma endregion
```

{% endcode %}
{% endtab %}
{% endtabs %}

{% code lineNumbers="true" fullWidth="true" %}

```cpp
# include <iostream>
# include <initializer_list>
# include <memory>
# include <map>
# include <exception>

using namespace std;

int main()
{
    try
    {
        shared_ptr<VehicleSolution> solution
        = make_solution({ {1, CarCreatorMaker::createCarCreator<Sedan>} });

        if (!solution->registrate(2, CarCreatorMaker::createCarCreator<SUV>))
        {
            throw runtime_error("Error registration!");
        }
        shared_ptr<CarCreator> cr(solution->create(2));

        User{}.use(cr);
    }
    catch (runtime_error& err)
    {
        cout << err.what() << endl;
    }
}
```

{% endcode %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://y2kot.gitbook.io/untitled/patterns/creationals-patterns/factory-method/realizacii-na-s++.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
