|
|
|||||||||||
by David Musicant | |||||||||||
|
||||||
Building on those attributes, a component is a typed, deployable
unit of functionality that adheres to a contract of specified
interfaces and deployment rules with a component model framework. A
component has no observable state despite any instances or data it
may create. Further, a component such as an entity bean is not
simply its interfaces, EntityBean class, and descriptor
files, but also any objects, code, or other resources created by the
container in its deployment.
Inheritance actually consists of two concepts: inheritance and inclusion polymorphism. Inheritance is the incorporation of abstraction aspects in an interface or implementation by one entity of another. Polymorphism is the ability for one thing to appear in multiple forms depending on context, and the ability of different things to appear the same in a certain context. Inclusion polymorphism is more frequently referred to as sub-typing. For this article, we will use "component inheritance" to refer to the idea of both inheritance and inclusion polymorphism of components.
Component inheritance can be confusing and difficult to grasp
for OOP developers due to choices in implementation and
demarcation. Some implementations, such as ours, rely in part on
object inheritance. Other choices might use delegation, proxies, or
other devices that may not fit the object-oriented inheritance
definition. The hierarchy mapping is also specified in a less
overt manner. In Java's object inheritance, the relationship is
clearly marked by the key word extends. Component
inheritance might be declared in an XML file, as a metadata key, or
via some other manner--it may not be evident in the code at
all.
Components' separation of type and implementation offer a new
inheritance paradigm for OO developers. In OO, if an object
Magazine's class inherited from Product
and was defined in a class loader, the class loader could not load
a new Product implementation without breaking
Magazine. Further, Magazine had to be
compiled against Product code--Product's code had to be available at compile time.
Components, on the other hand, do not require the base component's
code at compile time. This is one of the most powerful advantages
for components. A base component's implementation can be changed at
runtime without breaking the inheritance relationship.
Component inheritance includes the ability to execute cross-type
actions. Thus, if we have a base Employee component and
the sub-types Manager and WorkerBee, we
can ask the Employment "family":
//Give me all the employees in Department 2
Collection employees =
employeeHome.getEmployeesInDepartment(
new Integer( 2 )
);
and expect to get back a collection of both Manager
and WorkerBee Employees.
Inclusion polymorphism works as it does for objects: components can be treated in the same manner and handled in the same context when treated as a base type. The implementation of the base component's inherited interface can be either overridden or utilized by the sub-type. Lastly, the base component has no knowledge of the sub-types that have inherited from it. This does not stop bounded inheritance, or the ability to restrict sub-types, but leaves this control in the hands of the deployer.
Why are EJBs components and not Plain Old Java Objects (POJOs)?
Many "enterprise" projects require numerous complex technologies, such as security architectures, transaction management, data manipulation, messaging, and other services. Custom solutions could be developed for these needs, but doing so would render most projects too expensive and unviable. Similar functionality has already been developed and tested by many groups. On the opposite end of the spectrum, a project could be fulfilled by an "off the shelf" application. However, this approach may not address the need for customization or combining functionality with existing software. Neither extreme is a perfect solution.
Component-oriented programming offers a balance between the extremes. A component-model framework gives companies the ability to combine custom software with tested, pre-developed software to provide services outside of the scope of a project or skills of their team. Components offer a great degree of customization on two levels:
A component can be deployed into numerous contexts and
implementations of the model framework without code changes. This
is enabled by loose coupling of components with services through
adherence to interface contracts. An entity bean can use the same
code (ejbStore()) to prepare instance data for
persistence regardless of whether it will be stored in a database,
XML file, email, or any other source.
Some programmers may want POJOs to replace EJBs due to a misconception about the purpose of EJBs, borne in part from the business context in which EJBs came to the industry. Sun needed to make sure the EJB architecture was adopted by vendors, and more importantly, give the vendors a market. While Sun wanted a new paradigm, they knew most companies would be unwilling to scrap their existing systems. Most were built on top of RDBMSes, and if Sun's new product had been incompatible, it would not have been a good business decision. Unfortunately, this resulted in too close of a relationship between EJBs and RDBMSes, both in design and in the minds of developers.
As a result, most resources on EJB speak of entity beans as though they were simply an object facade for database entries. Container-managed persistence is frequently regarded as synchronizing an entity bean with a database record (one example of this can be found in Enterprise JavaBeans, 3rd Edition, by Richard Monson-Haefel). While all of the popular EJB containers work only with databases, that is an implementation choice, not a specification requirement. Entity beans could represent emails persisted into email servers, files, XML, or some other entities we have yet to imagine. In fact, Sun's definition of EJBs makes no mention of data or databases. "The EJB architecture is a component architecture for the development and deployment of component-based distributed business applications."
Part of the problem also stems from shifting from OOP's development/compile-time focus to COP's deployment-oriented focus. Perhaps that is why components in general have not caught on in the world of Java like they did in Microsoft's world.
View all java.net Articles.
|
|