Components
David Talby
This Lecture
What Components Are
With Demonstrations in Delphi
Common Object Model (COM)
Creating and calling objects Distributed COM
Component-Oriented Software
Components
The Holy Grail of Software Engineering Build software by connecting existing components in simple ways Beyond Object-Oriented Software
The three parts of industry-scale reuse
A widely used object-oriented framework Developers building components for it Other developers using them to build apps
Small-scale success, large-scale failure
Using Components in Delphi
Visual Components
Visual editors, containers, dialogs
Database Connectivity Office Automation
Use of properties, events, methods Non-visual components Delegation & Inheritance between components Visual & Non-Visual use
Whole programs as components
Writing Components in Delphi
Writing Components
Inheritance
TObject
TPersistent TComponent
3rd Party Components
Use of properties, events, methods Tens of thousands of components Many are freeware or shareware www.torry.net and others Based on Object Pascal
TControl
TWinControl
TCustomListControl
Delphis Framework
TCustomCombo
TCustomComboBox
Can import components from COM,
CORBA, COM+, EJB, Web Services
TComboBox
A Components Interface
A components interface has:
Methods - like draw() and disconnect() Properties like color and name
What about implementation?
(wrappers to getter and setter methods) Events like onClick and onNetworkError (pointer to function or list of such pointers)
Completely unavailable to clients Can be one or many classes Can be in another language
Defining Components
Definition: a software component is a module with these properties:
It can be used by other software modules,
called clients (not just humans) Only its specification (interface) is required for clients to use it The clients and their authors do not need to be known to the components authors
Defining Components II
Usually theres another property:
framework the tool supports
It can be used within a visual designer tool To do so, it must be derived from the
Commercial component frameworks
COM (Visual Basic, Visual C++) Delphi (Delphi)
JavaBeans (JBuilder, Caf, WebSphere)
.NET (Visual Studio.NET)
Comparison of Reuse Techniques
Components are less abstract than frameworks Frameworks are incomplete applications: They compile, but they dont run Components are usually framework-specific Frameworks make components possible Frameworks are less abstract than patterns Include actual code, not just essays Specific to one programming language Specific to one application domain Many patterns came from successful FWs
COM: Common Object Model
Most widely used component FW on earth Microsofts goals in the early 90s
Attracting developers to Windows
Combining the speed and power of C++ with
COM introduced in 1995 as OLE 2.0
the ease of use of Visual Basic Versioning of Windows UI & Services OLE (Object Linking & Embedding) and Clipboard features in Office
A Binary Standard
COM objects are used via interface pointers Use by normal methods calls COM is a binary standard for doing this:
Common data types, marshalling Creating and destroying objects
calling methods: calling convention, argument passing
Supporting Interfaces
An object can implement many interfaces
All COM objects implement IUnknown
IUnknown defines QueryInterface()
Cast to another interface by giving its ID
IUnknown also defines AddRef() and Release()
Implementations can be changed dynamically
COM manages memory by reference counting
Versioning
COM interfaces are logically immutable
Change = New version of interface
For example, each Office version is backward compatible to old interfaces:
Not enforced, but required from developers
Word.Application.7, Word.Application.8,
Windows UI is updated in the same way
Buttons, Windows, Standard dialogs,
Current (last) version is Word.Application
Create objects using the Windows API The Registry names a server EXE or DLL which contains the implementation Object is created in server, and the client receives a pointer to it
Creating Objects
Creating Objects II
An interface must be uniquely identified
CoCreateInstance() receives a CLSID
CLSID is a 128-bit globally unique identifier PROGID is easier: Word.Application.10
The Windows Registry
\HKEY_CLASSES_ROOT\ CLSID\ {000209FF-0000-0000-C000-000000000046}\ LocalServer32 = C:\Program Files\...\WINWORD.EXE Word.Application.10\ CLSID = {000209FF-0000-0000-C000-000000000046}
Creating Objects III
COM Servers
Consequences
EXE or DLL file, with a server entry function DLL can be in-process or out-of-process Server can enquire client & context Request cannot specify an implementation OS manages DLLs and object pools
Requires operating system support A dynamic, very expensive operation Works on all versions of Windows since 95
Calling Methods
Simplest case: In-process DLL
Calls are dispatched using virtual tables
Client and Server in same process
Same as standard C++ (multiple inheritance) Calling convention is also same as in C/C++ Should be as efficient as virtual methods In reality slower, due to less optimization
Calling Methods II
The COM object can be in a different process
If its implemented in an EXE or out-of-process DLL Less efficient calls Shared data between clients Failure of either process wont terminate the other
Calling Methods III
The COM object can be in a different machine
This is DCOM: Distributed COM Transparent: Add network location in Registry Each method call is very expensive
Language Support
Any language can use COM objects
class in C++, interface in Delphi and Java
Each language has a syntax for interfaces
And can be used for writing such objects
Compiler Support Required
Handle COM method calls transparently Replace creation operator by CoCreateInstance() Replace casting with QueryInterface() Call AddRef() and Release() in = operator code Wrap returned error codes with exceptions
Defining COM Interfaces
Microsoft Interface Definition Language
Interface names, CLSID and properties Method names, argument names and types
Properties & events have a special syntax
Data Types: Primitives, arrays, COM objects
MIDL Compiler
in and out parameters, and other settings Produces Type Libraries (*.TLB Files)
Used by compilers & tools that support COM
Writing a COM Server
Start a new COM Server project
Development tool generates code for server
Define interfaces and implement them
Define interface in IDL (or visual tool)
entry, register and unregister functions Development tool generates empty IDL file
Build the DLL or EXE
Implement in your favorite language
Then use regsvr32.exe to register it
Calling Methods Summary
Distributed COM
DCOM is COMs extension to networks Location Transparency Language Neutrality Simple, two-way connection model
Connection Management
Easier than a custom socket-based protocol
Distributed garbage collection
Shared connection pool between clients
Efficient ping: per machine, groups all remote
object IDs, and piggy-backed on messages
Distributed COM II
Scalability
Protocol Neutrality: UDP, TCP, others Flexible deployment & redeployment Referral: passing remote references
Thread pool, multi-processing
No server downtime during redeployment Exploits COMs support for versioning A directory of waiting remote chess players A load-balancing broker component DCOM automatically short-circuits middleman
Distributed COM III
Security
Transparent: NTs basis & online admin tool Can also be programmatic Any NT security provider supports
Distributed Components
encryption and many authentication protocols
Injecting server code into the client side An infrastructure for load balancing and
fault tolerance (hot failover, recovery cache) Used by MTS for distributed transactions
DCOM Design Issues
Each method call is very expensive
Violate command-query separation to
Stateless components are more scalable Transparent proxy & stub model is also useful when objects are a different:
Thread Security Context Transaction Context
minimize network round-trips (=method calls) Also useful for ordinary COM objects
COM Based Technologies
All 90s MS technologies for software developers are based on COM
Tied to the Windows platform
A few examples
Efficient, scalable, language neutral
ActiveX: applications inside web browsers
MTS and COM+: Transaction services
VBA and Windows Scripting
DirectX, Internet Explorer, ODBC & ADO, ...
The Competition: JavaBeans
The Java Component Model
A component is a Java class
Component = Can be used in a visual designer
Relies heavily on reflection
With a default constructor
Property = defined by getAbc() and setAbc()
With added information in java.beans.*
Names, version, company,
Event = same as Swings event model
The Competition: RMI, EJB
Remote Method Invocation
Java-to-Java transparent remote objects Generates proxy and stub, like DCOM Supports exceptions, unlike (D)COM Easier to learn: Java only
Not protocol neutral, less admin tools
Enterprise JavaBeans
Adds distributed transaction services
All non-MS big names support it
An open standard, with multiple vendors
Making a Choice
The COM+/EJB issue is a huge economic struggle between giant companies The basic criteria:
COM based: One OS, language independent
Java based: One language, OS independent
But theres much more to it than that Microsoft is now upgrading to .NET
Web services: finding components on the net Much improved language inter-operability
Summary: Back to Basics
What is a component?
A software module that can be used by
Components are key to large-scale reuse
Delphi, COM, JavaBeans reuse is a success
Object-oriented methods alone arent
other modules, knowing only its interface Written inside a framework Can usually be inserted into a visual designer
A few leading frameworks lead the industry
Write your software as components!