Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use C++ Decoration Mode

Shulou Source: shulou.com Published: 2022-06-02 09:21:44 09月14日 Update

This article mainly introduces "how to use C++ decoration mode". In daily operation, I believe many people have doubts about how to use C++ decoration mode. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubts about how to use C++ decoration mode! Next, please follow the editor to study!

C++ design mode, decoration mode

I was deeply impressed by the decoration pattern in the structural pattern, and I also felt that it was basically the same in the design pattern.

It relies on the polymorphism of C++, and the decoration pattern is no exception, which allows you to do so without changing the code of the original class.

Code that does not directly inherit the original class is implemented through an abstract interface layer, and can even be combined at will

So record it here for use.

The following is the model diagram:

The following is a simple simulation code, the simulation originally a tool only has the write function, but we have to constantly expand it

The function gives it the ability to listen and read:

This is the result of running out.

-source tool

I can write!!

-can listen tool-

I can write!!

I can listene!!

-can read tool-

I can write!!

I can read!!

-can listen and read tool-

I can write!!

I can read!!

I can listene!!

Here is the code:

# include

Using namespace std

/ * Decoration mode

* decorator pattern (Decorator Pattern) dynamically adds some additional responsibilities to an object

, /

Class ABS_TOOL

{

Public:

Virtual ~ ABS_TOOL () {}

Virtual void fun () = 0; / / functional interface

}

Class write:public ABS_TOOL

{

Public:

Virtual void fun ()

{

Coutfun ()

Coutfun ()

Cout

Tags: Patterns caching code functions learning interfaces more design patterns help design polymorphism utility next medium dynamic impressions only basics objects tools Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft Docker Shulou Technology OPPO Reno Shulou Tech Info