Component Tools Guide略读笔记(2):General Principles of Component Design

本文详细介绍了组件设计的基本概念,包括部件的分类、接口设计、CLSID的使用、初始化和终止事件的处理等内容。此外还探讨了类模块与标准模块的区别、属性和方法的实现方式以及如何提供组件的命名常量。

来自MSDN-2001-OCT: Visual Tools and Languages/Visual Studio 6.0 Documentation/Visual Basic Documentation/Using Visual Basic/Component Tools Guide/General Principles of Component Design

1. 部件基础

(1) An application or component that uses objects provided by another component is called a client. Components are characterized by their location relative to clients.

An out-of-process component is an .exe file that runs in its own process, with its own thread of execution.

An in-process component, such as a .dll or .ocx file, runs in the same process as the client.

(2) The names you select for your class modules and for their properties, methods, and events make up the interface(s) by which your component will be accessed.

(3) 确定工程的种类(有一个比较详细的讨论,不过我觉得没有略读笔记(1)里摘的那个说的清楚)。

(4) 设置工程的属性。开始一个新的component工程后,有些属性要设定:name, description, startup object(指定初始化代码的位置)。还可以设置version number, 编译, 帮助文件, version compatibility 等。

2. interface的概念及CLSID

(1) Information about the classes provided by your component is contained in a type library. In Visual Basic, the type library is included as a resource in the compiled component. Clients access the type library by setting references to it.

(2) The combination of project name and class name is sometimes referred to as a fully qualified class name, or as a programmatic ID.

Public gwdgDriveLink As SmallMechanicals.Widget

(3) Every class provided by your component has at least one interface, called the default interface, which is composed of all the properties and methods you declare in the class module.

The default interface is usually referred to by the same name as the class, though its actual name is the class name preceded by an underscore. The underscore prefix is a convention, signifying that the name is hidden in the type library.

If the class raises events, it also has an IConnectionPointContainer interface that enumerates those events. Events are outgoing interfaces, as opposed to the incoming interfaces composed of properties and methods. In other words, clients make requests by calling into your class’s properties and methods, while the events raised by your class call out to event handlers in clients.

所以,缺省的是incoming interface,而events则是out-going interfaces.

(4) GUID (pronounced goo-id) stands for Globally Unique IDentifier, a 128-bit (16-byte) number generated by an algorithm designed to ensure its uniqueness.

GUIDs are used to uniquely identify entries in the Windows registry. For example, Visual Basic automatically generates a GUID that identifies your type library in the Windows registry.

Visual Basic also automatically generates a GUID for each public class and interface in your component. These are usually referred to as class IDs (CLSID) and interface IDs (IID). Class IDs and interface IDs are the keys to version compatibility for components authored using Visual Basic.

Note   You may also see GUIDs referred to as UUIDs, or Universally Unique IDentifiers.

IID可以帮助维持版本的可兼容性。在你对你的版本做可能不兼容的改动的时候,系统会警告你,并且会建议你适当改名字。

3. Starting and Ending a Component

(1) 对于ActiveX dll和ActiveX exe而言,是不允许用forms作为启动对象的。因为这些可能导致对象初始化过长而time-out.

(2) 处理lengthy initialization有几种方法:Class_Initialize event或者在后台做using a call-back timer。但不管哪种办法都要注意用全局标志位来防止反复初始化。

(3) in-process和out-of-process组件的shut down过程有所不同。

4. Adding Classes to Components

(1) a programmatic ID or ProgID:组件名+类名,如Finance.BusinessRule

(2) 部件与其他应用的区别在于,部件至少提供1个公共类,该公共类可供client程序来使用控制。

(3) Public or Instancing Property: UserControl classes have a Public property that determines whether the class is public or private. 其他种类的部件也有相关的设置来决定类是否可供外部调用。

5. Instancing for Classes Provided by ActiveX Components

(1) 有Private, PublicNotCreatable, MultiUse, GlobalMultiUse, SingleUse, GlobalSingleUse

(2) Dependent Objects (PublicNotCreatable)

(3) MultiUse and Multithreading

(4) Global Objects有点类似application那种对象。就是不用声明对象就可以直接用方法属性啥的。好玩的特性,可是貌似我没办法用得上,因为activex control并不支持后几种实例化方法。

 

6. Coding Robust Initialize and Terminate Events

(1) Errors that occur in the Class_Initialize event procedure are returned to the point in the client at which the object was requested.

