First, I declare that I am a com cainiao. My knowledge of com mainly comes from DirectX and several books. This article is my understanding of the COM structure, for errors and float details, please kindly advise. Thank you.
(1) I think the most obvious design pattern in COM is the factory pattern.In the factory model, to get a product, users do not need to know which type of the product is created, and users only need to pass the requirements to the factory that produces the product. The following is a section of the DirectDraw C ++ code:
// The following code is taken from the example in kanepeng's Guide to game programming. This book is the easiest to understand in the DirectX programming book. We recommend this to anyone who wants to know about DirectX programming.
Lpdirectdraw7 lpdd; // pointer of the DirectDraw object
Ddsurfacedesc2 ddsd; // DirectDraw page description
Directdrawcreateex (null, (void **) & lpdd, iid_idirectdraw7, null); // create a DirectDraw object
Lpdd-> setcooperativelevel (hwnd, ddscl_exclusive | ddscl_fullscreen); // sets the DirectDraw control level
Lpdd-> setdisplaymode (800,600, 32, 0, ddsdm_standardvgamode); // set the Display Mode
// Start to create the Home Page. Clear the page description first.
Memset (& ddsd, 0, sizeof (ddsurfacedesc2 ));
// Fill in the page description
Ddsd. dwsize = sizeof (ddsd );
Ddsd. dwflags = ddsd_caps | ddsd_backbuffercount; // There is a background Cache
Ddsd. ddscaps. dwcaps = ddscaps_primarysurface | ddscaps_flip | ddscaps_complex; // This is the home page.
Ddsd. dwbackbuffercount = 1; // a background Cache
Lpdd-> createsurface (& ddsd, & lpddsprimary, null); // create a home page
Ddsd. ddscaps. dwcaps = ddscaps_backbuffer; // This is the background cache.
Lpddsprimary-> getattachedsurface (& ddsd. ddscaps, & lpddsbuffer); // create a background Cache
// The following statement applies black to all lpddsprimary and lpddsbuffer.
Ddbltfx;
Ddbltfx. dwsize = sizeof (ddbltfx );
Ddbltfx. dwfillcolor = RGB (, 0); // The color to be filled
Lpddsprimary-> BLT (null, ddblt_wait | ddblt_colorfill, & ddbltfx );
Lpddsbuffer-> BLT (null, ddblt_wait | ddblt_colorfill, & ddbltfx );
Ddsd. dwsize = sizeof (ddsd );
Ddsd. dwflags = ddsd_caps | ddsd_width | ddsd_height;
Ddsd. ddscaps. dwcaps = ddscaps_offscreenplain; // This is the off-screen page.
Ddsd. dwheight = 16; // high
Ddsd. dwwidth = 16; // width
Lpdd-> createsurface (& ddsd, & lpddsback, null); // create a page for the background image
The global method directdrawcreateex in the 1st red statements actually plays the role of the factory. The third input parameter of this function is "the type of product required by the user ". "Iid_idirectdraw7" is the ID of the diretctdraw7 interface. The factory produces a directdraw7 interface and stores the pointer in lpdd. In the 2nd and 3rd red statements, lpdd is used as a factory to produce two products of the same type. The product type is in the ddscaps of the ddsd structure. set in the dwcaps field (the set statements for this field are marked in blue ). We can clearly see that the global method directdrawcreateex is a factory used to produce factories, and its products are factories.
(2) There is also a mode in com called interceptor ).I have seen this model from the book. This book is called pattern-oriented software architecture, and is an award-winning book, volume 2 of this book specifically describes the patterns used for concurrent and networked objects. I borrowed a Chinese version from the school library. It is rare that there are still such good books in the library. I have occupied this book for three months and will continue to occupy it through the method of renewal, until graduation ...... Pai_^
This mode is explained in detail in section 2.3 of the book. The idea of this mode is that "a class is a set of interfaces and interfaces are used as classes, the class that owns this interface must inherit this interface class ". The old method for adding class functions (adding abstract methods to the base class to add new functions to each subclass) has a fatal weakness, that is, users of the new version of the class library may use the new version method on the old version of the object, which will cause the program to crash (this is described in detail in section 1.8 of "com essence-based essntial com ).
I really admire the designers of this model. In my mind, multi-inheritance is an object-oriented taboo, A category cannot be derived from either a base class or another base class (logically speaking, this is not the case ). The idea of this mode is that a type can have both the attributes of a base class and the attributes of another base class. The attributes here are interfaces. It is a great innovation to regard interfaces as classes. (I feel that this great idea can be born only in C ++)