Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Bridge pattern
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== C++ === <syntaxhighlight lang="cpp"> import std; class DrawingAPI { public: virtual ~DrawingAPI() = default; virtual std::string DrawCircle(float x, float y, float radius) const = 0; }; class DrawingAPI01 : public DrawingAPI { public: std::string DrawCircle(float x, float y, float radius) const override { return std::format("API01.circle at {}:{} - radius: {}", std::to_string(x), std::to_string(y), std::to_string(radius)); } }; class DrawingAPI02 : public DrawingAPI { public: std::string DrawCircle(float x, float y, float radius) const override { return std::format("API02.circle at {}:{} - radius: {}", std::to_string(x), std::to_string(y), std::to_string(radius)); } }; class Shape { public: Shape(const DrawingAPI& drawing_api): drawing_api_(drawing_api) {} virtual ~Shape() = default; virtual std::string Draw() const = 0; virtual float ResizeByPercentage(const float percent) = 0; protected: const DrawingAPI& drawing_api_; }; class CircleShape: public Shape { public: CircleShape(float x, float y, float radius, const DrawingAPI& drawing_api): Shape(drawing_api), x_(x), y_(y), radius_(radius) {} std::string Draw() const override { return drawing_api_.DrawCircle(x_, y_, radius_); } float ResizeByPercentage(const float percent) override { return radius_ *= (1.0f + percent/100.0f); } private: float x_, y_, radius_; }; int main(int argc, char** argv) { const DrawingAPI01 api1{}; const DrawingAPI02 api2{}; std::vector<CircleShape> shapes { CircleShape{1.0f, 2.0f, 3.0f, api1}, CircleShape{5.0f, 7.0f, 11.0f, api2} }; for (CircleShape& shape: shapes) { shape.ResizeByPercentage(2.5); std::println("{}", shape.Draw()); } return 0; } </syntaxhighlight> Output: <pre> API01.circle at 1.000000:2.000000 - radius: 3.075000 API02.circle at 5.000000:7.000000 - radius: 11.275000 </pre>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)