(2) Errors in the Terminate event require careful handling. Because the Terminate event is not called by the client application, there is no procedure above it on the call stack. This means that an unhandled error in a Terminate event will cause a fatal error in the component

You should always handle errors in the Class_Terminate event procedure. Errors in Class_Terminate cannot be handled by applications that use your component, and will therefore be fatal to the application.

注意:Terminater事件是在对象的引用计数为0时由vb运行环境在销毁对象前自动执行的,当时不在任何一个用户子程序环境中,不知道堆栈的情况,不知道当前的enabled error handler是哪一个。所以,即使有对象外部有on error goto 或 on error resume 也不能捕获这样的错误,从而对应用是致命的。所以,只能在terminate事件内部处理。所以,Terminal事件外的其它时间不存在这样的无法捕捉的情况。见这个帖子的讨论,感谢wangmu7206

7. Standard Modules vs. Class Modules

(1) Class module data, on the other hand, exists separately for each instance of the class. 最好避免在类模块里依赖标准模块里的公共变量,因为可能会导致逻辑上的错误。

(2) 类的静态数据

However, there may be occasions when you want a data member to be shared among all objects created from a class module. This deliberate violation of encapsulation is sometimes referred to as static class data.

You can implement static class data in a Visual Basic class module by using Property procedures to set and return the value of a Public data member in a standard module, as in the following code fragment:

' Read-only property returning application name.
Property Get ComponentName() As String
   ' The variable gstrComponentName is stored in a 
   '   standard module, and declared Public.
   ComponentName = gstrComponentName
End Property

8. Adding Properties and Methods to Classes

(1) Implementing Properties in Components:尽量用properties而不要用公共变量,主要出于稳定的考虑。

另一方面,Internally, Visual Basic generates a pair of property procedures for every public variable you declare. For this reason, declaring public variables doesn’t provide any size or performance benefits.

(2) Persisting a Component's Data:保存类的属性的初始值的方法。

While ActiveX controls have always been able to persist their data; persistence for ActiveX components is slightly different. A control stores property settings inside it's .cls file, but a component can't do that. Instead, it uses a PropertyBag object that can be saved just about anywhere – in a file, a database, a cell in a spreadsheet, or even in the registry.

(3) Data Types Allowed in Properties and Methods:见后续笔记。

9. Providing Named Constants for Your Component

(1) Although an enumeration must appear in a module that defines a class, it always has global scope in the type library. It is not limited to, or associated in any other way with the class in which you declared it.

(2) General Purpose Enumerations

(3) When you declare a variable using the name of an Enum as the data type, you’re effectively declaring the variable As Long.

(4) Occasionally you may need to provide a string constant, or a constant that isn’t an integer value. Visual Basic doesn’t provide a mechanism for adding such values to your type library as public constants, but you can get a similar effect using a global object with read-only properties.

内容概要:本文系统介绍了算术优化算法(AOA)的基本原理、核心思想及Python实现方法,并通过图像分割的实际案例展示了其应用价值。AOA是一种基于种群的元启发式算法,其核心思想来源于四则运算,利用乘除运算进行全局勘探,加减运算进行局部开发,通过数学优化器加速函数(MOA)和数学优化概率(MOP)动态控制搜索过程,在全局探索与局部开发之间实现平衡。文章详细解析了算法的初始化、勘探与开发阶段的更新策略,并提供了完整的Python代码实现,结合Rastrigin函数进行测试验证。进一步地,以Flask框架搭建前后端分离系统,将AOA应用于图像分割任务,展示了其在实际工程中的可行性与高效性。最后,通过收敛速度、寻优精度等指标评估算法性能,并提出自适应参数调整、模型优化和并行计算等改进策略。; 适合人群:具备一定Python编程基础和优化算法基础知识的高校学生、科研人员及工程技术人员,尤其适合从事人工智能、图像处理、智能优化等领域的从业者;; 使用场景及目标:①理解元启发式算法的设计思想与实现机制;②掌握AOA在函数优化、图像分割等实际问题中的建模与求解方法;③学习如何将优化算法集成到Web系统中实现工程化应用;④为算法性能评估与改进提供实践参考; 阅读建议:建议读者结合代码逐行调试,深入理解算法流程中MOA与MOP的作用机制,尝试在不同测试函数上运行算法以观察性能差异,并可进一步扩展图像分割模块,引入更复杂的预处理或后处理技术以提升分割效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值