Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Component-Based Engineering Howard Abrams

Similar presentations


Presentation on theme: "Introduction to Component-Based Engineering Howard Abrams"— Presentation transcript:

1 Introduction to Component-Based Engineering Howard Abrams howard@howardism.org

2 9.23.2003Introduction to CBE2 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!

3 9.23.2003Introduction to CBE3 Nature of Software Components  Unification of Data and Function  Data Hiding  Unique Identity } OOP  Independent  Interactive  Replaceable  Reusable } Use existing OOP techniques, but architect your apps as components.

4 9.23.2003Introduction to CBE4 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

5 9.23.2003Introduction to CBE5 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

6 9.23.2003Introduction to CBE6 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.

7 9.23.2003Introduction to CBE7 Object Replacement?  Replacing objects means editing all references  System Testing Critical Forgotten references

8 9.23.2003Introduction to CBE8 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 9.23.2003Introduction to CBE9 Components Replacement Customer Name First Name Last Name  Component-to-component communication not always possible… Do you convert the data prior to calling? Do you modify the component to accept different data?

10 9.23.2003Introduction to CBE10 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 (Components) View (User Interface)

11 9.23.2003Introduction to CBE11 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

12 9.23.2003Introduction to CBE12 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

13 9.23.2003Introduction to CBE13 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

14 9.23.2003Introduction to CBE14 JSP Tags  Add more interfaces to increase reuse  JSP TagLib interface Properties == Parameters JavaBean BeanInfo Property Editors Component Tips (con’t)  Code to the lowest deployment requirements  Non-GUI JavaBean is most versatile Need multiple methods? Create “tag wrappers” Tag 1Tag 2Tag 3 Component Functionality

15 9.23.2003Introduction to CBE15 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

16 9.23.2003Introduction to CBE16 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);

17 9.23.2003Introduction to CBE17 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

18 9.23.2003Introduction to CBE18 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); }

19 9.23.2003Introduction to CBE19 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); }

20 9.23.2003Introduction to CBE20 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: Name: org/howardism/xml/XmlQuery.class Java-Bean: true

21 9.23.2003Introduction to CBE21 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:

22 9.23.2003Introduction to CBE22 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()); }

23 9.23.2003Introduction to CBE23 Property Editors: Example AppComposerSun’s BeanBox Sun’s BeanBuilder

24 9.23.2003Introduction to CBE24 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

25 9.23.2003Introduction to CBE25 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); }

26 9.23.2003Introduction to CBE26 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);

27 9.23.2003Introduction to CBE27 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); }

28 9.23.2003Introduction to CBE28 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

29 Thanks www.howardism.org/geekin


Download ppt "Introduction to Component-Based Engineering Howard Abrams"

Similar presentations


Ads by Google