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
Decorator 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++ === This implementation (which uses C++23 features) is based on the pre C++98 implementation in the book. <syntaxhighlight lang="c++"> import std; // Beverage interface. class Beverage { public: virtual void drink() = 0; virtual ~Beverage() = default; }; // Drinks which can be decorated. class Coffee: public Beverage { public: virtual void drink() override { std::print("Drinking Coffee"); } }; class Soda: public Beverage { public: virtual void drink() override { std::print("Drinking Soda"); } }; class BeverageDecorator: public Beverage { public: BeverageDecorator() = delete; BeverageDecorator(std::unique_ptr<Beverage> component_): component(std::move(component_)) {} virtual void drink() = 0; protected: void callComponentDrink() { if (component) component->drink(); } private: std::unique_ptr<Beverage> component; }; class Milk: public BeverageDecorator { public: Milk(std::unique_ptr<Beverage> component_, float percentage_): BeverageDecorator(std::move(component_)), percentage(percentage_) {} virtual void drink() override { callComponentDrink(); std::print(", with milk of richness {}%", percentage); } private: float percentage; }; class IceCubes: public BeverageDecorator { public: IceCubes(std::unique_ptr<Beverage> component_, int count_): BeverageDecorator(std::move(component_)), count(count_) {} virtual void drink() override { callComponentDrink(); std::print(", with {} ice cubes", count); } private: int count; }; class Sugar: public BeverageDecorator { public: Sugar(std::unique_ptr<Beverage> component_, int spoons_): BeverageDecorator(std::move(component_)), spoons(spoons_) {} virtual void drink() override { callComponentDrink(); std::print(", with {} spoons of sugar", spoons); } private: int spoons = 1; }; int main(int argc, char* argv[]) { std::unique_ptr<Beverage> soda = std::make_unique<Soda>(); soda = std::make_unique<IceCubes>(std::move(soda), 3); soda = std::make_unique<Sugar>(std::move(soda), 1); soda->drink(); std::println(); std::unique_ptr<Beverage> coffee = std::make_unique<Coffee>(); coffee = std::make_unique<IceCubes>(std::move(coffee), 16); coffee = std::make_unique<Milk>(std::move(coffee), 3.); coffee = std::make_unique<Sugar>(std::move(coffee), 2); coffee->drink(); return 0; } </syntaxhighlight> The program output is like <syntaxhighlight lang="c++"> Drinking Soda, with 3 ice cubes, with 1 spoons of sugar Drinking Coffee, with 16 ice cubes, with milk of richness 3%, with 2 spoons of sugar </syntaxhighlight> Full example can be tested on a [https://godbolt.org/z/s848nWozP godbolt page].
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
Decorator pattern
(section)
Add topic