Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use C++ Decoration Mode

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report