Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Special pages
Niidae Wiki
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Bridge pattern
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Page information
Appearance
move to sidebar
hide
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>
Summary:
Please note that all contributions to Niidae Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Encyclopedia:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Search
Search
Editing
Bridge pattern
(section)
Add topic