Skip to content

Instantly share code, notes, and snippets.

@m1irka
Created May 5, 2026 07:22
Show Gist options
  • Select an option

  • Save m1irka/09f78090aaf07180ffde98db28a70181 to your computer and use it in GitHub Desktop.

Select an option

Save m1irka/09f78090aaf07180ffde98db28a70181 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <memory>
#include <string>
using namespace std;
class RouteStrategy {
public:
virtual ~RouteStrategy() = default;
virtual void buildRoute(const string& A, const string& B) = 0;
};
class RoadStrategy : public RouteStrategy {
public:
void buildRoute(const string& A, const string& B) override {
cout << "Building route by car from " << A << " to " << B << endl;
}
};
class WalkingStrategy : public RouteStrategy {
public:
void buildRoute(const string& A, const string& B) override {
cout << "Building walking route from " << A << " to " << B << endl;
}
};
class PublicTransportStrategy : public RouteStrategy {
public:
void buildRoute(const string& A, const string& B) override {
cout << "Building public transport route from " << A << " to " << B << endl;
}
};
class Navigator {
private:
unique_ptr<RouteStrategy> routeStrategy;
public:
void setStrategy(unique_ptr<RouteStrategy> strategy) {
routeStrategy = move(strategy);
}
void buildRoute(const string& A, const string& B) {
if (routeStrategy) {
routeStrategy->buildRoute(A, B);
}
else {
cout << "No route strategy selected!" << endl;
}
}
};
int main() {
Navigator navigator;
navigator.setStrategy(make_unique<RoadStrategy>());
navigator.buildRoute("Odessa", "Kyiv");
navigator.setStrategy(make_unique<WalkingStrategy>());
navigator.buildRoute("Park", "Museum");
navigator.setStrategy(make_unique<PublicTransportStrategy>());
navigator.buildRoute("Station", "University");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment