COM Samples (2) - Sample Structure Design

zhaozj2021-02-16  64

COM Sample Structure Design This article is the second article of the "COM Sample" series of articles, explaining the structure of the sample, explains the programming idea of ​​COM, and helps understand the sample code. Sample target wants to make a company to make an information management system, that is, information in the company can be entered into the computer and can be distributed inquiries, that is, the general manager can check the latest order situation and shipping. Due to the use of COM as the base frame of this information management system, it is easy to solve distributed problems, and due to the secure packaging, access control is also easy. Let's first describe the programming ideas provided by COM, and then design the idea to design each interface. The COM programming model has seen a lot of this statement: "COM is more face-to-face, and is more complete". Here is the idea of ​​this error, although it can be said, but it is a wrong application. This is like a cow knife can kill chickens, but it should not be said. Object-oriented programming is a thought, guiding how to design a program architecture. Its main thinking is to see the operation data as an object. The so-called object is a status and provides an interface to expose the logical concepts of services they can provide. It is a mixture of state and function through semantics. Its and daily lives are very similar, such as TV sets provide both services - search, which is provided - which channel is. Therefore, the data structure is defined from the object's concept of objects when using object-oriented programming ideas, which is completely different from COM. The COM is called a component object model. From the name, it is obviously the correctness in the first quotation mark, which is a misunderstanding. COM's most prominent contribution is not a component of the component, but an interface. The interface represents a collection of functions, which is not status. In contrast to object-oriented, it does not value the implementation of the object, even desadcussion of the concept of the object, which greatly emphasizes the concept of the interface, which is obvious in the various COM textbooks - the first thing is the iUnknown interface, strong Emphasize that there is no object pointer, only interface pointers. This looks a little confusing, if it is considered to be object-oriented, COM emphasizes the collection of functions. The class is to implement a COM component (excluding iUnknown) of an interface, which is fundamentally the degradation of COM. Therefore, when each COM component in the design implements an interface, it is not designed to design a COM application at this time, just applying the programming idea for C on binary code level. Since COM is not so good, it will produce the misunderstandings mentioned above. It emphasizes the concept of functionality, but more manifested as components, so it is easy to think that the component is building blocks, and the entire program is a house built with different components. This is the modular programming of the object level. COM will not design to go back to the old road in the end. The focus of the wood is building blocks, which is to build a house with blocks. And COM is not building blocks, it is the shape of the building blocks. It advocates that a shelf is taken first, and the different blocks can be placed in different plaids formed on the shelf, and the shelf is on the shelf. The shape of the lattice is a block, and finally put the building blocks on the shelf. Instead of doing building blocks first, then take a house according to the building blocks (this metaphor is not very accurate). Reflections this question: Design Two Interfaces ITASK and ItaskManager for the function of task and task manager, consider the function definition of iTask. The representative is a function that can act on the task, not the task itself, so it has the following two methods: TerminateTask and getProcessRateOftask to terminate the task and get task progress. But it is obvious that the task needs to be started.

If you don't consider the design mode, it is easy to think of this action as an object-oriented idea, which is easy to initiate this action as a method in iTask: StartTask, so that iTask's implementation is a full task, if you use threads Operation, which even packages together, forming a task. This is not a good design, itask is an interface, representing the function, not an object. The interface allows you to perform its definition, so that iTask's implementation can be equivalent to the same operation, not a task. The former has better scalability, such as operating things by pressing the remote control, but that thing does not necessarily must be a TV, and the latter must request a TV. Therefore, the focus in COM is not a component, but an interface, which is an extensible design idea that can be called an interface programming idea. It is itself a shortcoming, but it is implemented because of the concept of using the object, it must be associated with a status, which is not good when the amount of data is large. If the order will be easily designed into a class, then provide a variety of services such as order checkout, pickup and other services (ie, member functions). The problem here is that the order is so much. If an array is used as its container is obviously serious, and the linked list should be sentenced to death. Therefore, it is a very unwise choice to design an order as a class. For this, how to deal with how to handle large data volume is studied, and the function is opened, and the data becomes a raw material, and the function becomes a machine, and the efficiency is improved through the pipeline. That is, object-oriented is personalism. When the amount of data is large, we need division of labor to improve efficiency. For this, Microsoft has already provided MTS to help development, which provides the programming idea to specifically designer, advocating stateless components, namely state, and functional separation, Micro-state components, namely state, and functional separation, Micro Very good support (on MTS, refer to another article "MTS Basic Tutorial"). The programming model provided in COM has been described earlier, and this example shows how to design an interface. The programmer considers the most things should be lazy, and the beautiful name "code is reuse", but now it is replaced by the nice noun - "Extensibility." The sample is a company's information management system. The information processing of the personnel department and the marketing department will have a thousand different processes. Therefore, it is certain that a one is needed. But they can be called a department, there must be a common place, which is the most powerful place - induction, and then perform other things to achieve lazy purposes. As previously said, due to huge data volume, it is decided to select the database instead of establishing an object. Since there is no identity (in fact, there is still still, the post squee), finally believed that the same is the same as the same company, so decided to provide a basic framework interface, which provides the most basic, such as error handling, logging and other functions. To express all departments in the same basis under the same basist.

The basic framework needs to provide the error logging to facilitate the system's maintenance and error; you need to provide an interface framework to accommodate the operation interface of different sectoral components, that is, the provision of the menu command is required, and the toolbar is provided for the idea of ​​the Windows interface. And shortcuts; task manager needs to be provided because it is not an instant to look up in massive data. Therefore, it may be a person with a person-finding, but it initiates an order lookup or customer lookup, but due to Waiting for impatient and terminated the previous personnel search; information about the database system is required to make the department components can store data to a unified place for management. The department components need to provide interfaces for information operations (such as entry, lookup, etc.), as a Windows interface, regularly need to provide menus, maintenance operations of the toolbar (provided by the command of the description string); need to provide task execution progress To increase the role of the operator. After the above decision, the basic framework should have four interfaces, and the department components should have two interfaces. However, please note that an interface represents a function collection. If a component implements an interface, it indicates that all the features are implemented, but COM is unfortunately providing E_NOTIMPL error code, thus causing the wrong interface design - inside The method can be unrealistic. This error code is accurately said to be reserved for future expansion, that is, a parameter in the method represents the type of function, such as drawing a circle or painting rectangle, but it can be specified as an ellipse, this shape is temporarily Support, but I believe that later versions will be supported, this is the true meaning of E_NOTIMPL, but it is incorrectly applied, for example: the department components mentioned above should support an interface, which provides a partial component interface, menu, toolbar, The provision of shortcut keys. It is possible to operate a department component without using a toolbar, completely using a dialogue to get everything, then when the poor basic frame is incorrectly invoking the corresponding method when it requires idle processing of the toolbar, it will get an E_NOTIMPL, it should how? Therefore, it should be classified as an interface at the same time, so it should be provided with three interfaces: shortcut processing, menu processing, toolbar processing. Since the shortcut is not processed, it is only available, and there is no need to provide a process such as a menu status, so that the sector component should have four interfaces, three of which are optional. The above interface division is somewhat intensive, but this is just a problem of particle size. If you are willing to coarsely granular, you can also say that the basic framework requires only one interface. If you want fine grain, you can rest your tool strip processing interface and menu processing interface, which is where you are in a place. But it is also recommended to ensure that the method in the interface is to be implemented if one is implemented.

Finally, the IDL definition file is as follows: import "oaidl.idl"; import "OCIDL.IDL"; // Basic Framework Implement Imodulesite, provides basic operation [Object, UUID (1A201ABA-A669-4AC7-9DF8-2DA772E927FC), Pointer_Default (UNIQUE)] Interface Imodulesite: IUNKNOWN {// Provisioning the current display module, if you click the // handler field in the order lookup result in the marketing module, automatically jump to the personnel module to display the relevant information of the handler HRESULT CHANGEMODULE ([in] refclsid clsid, // module's CLSID / / module name is only used to prompt [in, string] wchar * PModuleName, // module command, indicating commands to make modules to execute, explained by modules [IN ] Ulong command, [in] ulong param; // module command related parameter hResult getFrameWindow ([out] hwnd * phwnd); // Return to the main frame window}; // Basic framework implementation IrrorReport, which provides a report error function [Object, UUID (1A201ABA-A669-4AC7-9DF9-2DA772E927FC), Pointer_Default (unique)] Interface IrrorReport: iUnknown {// Report a moderate error, equivalent to warning // filename represents the name of the source code file, ROW represents an error HRESULT ReportsoftError ([in, string] wchar * filename, [in] ulong row, [in, string] wchar * errorstring ); // Report violent type errors, equivalent to error HRESULT REPORTHARDERROR ([IN, String] Wchar * filename, [in] ulong row, [in, string] wchar * errorstring);} // Basic framework implementation ICOMPANYINFO, Database server information [Object, UUID (1A201ABA-A669-4AC7-9DFA-2DA772E927FC), Pointer_Default (unique)] interface iCompanyInfo: IUNKNOWN {// Return to the database server related information, host IP, server name and password HRESULT GETDATASERVERINFO ([IN , String] Wchar * loact, [in, string] wchar * server, [in, string] wchar * password;} // Basic framework implementation of ItaskManager, which provides task operation interface itask;

