Created
April 5, 2026 07:52
-
-
Save XoLinA/d5de69daa92c25125d312058bd527d7b 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 Car | |
| { | |
| public: | |
| string name; | |
| string body; | |
| int engine; | |
| int wheels; | |
| int gears; | |
| string transmission; | |
| }; | |
| class CarBuilder abstract | |
| { | |
| public: | |
| virtual void Start() {} | |
| virtual void AddCar(Car car) {} | |
| virtual void Finish() {} | |
| virtual string Result() abstract; | |
| }; | |
| class TextCarBuilder : public CarBuilder | |
| { | |
| private: | |
| string text; | |
| public: | |
| void Start() override | |
| { | |
| text = "-------------------------------------------------------------\n"; | |
| text += "Name\t\tBody\t\tEngine\tWheels\tGears\tTransmission\n"; | |
| text += "-------------------------------------------------------------\n"; | |
| } | |
| void AddCar(Car car) override | |
| { | |
| text += car.name + "\t"; | |
| text += car.body + "\t\t"; | |
| text += to_string(car.engine) + "\t"; | |
| text += "R" + to_string(car.wheels) + "\t"; | |
| text += to_string(car.gears) + "\t"; | |
| text += car.transmission + "\n"; | |
| } | |
| void Finish() override | |
| { | |
| text += "-------------------------------------------------------------\n"; | |
| } | |
| string Result() override | |
| { | |
| return text; | |
| } | |
| }; | |
| class Shop | |
| { | |
| public: | |
| Car CreateCar(string name, string body, int engine, int wheels, int gears, string transmission) | |
| { | |
| Car car; | |
| car.name = name; | |
| car.body = body; | |
| car.engine = engine; | |
| car.wheels = wheels; | |
| car.gears = gears; | |
| car.transmission = transmission; | |
| return car; | |
| } | |
| string BuildTable(CarBuilder* builder) | |
| { | |
| builder->Start(); | |
| builder->AddCar(CreateCar("Daewoo Lanos", "Sedan", 98, 13, 5, "Manual")); | |
| builder->AddCar(CreateCar("Ford Probe", "Kupe", 160, 14, 4, "Auto")); | |
| builder->AddCar(CreateCar("UAZ Patriot", "Unive", 120, 16, 4, "Manual")); | |
| builder->AddCar(CreateCar("Hyundai Getz", "Hetc", 66, 13, 4, "Auto")); | |
| builder->Finish(); | |
| return builder->Result(); | |
| } | |
| }; | |
| void client(CarBuilder* builder) | |
| { | |
| Shop shop; | |
| string table = shop.BuildTable(builder); | |
| cout << table; | |
| } | |
| void main() | |
| { | |
| cout << "Cars table:\n\n"; | |
| CarBuilder* builder = new TextCarBuilder(); | |
| client(builder); | |
| delete builder; | |
| system("pause"); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment