C + + implementation of "hidden implementation, open Interface" scheme _c language

Source: Internet
Author: User
Tags unique id

Why should I have an interface?

An interface is a window through which a program communicates with other programs. For example, there is a television, I do not need to know how it works, I just know to press the power button can turn on the TV, by the program Plus (+) minus (-) can switch TV channels on it.

Java programmers know that there are interface in Java that can be implemented externally, but C + + does not have the syntax of interfaces, so how is it better to implement external interface? We can define an abstract class through a pure virtual function, specifically to declare the functionality of a class.

We have completed the development of a program module, to use this program module for others, you will certainly not give him the source code (then others will fully support your technology), you will compile this program module into a library (Static library lib or dynamic library DLL) to be used by others. And how do you use it when someone gets your library? This requires a look at the interface provided by your program. C + + encapsulation is particularly good (personally feel much better than Java, Java into the jar package can easily be decompile, C + + to decompile more difficult), I just give you compiled the library and interface header file on it.

From an example to talk about the implementation of the scheme

Need

Let's look at a scene first. Suppose you have an electronic document, a document with multiple pages (page), a plurality of text units under each page (TextUnit, which represents the base unit of the elements within a document), and all text cell objects in a document have a unique ID. Its class diagram relations are as follows:

Figure 1: Diagram of the class

Design

According to the requirements, we can define three classes document, page, TextUnit, respectively, representing documents, pages, text units, each class we also need an external interface, so need three external interface class IDocument, IPage, Itextunit.
Based on these classes, we first create the. cpp file and the. h file, and organize the Engineering (EBOOK) directory structure as follows:

Figure 2: Engineering directory structure

Here document, Page, TextUnit is the specific implementation of the class, IDocument, IPage, Itextunit is the external interface provided, so that the implementation and interface separation.

Code implementation

IDocument.h: #pragma once class ipage; Class IDocument {public:virtual ~idocument (void) {} public://------------------------------------------------------ ---------//function://Generateid Cost document unique text object ID//access://Virtual public//parameter://returns://INT-Return ID// Remarks://...//author:luoweifu//---------------------------------------------------------------virtual int

Generateid () = 0; ---------------------------------------------------------------//function://AddPage Add a page//access://virtual Public//parameter://returns://ipage*-Return Page object//remarks://...//author:luoweifu//---------------------------------
------------------------------Virtual ipage* addpage () = 0;

};

IPage.h: #pragma once class itextunit; Class IPage {public:virtual ~ipage (void) {} public://-------------------------------------------------------------- -//function://Addtextunit Add a text unit//access://Virtual public//parameter://returns://itextunit*-Text Cell object//remArks://...//author:luoweifu//---------------------------------------------------------------virtual itextunit*
Addtextunit () = 0;

}; ITextUnit.h #pragma once class Itextunit {public: ~itextunit (void) {} public://--------------------------------------- ------------------------//function://GetId Get ID//access://Virtual public//parameter://returns://INT-Return ID//r Emarks://...//author:luoweifu//---------------------------------------------------------------virtual int GetId (

) = 0; ---------------------------------------------------------------//function://SetId Set ID//access://virtual Public//parameter://[in] int ID-ID//returns to set://Void-//remarks://...//author:luoweifu//----------------

-----------------------------------------------virtual void SetId (int id) = 0;

 };

Provide C interface

From the above code we can see that ipage can be created by idocument, Itextunit can be created by IPage. The question comes, who is idocument to create it? At this point we can provide two global functions Createdoc and Destroydoc to create and destroy IDocument object pointers, which are global functions (functions of type C) and we need to provide the export interface for C (which is important). The interface is defined as follows:

#pragma once #include "IDocument.h" #include "IPage.h" #include "ITextUnit.h"//====================================== =========================//To export a static library, the project to export the library and the project to use the library are precompiled macros Export_static//To export a dynamic library, the project to export the library is to precompile the macro export_static, Works with libraries without//=============================================================== #ifdef export//export library #define _API_ __  Declspec (dllexport) #else//import library #define _API_ __declspec (dllimport) #endif//export #ifdef export_static//export static libraries #define EBAPI int #else//Export Dynamic library #define EBAPI extern "C" _api_ int #endif//export_static//----------------------------------- ----------------------------//function://Createdoc Create Document Object//access://Public//parameter://[in] idocument * & Pdocument-//returns://Ebapi-//remarks://...//author:luowf[/luoweifu]//-----------------------------------


----------------------------Ebapi Createdoc (idocument*& pdocument); ---------------------------------------------------------------//function://Destroydoc destroy a DOcument object//access://Public//parameter://[in] idocument * pdocument-//returns://Ebapi-//remarks://...//auth Or:luowf[/luoweifu]//---------------------------------------------------------------Ebapi Destroydoc (idocument*

 pdocument);

Working with libraries

We can compile the ebook into a static library and then create a new project to use it. Ebook Project setup:

Create a new project Useebook use the ebook library. Useebook Project Preparation:
Generation properties\c++\preprocess\preprocess definitions:export_static

Generation properties\linker\general\addtional Library Directories:lib repository Path

Generation properties\linker\input\addtional Dependencies:EBook.lib

Test code:

#include "stdafx.h"

#include <iostream>

int _tmain (int argc, _tchar* argv[])
{
idocument* pDoc = NULL;
if (Createdoc (PDOC)!= 0)
{
return-1;
}

ipage* pPage = Pdoc->addpage ();
itextunit* ptextunit = Ppage->addtextunit ();
Std::cout << Ptextunit->getid () << Std::endl;

Destroydoc (PDOC);

return 0;
}

The above is the entire content of this article, I hope to help you learn.

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.