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

What is the bridge pattern in C++ design pattern?

2025-04-08 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces what is the bridge pattern in C++ design pattern. It has certain reference value. Interested friends can refer to it. I hope you can learn a lot after reading this article. Let the editor take you to know it.

Single responsibility model:

In the design of software components, if the division of responsibility is not clear, the result of using inheritance is that with the change of requirements, the subclass expands rapidly and is full of repetitive code.

Typical mode

Decorator

Bridge

Bridge motivation (Motivation)

Because of the inherent implementation logic of some types, they have two changing dimensions and even multiple latitudes.

How to deal with this "multi-dimensional change"? How to use object-oriented technology to make it easy for types to change in two or more directions without introducing additional complexity?

Schema definition

Separate the abstract part (business function) from the implementation part (platform implementation) so that they can change independently.

structure

Summary of main points

The Bridge pattern uses "composition relationships between objects" to decouple the inherent binding relationship between abstractions and implementations, so that abstractions and implementations can change along their respective dimensions. The so-called abstraction and implementation change along their respective latitudes, that is, "subclassing" them.

The Bridge pattern is sometimes similar to the multi-inheritance scheme, but the multi-inheritance scheme often violates the principle of single responsibility (that is, there is only one reason for a class to change) and has poor reusability. The Bridge pattern is a better solution than the multi-inheritance solution.

The application of Bridge pattern is generally in "two very strong change dimensions", and sometimes a class has more than two change dimensions, so you can use Bridge's extension pattern.

Cppclass Messager {public: virtual void Login (string username, string password) = 0; virtual void SendMessage (string message) = 0; virtual void SendPicture (Image image) = 0; virtual void PlaySound () = 0; virtual void DrawShape () = 0; virtual void WriteText () = 0; virtual void Connect () = 0; virtual ~ Messager () {}} / / platform implementation class PCMessagerBase: public Messager {public: virtual void PlaySound () {/ / *} virtual void DrawShape () {/ / *} virtual void WriteText () {/ / *} virtual void Connect () {/ / *}} Class MobileMessagerBase: public Messager {public: virtual void PlaySound () {/ / =} virtual void DrawShape () {/ / =} virtual void WriteText () {/ / =} virtual void Connect () {/ / Business abstraction class PCMessagerLite: public PCMessagerBase {public: virtual void Login (string username, string password) {PCMessagerBase::Connect () / /. } virtual void SendMessage (string message) {PCMessagerBase::WriteText (); / /. } virtual void SendPicture (Image image) {PCMessagerBase::DrawShape (); / /. }; class PCMessagerPerfect: public PCMessagerBase {public: virtual void Login (string username, string password) {PCMessagerBase::PlaySound (); / * PCMessagerBase::Connect (); / /. } virtual void SendMessage (string message) {PCMessagerBase::PlaySound (); / * PCMessagerBase::WriteText (); / /. } virtual void SendPicture (Image image) {PCMessagerBase::PlaySound (); / * PCMessagerBase::DrawShape (); / /. }; class MobileMessagerLite: public MobileMessagerBase {public: virtual void Login (string username, string password) {MobileMessagerBase::Connect (); / /. } virtual void SendMessage (string message) {MobileMessagerBase::WriteText (); / /. } virtual void SendPicture (Image image) {MobileMessagerBase::DrawShape (); / /. }; class MobileMessagerPerfect: public MobileMessagerBase {public: virtual void Login (string username, string password) {MobileMessagerBase::PlaySound (); / * MobileMessagerBase::Connect (); / /. } virtual void SendMessage (string message) {MobileMessagerBase::PlaySound (); / * MobileMessagerBase::WriteText (); / /. } virtual void SendPicture (Image image) {MobileMessagerBase::PlaySound (); / * MobileMessagerBase::DrawShape (); / /. }; void Process () {/ / compile-time assembly Messager* m = new MobileMessagerPerfect ();} class Messager {protected: MessagerImp* messagerImp;//...public: virtual void Login (string username, string password) = 0; virtual void SendMessage (string message) = 0; virtual void SendPicture (Image image) = 0; virtual ~ Messager () {}}; class MessagerImp {public: virtual void PlaySound () = 0; virtual void DrawShape () = 0 Virtual void WriteText () = 0; virtual void Connect () = 0; virtual MessagerImp () {}} / / platform implementation nclass PCMessagerImp: public MessagerImp {public: virtual void PlaySound () {/ / *} virtual void DrawShape () {/ / *} virtual void WriteText () {/ / *} virtual void Connect () {/ / *}} Class MobileMessagerImp: public MessagerImp {public: virtual void PlaySound () {/ / =} virtual void DrawShape () {/ / =} virtual void WriteText () {/ / =} virtual void Connect () {/ / virtual void Connect () {/ / number of classes: 1+n+mclass MessagerLite: public Messager {public: virtual void Login (string username, string password) {messagerImp- > Connect () / /. } virtual void SendMessage (string message) {messagerImp- > WriteText (); / / } virtual void SendPicture (Image image) {messagerImp- > DrawShape (); / / }; class MessagerPerfect: public Messager {public: virtual void Login (string username, string password) {messagerImp- > PlaySound (); / / * messagerImp- > Connect (); / /. } virtual void SendMessage (string message) {messagerImp- > PlaySound (); / * messagerImp- > WriteText (); / /. } virtual void SendPicture (Image image) {messagerImp- > PlaySound (); / * messagerImp- > DrawShape (); / /. }; void Process () {/ / Runtime Assembly MessagerImp* mImp = new PCMessagerImp (); Messager* m = new Messager (mImp);} Thank you for reading this article carefully. I hope the article "what is the Bridge pattern in C++ Design pattern shared by the editor is helpful to you?" at the same time, I also hope you can support us, pay attention to the industry information channel, and more related knowledge is waiting for you to learn!

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