Why should I have an interface?
An interface is a window in which a program communicates with other programs. For example, there is a TV, I do not need to know how it works, I just know to press the power button can turn on the TV, according to the program Plus (+) minus (-) can switch TV channels.
Java programmers know that Java has interface can implement external interface, but C + + does not have the interface of such a syntax, it is better how to implement the external interface? We can define an abstract class through a pure virtual function, specifically to declare the functionality of a class.
We have completed a program module development, to put this program module to others, you certainly will not give the source code to him (that others will fully support your technology), you will compile this program module into a library (Static library lib or dynamic library dll) to others. How do you use it when someone else gets your vault? This will need to look at the interface that your program provides. C + + encapsulation is particularly good (personally feel much better than Java, Java into the jar package can easily be anti-compilation, C + + to anti-compilation is more difficult), I just give you to compile the library and interface of the header file can be.
From an example, the realization of the solution needs
Let's look at a scene first. Suppose you have an electronic document, a document with multiple pages (page), and a number of text cells under each page (TextUnit, which represents the basic unit of elements within a document), and all the text cell objects in a document have a unique ID. Its class diagram relationships are as follows:
Figure 1: Diagram of a class
Design
According to the requirements, we can define three classes of document, page, TextUnit, respectively, to represent 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 Project (EBook) directory structure as follows:
Figure 2: Engineering directory structure
Here document, Page, TextUnit is the specific implementation class, IDocument, IPage, Itextunit is the interface provided externally, so that the implementation and interface separation.
Code implementation
IDocument.h:
#pragma onceclassIPage;classidocument{ Public:Virtual~idocument (void){} Public://--------------------------------------------------------------- //function: //Generateid The unique text object ID within the document //access: //Virtual public //parameter: //returns: //INT-Return ID //remarks: // ... //author:luoweifu //--------------------------------------------------------------- Virtual intGenerateid () =0;//--------------------------------------------------------------- //function: //AddPage Add a page //access: //Virtual public //parameter: //returns: //ipage*-Return Page object //remarks: // ... //author:luoweifu //--------------------------------------------------------------- Virtualipage* AddPage () =0;};
IPage.h:
#pragma onceclassItextunit;classipage{ Public:Virtual~ipage (void){} Public://--------------------------------------------------------------- //function: //Addtextunit Add a text unit //access: //Virtual public //parameter: //returns: //itextunit*-Text Cell object //remarks: // ... //author:luoweifu //--------------------------------------------------------------- Virtualitextunit* addtextunit () =0;};
ITextUnit.h
#pragma once class itextunit{ Public: ~itextunit (void){} Public://--------------------------------------------------------------- //function: //GetId Get ID //access: //Virtual public //parameter: //returns: //INT-Return ID //remarks: // ... //author:luoweifu //---------------------------------------------------------------VirtualintGetId () =0;//--------------------------------------------------------------- //function: //SetId set ID //access: //Virtual public //parameter: //[in] int ID-ID to set //returns: //Void- //remarks: // ... //author:luoweifu //---------------------------------------------------------------VirtualvoidSetId (intID) =0;};
C Interface Available
From the above code we can see that ipage can be created by idocument, Itextunit can be created by IPage. That's the question, who's 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 them with a C export interface (which is important). Its interface is defined as follows:
#pragma once#include "IDocument.h"#include "IPage.h"#include "ITextUnit.h"//===============================================================//To export a static library, both the project to export the library and the project using the library are precompiled macros Export_static//To export a dynamic library, the project to export the library to be precompiled macro export_static, the use of the library works//===============================================================#ifdef Export//exports library#define _API_ __declspec (dllexport)#else//import library#define _API_ __declspec (dllimport)#endif//export#ifdef export_static//export Static library#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 destroys a Document object//access:// public//parameter://[in] idocument * pdocument-//returns://Ebapi-//remarks:// ...//author: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 Settings:
Figure 3: Engineering Configuration
Create a new project Useebook using the ebook library. Useebook Engineering Preparation:
Generation properties\c++\preprocess\preprocess definitions:export_static
Generation properties\linker\general\addtional Library directories:lib Repository location 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; if0) { return -1; } IPage* pPage = pDoc->AddPage(); ITextUnit* pTextUnit = pPage->AddTextUnit(); std::coutstd::endl; DestroyDoc(pDoc); return0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permitted not for any commercial use, reproduced please indicate the source.
C + + "hidden implementation, open Interface" Implementation scheme