Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

Program architecture: responsibilities and dependencies

Program architecture defines responsibilities, dependency direction, and object ownership. The example combines strategy pattern, factory registry, dependency injection, and decorator, while treating singleton as a separate decision about access and lifetime.

Version: C++23; repository compiler flag, production runtime not attested

Syntax

struct Interface { virtual ~Interface() = default; virtual int apply(int) const = 0; };

Program architecture describes component boundaries, responsibilities, dependencies, and lifetimes. It is not a C++ keyword. The patterns below are design conventions, not standard-library features. The sources specify language mechanisms, not a pattern catalog. The supplied working draft evolves and is not a frozen C++23 specification; a repository compiler flag does not attest the production runtime.

strategy pattern separates a replaceable algorithm from its client: Double implements Strategy, and Runner uses the interface. dependency injection means supplying a dependency externally; the Runner constructor receives a reference instead of constructing its own strategy. Virtual dispatch selects the implementation by dynamic type; override detects a mismatched declaration at compile time.

A factory registry maps keys to object-creation functions. Here, registry.at("double")() creates a strategy; an unknown key causes std::out_of_range. A decorator wraps an object while preserving its interface: AddOne delegates to the strategy, then adds one. The registry selects creation, the strategy selects an algorithm, and the decorator extends behavior.

std::unique_ptr expresses exclusive ownership; the virtual destructor enables correct deletion through a pointer to Strategy. AddOne requires a non-null pointer. The reference in Runner is non-owning: the referenced strategy must outlive the client using it. The example meets both conditions and produces 7 for 3.

A singleton restricts a class to one instance and provides a global access point. It is unnecessary here: object composition happens in main. A function-local static object can support such an implementation; concurrent initialization waits for completion, but later operations on the object do not automatically become thread-safe. Global access can hide dependencies and complicate test isolation.

Examples

#include <iostream>
#include <map>
#include <memory>
#include <string>
#include <utility>

struct Strategy {
    virtual ~Strategy() = default;
    virtual int apply(int value) const = 0;
};

struct Double final : Strategy {
    int apply(int value) const override { return value * 2; }
};

class AddOne final : public Strategy {
    std::unique_ptr<Strategy> inner_;
public:
    explicit AddOne(std::unique_ptr<Strategy> inner)
        : inner_(std::move(inner)) {}
    int apply(int value) const override {
        return inner_->apply(value) + 1;
    }
};

class Runner {
    const Strategy& strategy_;
public:
    explicit Runner(const Strategy& strategy) : strategy_(strategy) {}
    int run(int value) const { return strategy_.apply(value); }
};

std::unique_ptr<Strategy> make_double() {
    return std::make_unique<Double>();
}

int main() {
    using Factory = std::unique_ptr<Strategy> (*)();
    const std::map<std::string, Factory> registry{{"double", &make_double}};
    auto strategy = std::make_unique<AddOne>(registry.at("double")());
    const Runner runner{*strategy};
    std::cout << runner.run(3) << '\n';
}

Result

7

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources