C ++ "hide implementation and open interface" implementation scheme and Interface Scheme
Why is there an interface?
An interface is a window for a program to communicate with other programs. For example, if there is a TV set, I don't need to know how it works. I only need to know how to press the power key to turn on the TV, and press the program plus (+) minus (-) you can switch the TV channel.
Java programmers know that interfaces in Java can be used to implement external interfaces, but C ++ does not have the syntax for interfaces. How can it implement external interfaces? We can use pure virtual functions to define an abstract class to declare the functions of a class.
We have completed the development of a program module. If we want to use this program module for others, you will certainly not give the source code to them (then others will fully support your technology ), you will compile this program module into a library (static library lib or dynamic library dll) for other users. How can someone else use your database? You need to check the interfaces provided by your program. The encapsulation of C ++ is particularly good (I personally think it is much better than Java. The jar package compiled by Java can be decompiled easily, and it is much more difficult to decompile C ++ ), I only need to compile the library and interface header files for you.
Explain the implementation solution needs from an instance
Let's first look at a scenario. Assume that there is an electronic Document and a Document has multiple pages, and each Page has multiple text units (TextUnit, indicating the basic unit of elements in the Document ), all text unit objects in a document have unique IDs. The class graph relationship is as follows:
Figure 1: class relationship diagram
Design
As needed, we can define three types of Document, Page, and TextUnit to indicate documents, pages, and text units. Each class also requires an external interface, therefore, three external interface classes are required: IDocument, IPage, and ITextUnit.
Based on these classes, we first create a. cpp file and. h file, and organize the project (EBook) directory structure as follows:
Figure 2: Project directory structure
Here, Document, Page, and TextUnit are specific implementation classes. IDocument, IPage, and ITextUnit are interfaces provided externally, thus implementing Implementation and interface separation.
Code Implementation
IDocument. h:
# Pragma onceclass IPage; class IDocument {public: virtual ~ IDocument (void) {} public: // variable // function: // GenerateId the unique Text object ID in the generated cost document // Access: // virtual public // Parameter: // Returns: // int-return ID // Remarks ://... // author: luoweifu // optional virtual int GenerateId () = 0; // response // function: // AddPage Add a page // Access: // virtual public // Parameter: // Returns: // IPage *-returned Page Object // Remarks ://... // author: luoweifu // --------------------------------------------------------------------- virtual IPage * AddPage () = 0 ;};
IPage. h:
# Pragma onceclass ITextUnit; class IPage {public: virtual ~ IPage (void) {} public: // container // function: // AddTextUnit Add a text unit // Access: // virtual public // Parameter: // Returns: // ITextUnit *-text unit object // Remarks ://... // author: luoweifu // --------------------------------------------------------------------- virtual ITextUnit * AddTextUnit () = 0 ;};
ITextUnit. h
# Pragma onceclass ITextUnit {public :~ ITextUnit (void) {} public: // region // function: // GetId get ID // Access: // virtual public // Parameter: // Returns: // int-return ID // Remarks ://... // author: luoweifu // optional virtual int GetId () = 0; // optional // function: // SetId Set ID // Access: // virtual public // Parameter: // [in] int id-ID to be set // Returns: // void-// Remarks ://... // author: luoweifu // --------------------------------------------------------------------- virtual void SetId (int id) = 0 ;};
Provides C Interfaces
From the code above, we can see that IPage can be created by IDocument, and ITextUnit can be created by IPage. The question is, who created the IDocument? At this time, we can provide two global functions, CreateDoc and DestroyDoc, to create and destroy IDocument object pointers. These two functions are global functions (C-type functions ), we need to provide it with the C export interface (which is important ). Its interface is defined as follows:
# Pragma once # include "IDocument. h "# include" IPage. h "# include" ITextUnit. h "// ============================================ ========================================/// when you want to export a static library, both the project for import and export and the project for use of the library must be precompiled with the macro EXPORT_STATIC // when the dynamic library is to be exported, the project for export and export must be precompiled with the macro EXPORT_STATIC, the project that uses the library does not need to use // ========================================== ============================## ifdef EXPORT // EXPORT the database # define _ API _ declspec (dllexport) # else // import to the database # define _ API _ declspec (dllimport) # endif // EXPORT # ifdef EXPORT_STATIC // EXPORT the static library # define EBAPI int # else // EXPORT the dynamic library # define EBAPI extern "C" _ API _ int # endif // EXPORT_STATIC // ----------------------------------------------------------------- // function: // CreateDoc create a Document Object // Access: // public // Parameter: // [in] IDocument * & pDocument-// Returns: // EBAPI-// Remarks: //... // author: luowf [/luoweifu] // --------------------------------------------------------------- EBAPI CreateDoc (IDocument * & pDocument); // destroy // function: // DestroyDoc destroys a Document Object // Access: // public // Parameter: // [in] IDocument * pDocument-// Returns: // EBAPI-// Remarks ://... // author: luowf [/luoweifu] // --------------------------------------------------------------- EBAPI DestroyDoc (IDocument * pDocument );
Usage Library
We can compile the EBook into a static library, and then create a new project to use it. EBook Project Settings:
Figure 3: project configuration
Create a new project UseEBook to use the EBook library. UseEBook project preparation:
Generation Properties \ C ++ \ Preprocess Definitions: EXPORT_STATIC
Generation Properties \ Linker \ General \ Addtional Library Directories: Path of the lib Library
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;}
Copyright Disclaimer: This article is an original article by the blogger. It cannot be used for any commercial purposes without the consent of the blogger. Please indicate the source for reprinting.