[Object, uuid (1A201ABA-A669-4ac7-9DFB-2DA772E927FC), pointer_default (unique)] interface ITaskManager: IUnknown {// add tasks HRESULT AddTask ([in, string] WCHAR * taskString, // task description string [in ] ITask * ptask, // Task pointer // Return the cookie [OUT] DWORD * PCOOKIE) of a task;}; // Basic Framework to implement iTaskNotify, provide a notification of tasks [Object, UUID (1A201ABA-A669-4AC7 -9DFC-2DA772E927FC), pointer_default (unique)] interface ITaskNotify: IUnknown {// notice specified task schedule has changed HRESULT ProcessRateChange ([in] DWORD cookie); // notify mission has ended HRESULT TaskOver ([in] DWORD cookie) }; // Department component must implement iModule, providing module operation [Object, UUID (1A201ABA-A669-4AC7-9DFD-2DA772E927FC), Pointer_Default (unique) Interface ImoDule: IUnknown {// Initialization module, NID is a module Sub-window ID hRESULT INITIALMODULE ([in] imodulesite * psite, [in] uint nid); // Return to the module icon HRESULT GETI ([out] hicon * phicon); // Return to the name of the module HRESULT GETNAME ([OUT String] Wchar ** PNAME);}; // Department component does not necessarily implement ImoduLCommand, which provides command unique to the execution module [ object, uuid (1A201ABA-A669-4ac7-9DFE-2DA772E927FC), pointer_default (unique)] interface IModuleCommand: IUnknown {HRESULT DoCommand ([in] ULONG command, [in] DWORD param);}; // door assembly necessarily achieving IModuleNotify, which provides a notification module pathway [object, uuid (1A201ABA-A669-4ac7-9DFF-2DA772E927FC), pointer_default (unique)] interface IModuleNotify: IUnknown {HRESULT OnActivate (); // when activated HRESULT OnDeActivate switching module ( ); // Cancel the activation} during module}; // Department components must implement ImoduleUi, which provides the related operation of the module interface [Object, UUID (1A201ABA-A669-4AC7-9E00-2DA772E927FC), Pointer_Default (unique) Interface ImoDule: IUNKNOWN {// Returns the main window of the module HRESULT GETMAINWINDOW ([OUT] hwnd * phwnd); // Rendering Shortcut HRESULT TRANSLATEACCELERATOR ([IN] MSG * PMSG);

// Department component does not necessarily implement iMenuudpate, which provides related operations of the menu in the module interface [Object, UUID (1A201ABA-A669-4AC7-9E01-2DA772E927FC), Pointer_Default (UNIQUE)] Interface iMenuupdate: IUnknown {HRESULT GETMENU ([OUT] HRESU * phmenu; HRESULT GETMENUITEMSTRING ([in] ulong nid, [out, string] wchar ** pstring);}; // When the department component creates a task, the task object must implement ITASK for the corresponding task management [ object, uuid (1A201ABA-A669-4ac7-9E02-2DA772E927FC), pointer_default (unique)] interface ITask: IUnknown {// return the progress of tasks HRESULT GetProcessRateOfTask ([out] float * pRate); HRESULT TerminateTask (); // termination Task // Binds the task and task manager HRESULT SETSKSITE ([IN] iTaskManager * PManager, [in] DWORD cookie;}; [uuid (1A201aba-a669-4ac7-9d00-2da772e927fc), Version (1.0), helpstring ( "ExampleBase 1.0 TypeLib")] library ExampleBaseLib {importlib ( "stdole32.tlb"); importlib ( "stdole2.tlb"); interface IModuleSite; interface IErrorReport; interface ICompanyInfo; interface ITaskManager; interface IModule; interface IModuleC OMMAND; Interface Immist; interface iMenuupdate; Interface iMenuupdate; Interface iMenupdate; Interface ketage; No more other uses, is a changeable DLL technology. This is the objective and characteristics of the sample (all departments completely different information processing methods) are determined, that is, a plug-in interface. The basic framework is equivalent to a player, and the department assembly is equivalent to a sound processing plugin. Since this is just a simple example, it is impossible to show the advantages of COM component characteristics, but the example gives an example of a threaded model is sufficient. If each department component is just information entry, information query, and information management (ignore its business processes, such as orders need to be linked), you can use another functional segmentation method, that is, the interface, entry information. Interfaces, query information interfaces and management information interface (or even abstract business to form business interface), this program will reflect the concept of components, but the complexity has also increased a lot, because its flexibility is much higher than Previous solution. Due to the support of the additive tool, more code is required, and there is no significance to this sample, the support is provided in this sample.

转载请注明原文地址:https://www.9cbs.com/read-22592.html

New Post(0)