Introduction to Component
Download
Report
Transcript Introduction to Component
Introduction to
Component-Based Engineering
Howard Abrams
[email protected]
Object-Oriented Design
Goals:
• Ease of Maintenance
• Robustness
• Code Reuse
Primary sources of reuse:
• Libraries and web services
Marketplace of “object stores” not realized
Go Beyond Objects!
9.23.2003
Introduction to CBE
2
Nature of
Software Components
}
Unification of Data and Function
Data Hiding
Unique Identity
Independent
Interactive
Replaceable
Reusable
9.23.2003
}
OOP
Use existing OOP
techniques, but
architect your apps
as components.
Introduction to CBE
3
Component Independence
Objects are extremely sticky.
• Objects as small as possible (object normalization)
• More efficient, but have unnecessary dependencies
CBD Solution:
•
•
•
•
Self contained and independent
Pass only “simple” (widely accepted) variables
Don’t make them too general
Components are slightly larger / Built using OOP
9.23.2003
Introduction to CBE
4
Component Interaction
Components are not plugins:
• Reusable in multiple applications
• Components can be connected to each other
Components built for an architecture:
•
•
•
•
Java Beans
Enterprise Java Beans
JSP Tag Libraries
Web Services
9.23.2003
Introduction to CBE
5
Component Replacement
Objects are only mildly replaceable.
Components must implement interfaces.
• Contracts or agreements
• Like a component’s Java Bean specification
Interfaces “out” are events (callbacks).
Think carefully about altering contract
• You can upgrade (add features) but don’t change
• Use versioning system or support old interfaces
When components only talk via interfaces, you
can upgrade/enhance with lower risk.
9.23.2003
Introduction to CBE
6
Object Replacement?
Replacing objects
means editing
all references
System Testing Critical
• Forgotten references
9.23.2003
Introduction to CBE
7
Component Replacement
Each “unit” only talks
to another unit via an
interface.
Goal: Components
can be safely replaced
by unit testing the
component’s interface
System testing is now
less critical
9.23.2003
Introduction to CBE
8
Components Replacement
First Name
Component-to-component
communication not always
possible…
Last Name
Do you convert the
data prior to calling?
9.23.2003
Customer Name
Do you modify the
component to accept
different data?
Introduction to CBE
9
Components and Glue Code
Identify/isolate “glue code” put in controller
Components more independent/reusable
Easier to replace components
Control
(App Rules and
Glue Code)
Model
View
(Components)
9.23.2003
(User Interface)
Introduction to CBE
10
Component Advantages
Can be upgraded
• Unit testing concentrates on testing interface
• Lower emphasis on system testing
• You can add, but you can’t take away
Can be reused
• Components can be made useful
• Less coding as your “Bag o’ Components”
grows
9.23.2003
Introduction to CBE
11
Component Tips
Don’t pass around objects. Keep it simple.
Concentrate more on independence and
reuse than on “object normalization.”
Create components based on “services”
Function instead of form or features.
Unit testing is critical and essential.
Avoid the temptation to put in everything
9.23.2003
Introduction to CBE
12
Component Tips (con’t)
Live your component interface:
• Java Bean
• JSP Tag Library
Create a level of trust in your components
Pretend others will use your components
•
•
•
•
Clean and simple interface
Liberal documentation
Include your JUnit test suite
Include examples of using the component
9.23.2003
Introduction to CBE
13
Component Tips (con’t)
BeanInfo
Property
Editors
JavaBean
Add more interfaces to
increase reuse
Component
Functionality
JSP Tags
Tag 1 Tag 2 Tag 3
9.23.2003
Code to the lowest
deployment requirements
Non-GUI JavaBean
is most versatile
JSP TagLib interface
• Properties == Parameters
• Need multiple methods?
Create “tag wrappers”
Introduction to CBE
14
XmlQuery JavaBean Example
Need to retrieve data from XML file
XML libraries quite complex …
Writing the same marshalling code over and over
Wrapper around XPath and DOM libraries
Properties:
• File - XML File
• Query - XPath Expression
• TrimWhiteSpace - boolean
Method: getValue() returns a String
9.23.2003
Introduction to CBE
15
XmlQuery is Serializable
public class XmlQuery implements Serializable
{
// Property to hold the XPath query expression…
protected String query = null;
// Property when set will trim all returned values.
protected boolean trimWhitespace = false;
// Property to hold the XML file to parse
protected File file = null;
// Transient factory classes re-established as needed.
transient DocumentBuilder docbuilder = null;
transient DocumentBuilderFactory docfactory = null;
// Support object to generate PropertyChange events.
PropertyChangeSupport changeSupport =
new PropertyChangeSupport(this);
9.23.2003
Introduction to CBE
16
Serialization Issues
Transient variables
• Stateless variables
• Those that loose meaning on another system
• Factory methods
Don’t initialize transients in constructor
• Initialize them as you need them
9.23.2003
Introduction to CBE
17
XmlQuery Setters/Getters
public String getQuery()
{
if (query == null)
return "";
else
return query;
}
public void setQuery (String newQuery)
{
String oldQuery = query;
query = newQuery;
changeSupport.firePropertyChange("query",
oldQuery, query);
}
9.23.2003
Introduction to CBE
18
Change Support Methods
public synchronized void
addPropertyChangeListener (PropertyChangeListener listener)
{
if (changeSupport == null)
changeSupport = new PropertyChangeSupport(this);
changeSupport.addPropertyChangeListener(listener);
}
public synchronized void
removePropertyChangeListener (PropertyChangeListener listener)
{
if (changeSupport == null)
changeSupport = new PropertyChangeSupport(this);
changeSupport.removePropertyChangeListener(listener);
}
9.23.2003
Introduction to CBE
19
Working with Bean Editors
Need to override the BeanInfo?
• Specify an icon representation
• Give a nicer interface to properties/methods
Manifest File
org/howardism/xml/XmlQuery.class
• By hand: Name:
Java-Bean: true
• Via Ant: <jar jarfile="${dist-beans}/xmlquery.jar">
<fileset dir="${build}" includes="org/howardism/**"/>
<manifest>
<section name="org/howardism/xml/XmlQuery.class">
<attribute name="Java-Bean" value="true"/>
9.23.2003
Introduction to CBE
20
Property Editors
If a property accepts any but basic types,
you’ll want to create an external editor.
You can create elaborate GUI editors…
A simple “text bridge” is usually better:
9.23.2003
Introduction to CBE
21
Property Editors: Example
public class ExceptionEditor extends PropertyEditorSupport
{
public String getAsText()
{
if (getValue() == null)
return "";
else
return ((Throwable) getValue()).getClass().getName();
}
public void setAsText(String value) throws IllegalArgumentException
{
if (value != null && ! value.trim().equals(""))
setValue( Class.forName(value).newInstance());
}
}
9.23.2003
Introduction to CBE
22
Property Editors: Example
AppComposer
Sun’s BeanBox
Sun’s BeanBuilder
9.23.2003
Introduction to CBE
23
Alarm JavaBean Example
Generates an “event” at a particular time
Properties:
• Time- Particular date or time in the future
• Identity- To label each event
Methods:
• startAlarm- Must be called
• stopAlarm- To stop event prematurely
Generates a “AlarmEvent” to listeners
9.23.2003
Introduction to CBE
24
Alarm Listener Methods
public synchronized void
addAlarmListener (AlarmListener listener)
{
if (alarmListeners == null)
alarmListeners = new java.util.Vector();
alarmListeners.addElement(listener);
}
public synchronized void
removeAlarmListener (AlarmListener listener)
{
if (alarmListeners == null)
return;
alarmListeners.removeElement(listener);
}
9.23.2003
Introduction to CBE
25
Alarm’s fireAlarm Method
protected void fireAlarm (String identity)
{
java.util.Vector targets = null;
synchronized (this)
{
if (alarmListeners != null)
targets = (java.util.Vector) alarmListeners.clone();
}
if (targets != null)
{
AlarmEvent evt = new AlarmEvent(identity);
for (int i = 0; i < targets.size(); i++)
{
AlarmListener target = targets.elementAt(i);
target.alarmGenerated(evt);
9.23.2003
Introduction to CBE
26
AlarmListener / AlarmEvent
public interface AlarmListener extends EventListener
{
public void alarmGenerated (AlarmEvent event);
}
public class AlarmEvent extends EventObject
{
public AlarmEvent (String label)
{
super(label == null ? "" : label);
}
}
9.23.2003
Introduction to CBE
27
Thoughts on Events
Event generation is the “output interface”
• Equivalent to a “Callback”
• Boils down to a simple function call
Java’s event mechanism not that nice
• Events in scripting languages generally nicer
• The “clean” event interface is a good thing
Tools like AppComposer compensate
• Helps client handle event-based communication
9.23.2003
Introduction to CBE
28
Thanks
www.howardism.org/geekin