How to implement a single case in the process of COM components

xiaoxiao2021-03-14  188

Recently, you have to do a status saver, you want to implement in the process, and save the status in memory, it is very simple, you only need to open a global memory area in the component application, you can achieve the effect, but suddenly thinking, why Can I do a single example? So, several attempts have been made. Problem: How to implement a single case COM process component. (SINGLEUSE for processes), discovery after carefully viewing: 1: Due to the implementation of the component's instance, the class factory is actually passing through the New way Creating an instance, so it is difficult to achieve the goal through a similar method in a single case design mode. If you want to implement, it is estimated that the class is the class factory, and the method of calling the component class provides the method (similar singleton mode, using global pointer to point to unique instance), then block the constructor, but think about the implementation of the MFC and ATL Complexity, I will give up this idea soon. 2: Perhaps, there is no practical need for a single order for components within the process, because every time you create an interface instance is not too much consumption (of course, to ensure that the implementation code in the component interface is simple, otherwise there will be memory loss). Or use the real shutdown. The data needs to be shared can be implemented by the outer data area of ​​the component application. So: MS does not consider how to simply generate a single-order component COM. For processed components, there is of course necessary single cases because the cost of new creation is quite high. 3: Think more, in fact, can actually improve the program structure to achieve a similar example. Principle: Although the single case of COM within the process is not good, it can achieve equivalent effects in the component to contain single-case objects. As long as the business logic is encapsulated into the object of implementation, the maximum savings can be achieved. And improve efficiency (reduce the loading of the program). To mention example: In VC: (Suitable for MFC or ATL) A: Components include CIMPLEMENT * PIMP. B: Package the component's business logic to CIMPLEMENT, the component's interface is only equivalent to one agent. C: Component creation When you get PIMP, ensure that PIMP points to a single object, which is easy, using a single case mode. In VB: a: comprises in Moudle the public gImp as classImpement b: public function getImpInstance () as object if gImp is nothing then set gImp = new classImplement endif set getImpInstanc = gImp end function c: business logic encapsulated into classImplement class This class is defined as private. D :: Component interface Gets the implementation object via GetImpinstance, call the corresponding function. Finally: This method does not implement a single example, just as possible to achieve the purpose of the single case. Because, in this way, if the creation of the component is not controlled by the caller (eg, multiple component programs), multiple instances will still be generated. The actual significance is big, you can see the specific application scenario. Stone in 2006.03.16

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

New Post(0)