This chapter starts with a quick tour of XPCOM-an introduction to the basic concepts and technologies in XPCOM and component development. The brief sections in this chapter introduce the concepts at a very high-level, so that we can discuss and use them with more familiarity in the tutorial itself, which describes the creation of a Mozilla component called WebLock.
The goal of XPCOM is to allow different pieces of software to be developed and built independently of one another. In order to allow interoperability between components within an application, XPCOM separates the implementation of a component from the interface, which we discuss in the "Interfaces" section. But XPCOM also provides several tools and libraries that enable the loading and manipulation of these components, services that help the developer write modular cross-platform code, and versioning support, so that components can be replaced or upgraded without breaking or having to recreate the application. Using XPCOM, developers create components that can be reused in different applications or that can be replaced to change the functionality of existing applications.
XPCOM not only supports component software development, it also provides much of the functionality that a development platform provides, such as:
We will discuss the above items in detail in the coming chapters, but for now, it can be useful to think of XPCOM as a platform for component development, in which features such as those listed above are provided.
Although it is in some ways structurally similar to Microsoft COM, XPCOM is designed to be used principally at the application level. The most important use of XPCOM is within Gecko, an open source, standards compliant, embeddable web browser and toolkit for creating web browsers and other applications.
XPCOM is the means of accessing Gecko library functionality and embedding or extending Gecko. This book focuses on the latter-extending Gecko-but the fundamental ideas in the book will be important to developers embedding Gecko as well.
Gecko is used in many internet applications, mostly browsers. The list includes devices such as the Gateway/AOL Instant AOL device and the Nokia Media Terminal. Gecko is also used in the latest Compuserve client, AOL for Mac OS X, Netscape 7, and of course the Mozilla client. At this time, Gecko is the predominant open source web browser.
XPCOM allows you to build a system in which large software projects can be broken up into smaller pieces. These pieces, known as components, are usually delivered in small, reusable binary libraries (a DLL on Windows, for example, or a DSO on Unix), which can include one or more components. When there are two or more related components together in a binary library, the library is referred to as a module.
Breaking software into different components can help make it less difficult to develop and maintain. Beyond this, modular, component-based programming has some well-known advantages, as Table 1 describes:
TABLE 1. Benefits from Modular CodeBenefit | Description |
Reuse | Modular code can be reused in other applications and other contexts |
Updates | You can update components without having to recompile the whole application |
Performance | When code is modularized, modules that are not necessary right away can be "lazy loaded", or not loaded at all, which can improve the performance of your application. |
Maintenance | Even when you are not updating a component, designing your appication in a modular way can make it easier for you to find and maintain the parts of the application that you are interested in. |
But it's not always a good idea to divide things up. There are some things in the world that just go together, and others that shouldn't be apart. For example, one author's son will not eat a peanutbutter sandwich if there isn't jam on it, because in his world, peanut butter and jam form an indelible union. Some software is the same. In areas of code that are tightly-coupled-in classes that are only used internally, for example-the expensive work to divide things may not be worth the effort.
The HTTP component in Gecko doesn't expose private classes it uses as separate components. The "stuff" that's internal to the component stays internal, and isn't exposed to XPCOM. In the haste of early Mozilla development, components were created where they were inappropriate, but there's been an ongoing effort to remove XPCOM from places like this.
Benefit | Description |
Reuse | 模块化的代码可以在其他的程序和环境中重用 |
Updates | 你不用重新编译程序就能更新组件 |
Performance | 当代码模块化以后,模块可以不用立即加载,可以"缓加载",或者根本不加在,这样可以改善你的程序的性能 |
Maintenance | 即便是你不更新组件,用一个模块化的方法设计你的程序可以让你更简单的找到和维护程序中你感兴趣的部分。 |
未完待续.................