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 problems should be paid attention to in implementing adapter classes using C++

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

In this article, the editor introduces in detail "what to pay attention to when using C++ to realize the adapter class". The content is detailed, the steps are clear, and the details are handled properly. I hope this article "what problems you should pay attention to when using C++ to achieve adapter classes" can help you solve your doubts. Let's follow the editor's train of thought to learn new knowledge.

Realize

Although the implementation of the Adapter pattern is usually simple and straightforward, there are still some issues to pay attention to:

1) implement the adapter class using C++ when implementing the adapter class using C++, the Adapter class should inherit the Target class in a public way and inherit the Adaptee class in a private way. Therefore, the Adapter class should be a subtype of Target, but not of Adaptee.

This is an example of the beginning of the adapter pattern. The new system wants to use the functions of the existing system, so it adapts the existing system so that it can be used in the new system.

2) pluggable adapters there are many ways to implement pluggable adapters. For example, the TreeDisplay window component described earlier can automatically lay out and display a hierarchical structure, and there are three ways to implement it:

The third method is Smalltalk, so here are the first two methods.

The first (and this is what all three implementations do) is to find a "narrow" interface for Adaptee, the minimum set of operations that can be used for adaptation. Because narrow interfaces with fewer operations are easier to match than wide interfaces with more operations.

The narrow interface here refers to the interface that provides only one aspect of the function.

For TreeDisplay, the matched object can be any hierarchical structure. Therefore, the minimum interface set contains only two operations: one operation defines how to represent a node in the hierarchy, and the other operation returns the child nodes of that node.

This is the minimum requirement for expressing a data structure in TreeView, one for getting the subordinate node and one for generating the current node.

There are three ways to implement this narrow interface:

A) use abstract operations

Define the corresponding abstract operation of the narrow Adaptee interface in the TreeDisplay class. In this way, the subclasses implement these abstract operations and match the objects of the specific tree structure. For example, the DirectoryTreeDisplay subclass will do this by accessing the directory structure, as shown in the following figure.

This is a class adapter.

DirectoryTreeDisplay specializes this narrow interface so that its DirectoryBrowser customers can use it to display the directory structure.

TreeDisplay provides basic tree structure representation, but doesn't know where to get the subordinate nodes or how to build the current node based on it. These two functions are implemented by DirecotryTreeDisplay, which understands the file system functions.

B) use proxy objects

In this approach, the TreeDisplay forwards the request to access the tree structure to the proxy object.

The TreeDisplay customer makes some choices and provides those choices to the proxy object so that the customer can control the adaptation, as shown in the following figure.

This situation belongs to the object adapter.

For example, there is a DirectoryBrowser that uses TreeDisplay as before. DirectoryBrowser may construct a better proxy for matching TreeDisplay and hierarchical directory structures.

In a statically typed language such as C++, an explicit interface definition of a proxy is required. We specify such an interface by putting the narrow interface required by TreeDisplay into the pure virtual class TreeAccessorDelegate. We can then use the inheritance mechanism to integrate this interface into the agent of our choice-here we choose DirectoryBrowser. If DirectoryBrowser does not have a parent class, we will adopt single inheritance, otherwise we will adopt multiple inheritance. This method of merging classes together is easier than introducing a new TreeDisplay subclass and implementing its operations separately.

As opposed to the first approach, TreeDisplay manages an adapter object that defines the narrow interface that needs to be implemented. The consumer inherits the adapter class and adapts the interface of the data structure you want to represent to this interface.

After reading this, the article "what problems you should pay attention to when using C++ to implement adapter classes" has been introduced. If you want to master the knowledge of this article, you still need to practice and use it before you can understand it. If you want to know more about related articles, welcome to follow 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