Adapter mode for C + + design mode (ii)

Source: Internet
Author: User
Tags connect socket

3. Socket network communication design and implementation------Class adapter

In addition to the object adapter mode, the adapter pattern also has a form, that is, the class adapter mode, The biggest difference between the class adapter pattern and the object adapter pattern is that the adapter and the adaptor have different relationships between the adapters and the adapter in the object adapter pattern, whereas the adapter and the adaptor in the class adapter pattern are inheritance relationships. Both the object adapter and the class adapter are used to reuse the functionality of the adapter class.

Socketpackage is a mature socket package, that is, the Adaptee adapter class, including the creation of sockets, bound sockets, listening sockets, connection sockets and other mature methods; The socket is the target abstract class, which defines the method that the customer wants; Socketadapter is an adapter class that inherits from the socket class and the Socketpackage class and converts the methods inside the mature package into the method that the customer expects in the socket class.

the mature socket package implementation code is as follows:

#ifndef _socket_package_h_#define _socket_package_h_#include <iostream> #include <string>using namespace std;//Socket Package (can be reused) class Socketpackage{public:void Createspecificsocket () {cout << "create sockets" << Endl;} void Bindspecificsocket () {cout << bind sockets << Endl;} void Listenspecificsocket () {cout << "listening sockets" << Endl;} void Connecspecifictsocket () {cout << "connect sockets" << Endl;}}; #endif
The Socketadapter socket adapter class and the Socketpackage socket package class are not in a combined relationship.Socketadapter inherits from the socket class and socketpackage, and when a method in the Socketadapter socket adapter class is called, the corresponding method of its base class socketpackage is called. The function reuse of the adapter class is realized by means of inheritance.
SetsThe socket adapter class implementation code is as follows:

#ifndef _socket_h_#define _socket_h_#include "SocketPackage.h"//Abstract Socket class socket{public://create socket virtual void Createsocket () = 0;//bound socket virtual void bindsocket () = 0;//listener socket virtual void listensocket () = 0;//connection socket virtual void Connect Socket () = 0;};/ /Socket Adapter (class adapter) class Socketadapter:public socket, socketpackage{public://create socket void Createsocket () { Createspecificsocket ();} Bind socket void Bindsocket () {Bindspecificsocket ();} Listener socket void Listensocket () {Listenspecificsocket ();} Connection socket void Connectsocket () {Connecspecifictsocket ();}}; #endif
The test program implementation code is as follows:

#include <iostream> #include "Socket.h" using namespace Std;int Main () {//Create socket Adapter Object Socket * Psocketadapter = new Socketadapter ();//socket operation with adapter Psocketadapter->createsocket ();p socketadapter->bindsocket (); Psocketadapter->listensocket ();p socketadapter->connectsocket ();//Destroy operation Delete Psocketadapter;psocketadapter = Null;return 0;}
Compiled and executed, the program results are as follows:


The adapter class inherits from the target abstract class and the adapter class, and when the method of the adaptor class is called, its base class is called within the method, which is the corresponding method in the adapter class, and the function of the reusable adapter class is implemented by inheritance.

Both the object adapter and the class adapter are intended to be reused for functionality.

Adapter mode for C + + design mode (ii)

Related Article

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.