Upload
millicent-thomas
View
222
Download
1
Tags:
Embed Size (px)
Citation preview
9.23.2003 Introduction to CBE 2
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 3
Nature of Software Components
Unification of Data and Function Data Hiding Unique Identity
}OOP
Independent Interactive Replaceable Reusable}Use existing OOP
techniques, butarchitect your appsas components.
9.23.2003 Introduction to CBE 4
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 5
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 6
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 7
Object Replacement?
Replacing objects means editingall references
System Testing Critical• Forgotten references
9.23.2003 Introduction to CBE 8
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 9
Components Replacement
Customer Name
First Name
Last Name
Component-to-component communication not always possible…
Do you convert thedata prior to calling?
Do you modify thecomponent to accept
different data?
9.23.2003 Introduction to CBE 10
Components and Glue Code
Identify/isolate “glue code” put in controller
Components more independent/reusable
Easier to replace componentsControl(App Rules and
Glue Code)
Model(Components)
View(User Interface)
9.23.2003 Introduction to CBE 11
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 12
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 13
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 14
JSP Tags
Add more interfaces to increase reuse JSP TagLib interface• Properties ==
Parameters
JavaBean
BeanInfoPropertyEditors
Component Tips (con’t) Code to the lowest
deployment requirements
Non-GUI JavaBean is most versatile
• Need multiple methods? Create “tag wrappers”
Tag 1 Tag 2 Tag 3
ComponentFunctionality
9.23.2003 Introduction to CBE 15
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 16
XmlQuery is Serializablepublic 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 parseprotected 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 17
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 18
XmlQuery Setters/Getters
public String getQuery(){
if (query == null)return "";
elsereturn query;
}
public void setQuery (String newQuery){
String oldQuery = query;query = newQuery;
changeSupport.firePropertyChange("query", oldQuery, query);
}
9.23.2003 Introduction to CBE 19
Change Support Methodspublic 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 20
Working with Bean Editors
Need to override the BeanInfo?• Specify an icon representation• Give a nicer interface to
properties/methods Manifest File• By hand:• 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"/>
Name: org/howardism/xml/XmlQuery.class
Java-Bean: true
9.23.2003 Introduction to CBE 21
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 22
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 23
Property Editors: Example
AppComposer Sun’s BeanBox
Sun’s BeanBuilder
9.23.2003 Introduction to CBE 24
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 25
Alarm Listener Methodspublic 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 26
Alarm’s fireAlarm Methodprotected 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 27
AlarmListener / AlarmEventpublic 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 28
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