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 does C++ realize Abstract Factory

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

Share

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

Today, the editor will share with you the relevant knowledge points about how C++ can achieve an abstract factory. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article, let's take a look at it.

Code example

User Code:

This code creates a Maze object, two Room objects, and six Wall objects, and then establishes the relationships between the various objects.

Factory base class

Test code

The MazeGame,MazeFactory is tied together by the following code in the example.

Derivative factory

Next is the derived factory class, which in this case has two: EnchantedMazeFactory and BombedMazeFactory. The class diagram of the factory class is as follows:

It's exactly the same as test1 except that you use a different factory class. The output is as follows:

Notice the part of the SetSide call.

The first call accepts the Wall object built by factory.MakeWall (), and since this object is not passed to other variables, the memory management rights of the wall object should be given to R1; the second call passes the aDoor object, which is passed to R2 in addition to R1. Who should manage the memory of this aDoor?

In order to solve this problem, the memory management strategy needs to be reconsidered. For example, the memory of all objects is managed by aMaze objects, and other objects are referenced. Of course, this can solve the problem, but it is more tedious and not fun enough. The author uses another strategy here: use shared_ptr in Clover 11 to solve the problem.

From a formal point of view, there are the following differences between the author's implementation and the Design pattern sample code:

All pointers are replaced with custom types

Objects are created using make_shared.

The custom type here is the smart pointer.

All the changes are due to the use of shared_ptr.

The result of this operation is that memory management is greatly simplified. In the example provided by the author, since memory management is all left to shared_ptr, there is no need to explicitly free memory.

These are all the contents of the article "how to realize the Abstract Factory of C++". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

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

Internet Technology

Wechat

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

12
Report