Created
April 27, 2026 19:32
-
-
Save XoLinA/d961bc8d1544894dd07d63e52a4a96b1 to your computer and use it in GitHub Desktop.
навигатор
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| #include <string> | |
| using namespace std; | |
| class RouteStrategy { | |
| public: | |
| virtual void buildRoute(const string& from, const string& to) = 0; | |
| virtual ~RouteStrategy() {} | |
| }; | |
| class CarRoute : public RouteStrategy { | |
| public: | |
| void buildRoute(const string& from, const string& to) override | |
| { | |
| cout << "Building route for car from : " << from << " to " << to << endl; | |
| } | |
| }; | |
| class WalkingRoute : public RouteStrategy { | |
| public: | |
| void buildRoute(const string& from, const string& to) override | |
| { | |
| cout << "Building walking route from : "<< from << " to " << to << endl; | |
| } | |
| }; | |
| class PublicTransportRoute : public RouteStrategy { | |
| public: | |
| void buildRoute(const string& from, const string& to) override | |
| { | |
| cout << "Building public transport route from : " << from << " to " << to << endl; | |
| } | |
| }; | |
| class Navigator { | |
| private: | |
| RouteStrategy* strategy; | |
| public: | |
| Navigator(RouteStrategy* strategy) | |
| { | |
| this->strategy = strategy; | |
| } | |
| void setStrategy(RouteStrategy* newStrategy) | |
| { | |
| strategy = newStrategy; | |
| } | |
| void buildRoute(const string& from, const string& to) | |
| { | |
| if (strategy) | |
| strategy->buildRoute(from, to); | |
| else | |
| cout << "Strategy is not set!" << endl; | |
| } | |
| }; | |
| int main() { | |
| Navigator navigator(new CarRoute()); | |
| navigator.buildRoute("point A", "point B"); | |
| navigator.setStrategy(new WalkingRoute()); | |
| navigator.buildRoute("Home", "Shop"); | |
| navigator.setStrategy(new PublicTransportRoute()); | |
| navigator.buildRoute("Hospital", "Center"); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment