来自MSDN-2001-OCT: Visual Tools and Languages/Visual Studio 6.0 Documentation/Visual Basic Documentation/Using Visual Basic/Component Tools Guide/Creating ActiveX Components/General Principles of Component Design/Organizing Objects: The Object Model及之后
1. Ojbect models
这一章讲了如何安排一个组件里各对象的关系以及对应的类模块的实例化设置,日后可再看一哈。
(1) object models 描述的是containment;class hierarchies 描述的是继承关系。
(2) Dependent objects are also referred to as nested objects. Set the Instancing property of a class module to PublicNotCreatable to make the objects created from that class dependent objects. 客户端程序通常只拥有依赖型对象的对象引用或对象集合的引用。
(3) 构造对象模型,就是要考虑好自己要描述的事物由几种对象构成,对象之间又是什么关系。对象之间的层次关系往往以属性或集合属性来维护,如下图所示An externally creatable object with a hierarchy of dependent objects:
![]()
注意,除了以上两种对象外,还有一种是friend对象,用来只共内部访问的。
另外,有时你也可以把常用的链接变为属性(比如那种好多个.的引用再引用)。
(4) Objects that will be both creatable and dependent should be designed to require no initialization beyond their Initialize events. ?
2. 循环引用
Microsoft Excel is written using C++ and low level COM interfaces, and it maintains the Parent properties of its objects without creating circular references.
所谓循环引用指的是:一个对象A的属性对象C,属性对象C通过C的parent属性又引用到对象A。这样A和C就构成循环引用。在VB里循环引用会导致对象久久不能被释放,即使你set nothing之后。而如果你终止了整个组件之后,虽然对象可以被释放,但仍然会有很多孤儿对象留在内存里直到应用终止。
孤儿是指:父对象的主引用已经终止,但是由于子对象有父对象的引用,导致父对象的terminate事件过程不会被执行,从而子对象也不会被销毁。这样子对象就成为了孤儿。
All VB objects are either explicitly created as COM objects (if they are exposed objects compiled into an ActiveX binary) or are created as such internally within VB. All COM objects must follow the COM contract, which means they must implement at least three methods: QueryInterface,AddRef,Release.
The second two of these are of interest to how VB knows when to terminate an object.
对循环引用的解决办法如下:
(1)引用父对象的时候用可以帮助定位到父对象的字符串。这个是MSDN推荐的。也是我打算采用的(其它的都太巧了,倒反让人觉得不靠谱)。
(2)引用父对象的时候直接用对象指针。就是通过ObjPtr得到父对象指针,然后通过CopyMemory直接引用到父对象,这样不会增加父对象的COM计数。
(3)每次引用父对象的时候,就直接再通过IUnkown接口把对象计数减一。You can cast your object to that type and call the Release method on it as soon as you have copied the object reference.
(4)自己写个TearDown函数,每次终止父对象的时候,先把子对象对父对象的引用移除。这个看sonic_andy在另一个帖子里也推荐了这种做法。也许是比较常见的做法。
注:(2)-(4)在这个文章里有可下载的例子。http://www.vbaccelerator.com/home/vb/Code/Techniques/Dealing_with_Circular_References/article.asp
(5)还可以用全局调试集合的方法。见这个帖子http://topic.youkuaiyun.com/u/20090705/04/de76dce2-031d-4566-b1b5-84380558328e.html?29582的33楼后半部分。
3. 要详读的
(1) Building ActiveX Controls
(2) Creating Property Pages for ActiveX Controls
(3) Creating Data Sources
(4) ActiveX Component Standards and Guidelines
4. Extending the Visual Basic Environment with Add-Ins
(1) Add-Ins在本质上是用extensibility model里的对象和集合制作的ActiveX components,用于扩展VB IDE的功能。
(2) Add-Ins的种类:Add-Ins(通常意义上的插件,通过IDE的菜单或工具条访问)、Wizard(可以用Wizard Manager来生成)、Utility(可以用作插件、也可以用作独立的程序)。
5. Accessing DLLs and the Windows API
(1) Windows API其实是一组DLL,供程序员调用来完成图形、管理窗口及管理内存等任务。
(2) 调用DLL会增大程序的风险,所以要特别小心。
(3) 这一章有个比较完整的论述讲述调用API的方方面面,主要是如何声明各种类型的参数和返回类型。有时间的时候可以详看一下。
6. Building Internet Applications
(1) There are two types of Visual Basic Internet applications: IIS applications and DHTML applications. In IIS applications, you make use of the Active Server Pages (ASP) object model to retrieve information from the user, send information to the browser, and maintain information about the current session. In DHTML applications, you use the Dynamic HTML (DHTML) object model to manipulate the elements on an HTML page.
DHTML applications require the end user to have Microsoft Internet Explorer version 4.0 or later, while IIS applications are browser- and operating system—independent to the end user. Because of this, IIS applications are the appropriate choice when you want to develop an application that reaches a broad audience. DHTML applications may be the more appropriate choice for intranet applications or applications that need to support remote or offline usage.
(2) 早期是静态网页,之后是基于CGI/ISAPI等的动态网页(完全在server端运行),之后是脚本。
(3) Scripting enables dynamic content by embedding executable scripts directly in an HTML page. Rather than querying the server for an executable, the browser can process scripts as it loads the HTML page. These scripts can be processed on either the client or the Web server. The most common languages for client-side scripting are VBScript and JavaScript. A common framework used for server-side scripting is called Active Server Pages, or ASP.
(4) In addition to DHTML applications and IIS applications, there are other types of Internet functionality you can add to your projects in Visual Basic, including:
- ActiveX components that can be used on HTML pages, either as part of an IIS or DHTML application or not. ActiveX components include controls, code components, and ActiveX documents. Using some or all of these solutions, you can create highly functional Internet solutions for your business needs.
- Internet extensions for Visual Basic applications. You can use special controls that provide some Internet-related functionality. These include the Internet Transfer control, the WebBrowser control, and the WinSock Control.
本节有上面几种应用的具体例子。另外,关于Web开发的最新技术可参考阅读:http://blog.youkuaiyun.com/slowgrace/archive/2009/07/08/4332400.aspx。
本文探讨了Visual Basic中组件设计的基本原则,包括对象模型组织、循环引用处理及组件标准。同时介绍了使用VB进行Web开发的不同方式,如IIS应用程序和DHTML应用程序,并讨论了各自的应用场景和技术细节。
4594

被折叠的 条评论
为什么被折叠?



