1 / 26

Java Bean Definition

Java Bean Definition. “A Java Bean is a reusable software component that can be manipulated visually in a builder tool.”. JavaBean. Properties Similar to instance variables. Methods Same as normal Java methods. Every property should have accessor (get) and mutator (set) method. Events

janice
Télécharger la présentation

Java Bean Definition

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Java Bean Definition “A Java Bean is a reusable software component that can be manipulated visually in a builder tool.”

  2. JavaBean • Properties • Similar to instance variables. • Methods • Same as normal Java methods. • Every property should have accessor (get) and mutator (set) • method. • Events • Similar to Swing/AWT event handling.

  3. Features of a Java Bean • Support for “introspection” so that a builder tool can analyze how a bean works. • Support for “customization” to allow the customisation of the appearance and behaviour of a bean. • Support for “events” as a simple communication metaphor than can be used to connect up beans. • Support for “properties”, both for customization and for programmatic use. • Support for “persistence”, so that a bean can save and restore its customized state.

  4. Why use JavaBeans? • Component Granularity • Portability • Simplicity • Uniform, high-quality API that will enable developers to write programs that will perform consistently everywhere.

  5. JavaBeans Vs Class Libraries • Beans should be used for software components that can be visually manipulated and customized. • Class libraries should be used to provide functionality to programmers, but which doesn't benefit from visual manipulation.

  6. Java Beans Concepts • A component is a self-contained reusable software unit • Components expose their features (public methods and events) to builder tools • A builder tool maintains Beans in a palette or toolbox. • You can select a bean from the toolbox, drop it in a form, and modify its appearance and behavior. • Also, you can define its interaction with other beans • ALL this without a line of code.

  7. Java Bean Characteristics • a public class with 0-argument constructor • it has properties with access methods • it has events • it can be customized • its state can be saved • it can be analyzed by a builder tool

  8. Key Concepts • A builder tool discover a bean’s features by a process known as introspection. • Adhering to specific rules (design pattern) when–naming Bean features. • Providing property, method, and event information–with a related Bean Information class. • Properties (bean’s appearance and behavior characteristics) can be changed at design-time.

  9. Key Concepts…. • Properties can be customized at design-time. • Customization can be done: • using property editor • using bean customizers • Events are used when beans want to intercommunicate • Persistence: for saving and restoring the state • Bean’s methods are regular Java methods. • Beans will also be running in a multi-threaded environment

  10. Security Issues • JavaBeans are subject to the standard Java security model • The security model has neither been extended nor relaxed. • If a bean runs as an untrusted applet then it will be subject to applet security • If a bean runs as a stand-alone application then it will be treated as a normal Java application.

  11. Beans Development Kit (BDK) • To start the BeanBox: • run.bat (Windows) • run.sh (Unix)

  12. BDK • Tool Box contains the beans available • Bean Box window is the form where you visually wire beans together. • Properties sheet: • displays the properties for the Bean currently selected within the BeanBoxwindow.

  13. MyFirstBean import java.awt.*; import java.io.Serializable; public class FirstBean extends Canvas implementsSerializable { public FirstBean() { setSize(50,30); setBackground(Color.blue); } }

  14. First Bean • Compile: javac FirstBean.java • Create a manifest file: • manifest.txt (jar file metadata) • Name: FirstBean.Class • Java-Bean: True • Create a jar file: • jar cfm FirstBean.jar mani.txt FirstBean.class

  15. Properties • Bean’s appearance and behavior • changeable at design time. • They are private values • Can be accessed through getter and setter methods getter and setter methods must follow some rules -- design patterns (documenting experience)

  16. Design Patterns for properties • Property is a subset of a bean’s state • Simple properties • Has a single value • Public T getN(); • Public void setN(T arg); • Public boolean isN(); ( isdotted) • Indexed properties • Consists of multiple values • Public T getN(index); • Public void setN(int index, T value); • Public T[ ] getN(); • Public void setN(T values[ ]);

  17. Introspection • Process of analyzing a bean to determine its capabilities • Allows an application builder tool to present information about a software component to the designer • Simple naming conventions • A class that explicitly supplies this information

  18. Properties • A builder tool can: • discover a bean’s properties • determine the properties’ read/write attribute • locate an appropriate “property editor” for each type • display the properties (in a sheet) • alter the properties at design-time

  19. Types of Properties • Simple • Index: multiple-value Properties • Bound: provide event notification when value changes • Constrained: how proposed changes can be okayed or vetoed by other object

  20. Simple Properties • When a builder tool introspect your bean it discovers two methods: • public Color getColor() • public void setColor(Color c) • The builder tool knows that a property named “Color” exists -- of type Color. • It tries to locate a property editor for that type to display the properties in a sheet.

  21. Simple Properties…. • Adding a Color property • Create and initialize a private instance variable • private Color color = Color.blue; • Write public getter & setter methods public Color getColor() {return color; } public void setColor(Color c) { color = c;–repaint(); }

  22. Indexed Properties • An indexed property is for when a single property can hold an array of values. • The design pattern for these properties is: public void setPropertyName (PropertyType[] list) public void setPropertyName ( PropertyType element, int position) public PropertyType[] getPropertyName () public PropertyType getPropertyName (int position)

  23. For instance, if you were to complete the item indexed property for the AWT List component, it might look something like this: public class ListBean extends List { public String[] getItem () { return getItems (); } public synchronized void setItem (String item[]) { removeAll(); for (int i=0;i<item.length;i++) addItem (item[i]); } public void setItem (String item, int position) { replaceItem (item, position) } } The String getItem (int position) routine already exists for List

  24. Bound Properties • Provide event notification when value changes • In order for the notification to happen, you need to maintain a watch list for PropertyChangeEvents via the PropertyChangeSupport class. • First, you have to create a list of listeners to maintain: private PropertyChangeSupport changes = new PropertyChangeSupport (this); And then, you have to maintain the list: public void addPropertyChangeListener (PropertyChangeListener p) { changes.addPropertyChangeListener (p); } public void removePropertyChangeListener ( PropertyChangeListener p) { changes.removePropertyChangeListener (p); }

  25. Constrained properties • Constrained properties are similar to bound properties. • In addition to maintaining a list of PropertyChangeListeners, the Bean maintains a list of VetoableChangeListeners. • Prior to the Bean changing a property value, it asks the VetoableChangeListeners if its okay. • If it isn't, the listener throws a PropertyVetoException, which you declare the set routine to throw.

  26. Adding… private VetoableChangeSupport vetoes = new VetoableChangeSupport (this); public void addVetoableChangeListener ( VetoableChangeListener v) { vetoes.addVetoableChangeListener (v); } public void removeVetoableChangeListener ( VetoableChangeListener v) { vetoes.removeVetoableChangeListener (v); }

More Related