< software architecture and Design Patterns > Houtie teacher on the deep analysis and model of adapter class in STL

Source: Internet
Author: User

Off-topic: Houtie Teacher rare a year to come to the nine class will be the Treasure Island, hereby commemorate the signing of the gift language and photo to encourage each other.

The land of the land is the shore

< body Start >

  Last night, 3 hours of classes to explore the adapter of a class, fortunately, I happened to be in the first year of contact with, for example, <functioinal> Library class bind1st,bind2nd These function objects use, after all, to use the < Algorithm> 's words, almost every function in it requires us to rewrite the function objects in the template, such as Comparato, but really didn't think that These libraries are clearly so simple to use, but in the teacher's explanation, the structure and the designers ' ideas are really complex and meticulous.

Specifically, adapter (adapter) This design must be preceded by the LSP (the Richter replacement principle), the content is: the subclass (derived typed) must be able to replace its base class (base type). This sentence is a bit confusing to me, since it is the subclass has inherited from the base class, it is not a subclass must be able to replace the parent class? otherwise polymorphic (polyporphism) also from the peace talks? If it's in Java, (I think) it might be uncontroversial, because Java has a powerful inheritance system. All classes inherit from the object class (the target object of a lambda expression is not a class, so it is not inherited from object), but in C + + There is another unique design--private inheritance in the language, this unique inheritance will be in the parent class whether it is protected or public inheritance or multiple inherited pointer reference to the private data of the child class, This means that the data has been declared terminated on this branch of the inheritance tree. Therefore, as in the case of MFC libraries such as the establishment of the LSP principle is to be adhered to, otherwise it will bring (serious) hidden danger to the program.

As shown in the courseware, there are generally three types of adapter designs (only two of them were spoken yesterday):

(1) Container Adapter

  The bottom container of this library we can see the selection is deque, it is worth noting that in this design, the queue and deque is a composite relationship, but contrary to my guess, the queue contains the Deque class, that is, the queue is built on the deque, As for why plainly deque and (double drive) list has the same "effect" or even the same method named but chose Deque as the default underlying container, the teacher humbly said he also dare not to make a conclusion, but we can at least boldly speculate that deque speed or efficiency is better than list. And in this case, the queue "has-a" deque such a relationship, the structure of more containers in this framework is basically starting with the underlying container, rewriting methods to add constraints to complete the architecture. As mentioned in the programming guidelines in more effective C + +, it is recommended that programmers write all data into private, but this does not mean that the class is missing part of the data in the inheritance chain, Because we will declare their getter&setter in public or protected (whether or not we think of Java:-P) to continue to inherit the operation of this data to achieve a secure architecture.

# #stack容器的实现和queue的实现是极其相似的, here's not much to repeat # #

(2) Function Adapter

  This part is more difficult to understand, the example of adapter is simply to implement a function: parameter binding. Some function objects may require only one parameter to be called as parameters, but we can only use a function object with a double parameter as a parameter, so the most efficient solution is to use adapter. Of course, don't be confident that you will use this Binder series not series function is to understand adapter, in-depth understanding of the architecture, to help us write the adapter we want, understand the STL specification can help us write adapter better with the upper container/ The upper object is compatible. For example, to write a three-parameter tied to a four-parameter binding for two of these special cases will have a good solution.

  The example is assuming that we want to use the COUNT_IF function, the function in the <algorithm> library is the If XX then count Count, where the first parameter and the second parameter are of course the iterator type, The third parameter, however, requires a single-parameter predicate function object or a function pointer. As shown, in fact, in this case predicate is just a name in the template, it can be t,fxxx various names, but a predicate name can let others see this function object finally need to return a Boolean value, If once the template is instantiated as one, overload the operator for example void operator () (paramlist.) Such a function, then in the count_if internal Judgment Branch language will encounter trouble, and because of the flexibility of C + + if the return value is overloaded to Int/long class, so that count_if will not error program execution as usual, will give seemingly correct program buried hidden trouble.

  So how does the adapter structure up? Here is a good overview of the picture below. As shown (actually there should be a comma between F and X anyway~) the implementation of adapter is to bind the parameters that need to be bound at the time of construction, and then overload the operator to make it a new function object. Yes, it seems very simple, but then look down, if considering that Functionadapter is inherited from the unary_function and binary_function template structure, will greatly increase the complexity of The whole process of building up adapter is also the process of "asking" a typedef type to the parent class, which is the most difficult part of adapter.

Finally, let's take a look at the concrete implementation of the macro process:

# #课后提问 # #

Me: Automatic type inference has been allowed in the new features of C++11, and why is the template type of bind2nd that is emphasized on the blackboard must be consistent with the parameter type? :::(blackboard) bind2nd<less<int>> (less<int> (), 12)

Houtie: Because the C++11 automatic type inference can only be used in the template function, but for the templated function object, we must indicate the parameters and the function object's template.

< software architecture and Design Patterns > Houtie Teacher's in-depth analysis and model discussion on adapter class in STL

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.