C + + Adapter mode

Source: Internet
Author: User

You want to use a class that already exists, and its interface doesn't fit your needs.

Creating a class requires working with other classes to accomplish the task, requiring an adapter to transfer methods from other classes to the adapter

What is adapter mode: There is a target client class that wants to apply an interface to an already existing class, but the interface of the existing class does not conform to the interface naming of the client class, Then you can implement an adapter class to call the client class his method is actually calling the method of the adapter class to invoke the method that already exists for the class (the method in the adapter class calls the existing class method)

Creating a class requires working with other classes to accomplish the task, requiring an adapter to transfer methods from other classes to the adapter

There are two ways to implement Adapter mode: Object Adapter mode and Class Adapter mode

Object mode: The constructor in the object adapter class passes in an existing class class object, with a private variable being an object of the existing class, and a method that invokes the method of the existing class.

The method is called in the main function: Client class object = new Object adapter class (already exists class object)

Method () In the Customer object class, Customer class object

Understanding1. Target is an abstract base class for client invocation. 2. Adaptee is a class that needs to be adapted (the adapter class). The Adaptee method (Specificrequest) is incompatible with the target method (Request). 3. Adapter is the adapter class. Complete the interface adaptation work for Target and Adaptee. Adapter re-implements the request method, which internally hides the details of calling the Adaptee incompatible method (Specificrequest). 4. Class adapters are implemented by inheriting the class adapter class (Adaptee). The class adapter implements the method required by the client class (Specificrequest). When the client object invokes the adapter class (Adapter) method, the adapter internally invokes the method of the adaptive person it inherits from. 5. The object adapter contains a reference to the adapter, the same as the class adapter, and the object adapter implements the interface required by the client class. When the client object invokes the object adapter's method, the object adapter calls the appropriate method for the adapter instance it contains. Object combination, allowing a adapter to fit multiple adaptee.   Key points1. The adapter mode is suitable for:you want to use a class that already exists, and its interface doesn't fit your needs. you want to create a reusable class that can work with other unrelated classes or classes that are not predictable (that is, those that might not necessarily be compatible with the interface). (for object adapters only) you want to use some subclasses that already exist, but it is not possible to subclass each to match their interfaces. The object adapter can be adapted to its parent class interface. 2. When using class adapters with C + +, the adapter class inherits the target class using public and inherits the Adaptee class using private mode. This way, adapter is a target subtype, but not a adaptee subtype. This is the recommended way to use object adapters. 3. The difference between adapter mode and bridging (bridge) mode. The purpose of bridging mode is to separate the interface parts from the implementation so that they can be changed relatively independently; bridging is the design phase behavior. The adapter pattern is a change to an existing interface, which enables classes with the same functionality but incompatible interfaces to be bridged together.

Finally, explain why the class adapter pattern public inherits the target class, and the private inheritance needs the appropriate class?

Public inheritance: When the target class object calls its own interface, it guarantees that the calling adapter class has an interface with the same name, and only the methods in the adapter class will invoke the implementation method in the existing class, and if it is a private inheritance then it will definitely call the client class's own method, not the method of the adapter class.

Private inheritance: In order to ensure that the deprecated method must be the method of the appropriate class, if it is a public inheritance may call the appropriate method name in the same class and not call the method of the appropriate class to cause an error.

Both of these are involved in private inheritance and public inheritance features:

Private inheritance only inherits the interface of the base class and does not inherit the implementation, and the child class invokes the method in the parent class when it calls the inherited method

Public inheritance inherits the interfaces and implementations in the base class

In the two modes of adapter mode, one of the most important concepts is interface inheritance and implementation of inheritance differences and relations. interface inheritance and implementation inheritance are two important concepts of object-oriented domain, and interface inheritance refers to inheriting, the subclass obtains the interface of the parent class, and implementing inheritance refers to obtaining the implementation of the parent class by inheriting the subclass (not overall BOM publishing interface). Public inheritance in C + + is both interface inheritance and implementation inheritance, because subclasses can provide both an interface operation in the parent class and an interface implementation of the parent class after inheriting the parent class. Of course, we can simulate a separate interface to inherit and implement inheritance in a certain way and technology, for example, we can get the effect of inheriting by private inheritance (after private inheritance, the interface in the parent class becomes private, and of course it can only implement inheritance.) ), simulates the effect of interface inheritance through a purely abstract base class, but pure virtual function can also provide a default implementation in C + +, so this is an impure interface inheritance, but in Java we can interface to get true interface inheritance.

C + + Adapter mode

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.