Intent
Allow programmatic access to the methods and properties of a component so that its structure and protocol can be determined at runtime.
Motivation
When you are building a component-based system, one of the fundamental questions that arises is "How do you tie the components together?" Graphical development tools, CASE tools, "Trader" services and the like all need to be able to determine the structure of components in order to connect them up properly. Consider the following two examples to illustrate why this is important:
Known Uses
In JavaBeans, introspection through the the java.beans.Introspector allows you to programmatically access a Bean's properties and methods. A client discovering a Bean's interface would ask for a BeanInfo? object with java.beans.Instrospector.getBeanInfo(Class beanClass). If one doesn't exist, it introspects the Bean, looking for methods starting with prefixes "get" and "set" and returns a BeanInfo? manufactured from this information.
In COM, ITypeInfo and IDispatch allow for programmatic discovery of an object's methods and properties. Development tools like VisualBasic discover methods, properties, and events for ActiveX controls. It displayes a graphical table that allows users to customize properties of controls inserted into forms, and uses it to discover the envents that can be dispatched from the control back to the form so the developer can implement EventHandlers (e.g., MyWidget_OnButtonClick would be an EventHandler shown in the form's drop-down list of functions and subroutines for a control named MyWidget?).
In most CORBA implementations there is an Interface respository that you can query for information about Modules, Interfaces, Methods and the like. This information can be used to construct Request objects using CORBA's Dynamic Invocation Interface to make CORBA calls whose structure was not known at runtime.
Related Patterns
In Java, one can FallBackOnReflection to support InterfaceDiscovery.
When using scripting languages, such as Tcl, to control components one can write a BootstrapScript that dynamically creates new commands based on the interfaces of a component found by InterfaceDiscovery.
The common way that systems implement InterfaceDiscovery is to SplitDesignTimeAndRunTime.
Sometimes it makes sense to externalize this interface location and signature information. Components can use ContainerManagedPersistence to archive its discovery of interfaces. For instance, CORBA Beans [http://www.trcinc.com/corbabeans] covers archiving method signature information so your code doesn't need to be recompiled if and when it changes.
I guess this is the best reason for having accessor names that begin with "get". (And a pretty weak reason at that.) -- DaveHarris
Actually, code performing InterfaceDiscovery of JavaBeans should use the bean APIs and not assume that accessors have names that begin with "get".
Alternatively, one can avoid explicit interfaces altogether and use a ComponentBus.
[Above the pattern as it evolved in the old format; it is currently being morphed into our standard format below. -ed]
Context
Applicability
Problem
Forces
Participants
Structure
Collaborations
Resulting Context
Consequences
Known Uses
In JavaBeans, introspection through the the java.beans.Introspector allows you to programmatically access a Bean's properties and methods. A client discovering a Bean's interface would ask for a BeanInfo? object with java.beans.Instrospector.getBeanInfo(Class beanClass). If one doesn't exist, it introspects the Bean, looking for methods starting with prefixes "get" and "set" and returns a BeanInfo? manufactured from this information.
In COM, ITypeInfo and IDispatch allow for programmatic discovery of an object's methods and properties. Development tools like VisualBasic discover methods, properties, and events for ActiveX controls. It displayes a graphical table that allows users to customize properties of controls inserted into forms, and uses it to discover the envents that can be dispatched from the control back to the form so the developer can implement EventHandlers? (e.g., MyWidget_OnButtonClick would be an event handler shown in the form's drop-down list of functions and subroutines for a control named MyWidget?).
In most CORBA implementations there is an Interface respository that you can query for information about Modules, Interfaces, Methods and the like. This information can be used to construct Request objects using CORBA's Dynamic Invocation Interface to make CORBA calls whose structure was not known at runtime.
Example
Implementation
Sample Code
Related Patterns
In Java, one can FallBackOnReflection to support InterfaceDiscovery.
When using scripting languages, such as Tcl, to control components one can write a BootstrapScript that dynamically creates new commands based on the interfaces of a component found by InterfaceDiscovery.
The common way that systems implement InterfaceDiscovery is to SplitDesignTimeAndRunTime.
Sometimes it makes sense to externalize this interface location and signature information. Components can use ContainerManagedPersistence to archive its discovery of interfaces. For instance, CORBA Beans [http://www.trcinc.com/corbabeans] covers archiving method signature information so your code doesn't need to be recompiled if and when it changes.
This material seems to be relevant to the subject matter of ServiceOrientedArchitecture, which has become a new buzzword. Perhaps some of this content could be moved to that page. Or are we looking at systems and environments from a different level of granualarity?
CategoryInterface ComponentDesignPatterns CategoryPattern CategoryDiscovery