Reusable Software Components Java Beans Design Pattern for
![Reusable Software Components Java Beans Reusable Software Components Java Beans](https://slidetodoc.com/presentation_image_h/7f57b9a3683404db747a6e8e945becfb/image-1.jpg)
Reusable Software Components Java Beans
![Design Pattern for Properties • • Property is a subset of a bean’s state Design Pattern for Properties • • Property is a subset of a bean’s state](http://slidetodoc.com/presentation_image_h/7f57b9a3683404db747a6e8e945becfb/image-2.jpg)
Design Pattern for Properties • • Property is a subset of a bean’s state which determines the appearance and behavior of the component Simple property Indexed Property Bound Property Constrained property
![Simple Property • Simple property has a single value. • N is the name Simple Property • Simple property has a single value. • N is the name](http://slidetodoc.com/presentation_image_h/7f57b9a3683404db747a6e8e945becfb/image-3.jpg)
Simple Property • Simple property has a single value. • N is the name of the property and T is its type • public T get. N(); • public void set. N(T arg) • For readonly property there is get. N() method only
![Indexed Property • One property may consists of multiple values stored in an array Indexed Property • One property may consists of multiple values stored in an array](http://slidetodoc.com/presentation_image_h/7f57b9a3683404db747a6e8e945becfb/image-4.jpg)
Indexed Property • One property may consists of multiple values stored in an array • • public T get. N(int index); public void set. N(int index, T value); public T[] get. N(); public void set. N(T values[]); where N may be a double data[] and T is double
![Bound Property • It can generate an event when the property is changed • Bound Property • It can generate an event when the property is changed •](http://slidetodoc.com/presentation_image_h/7f57b9a3683404db747a6e8e945becfb/image-5.jpg)
Bound Property • It can generate an event when the property is changed • The event is of type Property. Change. Event and is sent to objects that previously registered an interest in receiving such notifications • bean with bound property - Event source • Bean implementing listener -- event target
![Implement Bound Property in a Bean 1. Import java. beans package 2. Instantiate a Implement Bound Property in a Bean 1. Import java. beans package 2. Instantiate a](http://slidetodoc.com/presentation_image_h/7f57b9a3683404db747a6e8e945becfb/image-6.jpg)
Implement Bound Property in a Bean 1. Import java. beans package 2. Instantiate a Property. Change. Support object private Property. Change. Support changes = new Property. Change. Support(this); 3. Implement methods to maintain the property change listener list: public void add. Property. Change. Listener(Property. Change. Listener l) { changes. add. Property. Change. Listener(l); } also remove. Property. Change. Listener method is needed
![Event Source Cont. 4. Modify a property’s setter method to fire a property change Event Source Cont. 4. Modify a property’s setter method to fire a property change](http://slidetodoc.com/presentation_image_h/7f57b9a3683404db747a6e8e945becfb/image-7.jpg)
Event Source Cont. 4. Modify a property’s setter method to fire a property change event when the property is changed. Public void set. X(int new. X){ int oldx = x; x = new. X; changes. fire. Property. Change(“x”, old. X, new. X); }
![Implement Bound Property Listener 1. Listener bean must implement Property. Change. Listner interface public Implement Bound Property Listener 1. Listener bean must implement Property. Change. Listner interface public](http://slidetodoc.com/presentation_image_h/7f57b9a3683404db747a6e8e945becfb/image-8.jpg)
Implement Bound Property Listener 1. Listener bean must implement Property. Change. Listner interface public class My. Class implements Property. Change. Listener, Serializable 2. It must override this method: public abstract void property. Change(Property. Changeevent evt)
![Registration For example: Our. Button button = new Our. Button(); button. add. Property. Change. Registration For example: Our. Button button = new Our. Button(); button. add. Property. Change.](http://slidetodoc.com/presentation_image_h/7f57b9a3683404db747a6e8e945becfb/image-9.jpg)
Registration For example: Our. Button button = new Our. Button(); button. add. Property. Change. Listener(a. Button. Listener); …. .
![Constrained Property • It generates an event when an attempt is made to change Constrained Property • It generates an event when an attempt is made to change](http://slidetodoc.com/presentation_image_h/7f57b9a3683404db747a6e8e945becfb/image-10.jpg)
Constrained Property • It generates an event when an attempt is made to change it value • The event type is Property. Change. Event • The event is sent to objects that previously registered an interest in receiving an such notification • Those other objects have the ability to veto the proposed change • This allows a bean to operate differently according to the runtime environment
![Three Parts in Implementation of Constrained Property 1. Source bean containing one or more Three Parts in Implementation of Constrained Property 1. Source bean containing one or more](http://slidetodoc.com/presentation_image_h/7f57b9a3683404db747a6e8e945becfb/image-11.jpg)
Three Parts in Implementation of Constrained Property 1. Source bean containing one or more constrained properties 2. Listener objects that implement the Vetoable. Change. Listener interface. This object either accepts or rejects the proposed change. 3. Property. Change. Event object containing property name, old value, new value.
![Implement Constrained Property in a Bean with constrained property must 1. Allow Vetoable. Change. Implement Constrained Property in a Bean with constrained property must 1. Allow Vetoable. Change.](http://slidetodoc.com/presentation_image_h/7f57b9a3683404db747a6e8e945becfb/image-12.jpg)
Implement Constrained Property in a Bean with constrained property must 1. Allow Vetoable. Change. Listener object to register and unregister its interest in receiving notifications 2. Fire property change at those registered listeners. The event is fired before the actual property change takes place
![Implementation of Constrained Property in a Bean 1. Import java. beans package 2. Instantiate Implementation of Constrained Property in a Bean 1. Import java. beans package 2. Instantiate](http://slidetodoc.com/presentation_image_h/7f57b9a3683404db747a6e8e945becfb/image-13.jpg)
Implementation of Constrained Property in a Bean 1. Import java. beans package 2. Instantiate a Vetoable. Change. Support object: private Vetoable. Change. Support vetos=new Vetoable. Change. Support(this); 3. Implement methods to maintain the property change listener list: public void add. Vetoable. Changelistener(Vetoable. Changelistener l) { vetos. add. Vetoable. Change. Listener(l); }
![Cont. 4. Write a property’s setter method to fire a property change event: public Cont. 4. Write a property’s setter method to fire a property change event: public](http://slidetodoc.com/presentation_image_h/7f57b9a3683404db747a6e8e945becfb/image-14.jpg)
Cont. 4. Write a property’s setter method to fire a property change event: public void set. X(int new. X) { int old. X=X; vetos. fire. Vetoable. Change(“X”, old. X, new. X); //if no veto there X=new. X; changes. fire. Property. Change(“X”, old. X, new. X); }
![Implementing Constrained Property Listeners 1. Implements the Vetoable. Change. Listener interface which has an Implementing Constrained Property Listeners 1. Implements the Vetoable. Change. Listener interface which has an](http://slidetodoc.com/presentation_image_h/7f57b9a3683404db747a6e8e945becfb/image-15.jpg)
Implementing Constrained Property Listeners 1. Implements the Vetoable. Change. Listener interface which has an abstract method Void veto. Change(Property. Change. Event evt) 2. Overide this abstract method. This is the method that will be called by the source bean on each object in the listener list kept by vetoable. Change. Support object
![Persistence • It has the ability to save a bean to storage and retrieve Persistence • It has the ability to save a bean to storage and retrieve](http://slidetodoc.com/presentation_image_h/7f57b9a3683404db747a6e8e945becfb/image-16.jpg)
Persistence • It has the ability to save a bean to storage and retrieve it at a later time • Configuration settings are saved • It is implemented by Java serialization • If a bean inherits directly or indirectly from Component class it is automatically Serializable. • Transient keyword can be used to designate data members not be saved ex. Thread reference member
![Customizers • Property sheet may not be the best user interface for a complex Customizers • Property sheet may not be the best user interface for a complex](http://slidetodoc.com/presentation_image_h/7f57b9a3683404db747a6e8e945becfb/image-17.jpg)
Customizers • Property sheet may not be the best user interface for a complex component • It can provide step-by-step wizard guide to use component • It can provide a GUI frame with image which visually tells what is changed such as radio button, check box, . . . • It can customize the appearance and behavior of the properties
![Design Pattern for User Defined Events • Bean can generate events and send them Design Pattern for User Defined Events • Bean can generate events and send them](http://slidetodoc.com/presentation_image_h/7f57b9a3683404db747a6e8e945becfb/image-18.jpg)
Design Pattern for User Defined Events • Bean can generate events and send them to other objects in delegation event model • Listener registration and unregistration public void add. TListener(TListener x); public void remove. TListener(TListener x);
![User defined listener and event Timer class will fire off timeout event: public interface User defined listener and event Timer class will fire off timeout event: public interface](http://slidetodoc.com/presentation_image_h/7f57b9a3683404db747a6e8e945becfb/image-19.jpg)
User defined listener and event Timer class will fire off timeout event: public interface Timer. Listener extends Event. Listener{ public void time. Out(Time. Event e); } public class Timer. Event extends Event. Object{ int count=0; public Timer. Event(Object obj){super(obj); } public int get. Count(){ return count; } public void set. Count(int count){this. count=count; }}
![Timer class fires off timeout event to all registered objects public void start. Timer(){ Timer class fires off timeout event to all registered objects public void start. Timer(){](http://slidetodoc.com/presentation_image_h/7f57b9a3683404db747a6e8e945becfb/image-20.jpg)
Timer class fires off timeout event to all registered objects public void start. Timer(){ if (t==null){t=new Thread(this); t. start(); } public void run(){for(; ; ){ try{t. sleep(timeout); } catch(Exception e){} fire. Off(); }} void fire. Off(){Timer. Event te=new Time. Event(this); te. set. Count(number); Vector listeners = (Vector)listeners. clone(); for(int I=0; I<listeners. size(); I++){ ((Timer. Listener)listeners. element. At(I). time. Out(te); }
![Any class that implements Timer. Listener can register itself by calling add. Timer. Listener() Any class that implements Timer. Listener can register itself by calling add. Timer. Listener()](http://slidetodoc.com/presentation_image_h/7f57b9a3683404db747a6e8e945becfb/image-21.jpg)
Any class that implements Timer. Listener can register itself by calling add. Timer. Listener() Vector listeners = new Vector(); public void add. Timer. Listener(Timer. Listener l){ listeners. add. Element(l); } public void remove. Timer. Listener(Timer. Listener l){ listeners. remove. Element(l); } Any class that implements Timer. Listener interface must override the time. Out method of interface Timer. Listener to respond the notification
![Create a New Bean Steps 1. Create a directory for the new Bean. 2. Create a New Bean Steps 1. Create a directory for the new Bean. 2.](http://slidetodoc.com/presentation_image_h/7f57b9a3683404db747a6e8e945becfb/image-22.jpg)
Create a New Bean Steps 1. Create a directory for the new Bean. 2. Create the Java source file(s). 3. Compile the source file(s). 4. Create a manifest file. 5. Generate a JAR file. 6. Start the BDK 7. Test
![Example 1 package sunw. demo. colors; import java. awt. *; import java. awt. event. Example 1 package sunw. demo. colors; import java. awt. *; import java. awt. event.](http://slidetodoc.com/presentation_image_h/7f57b9a3683404db747a6e8e945becfb/image-23.jpg)
Example 1 package sunw. demo. colors; import java. awt. *; import java. awt. event. *; public class Colors extends Canvas { transient private Color color; private boolean rectangular; public Colors() { add. Mouse. Listener(new Mouse. Adapter() { public void mouse. Pressed(Mouse. Event me) { }); rectangular = false; set. Size(200, 100); change(); } change(); }
![Example 1 public boolean get. Rectangular() { return rectangular; } public void set. Rectangular(boolean Example 1 public boolean get. Rectangular() { return rectangular; } public void set. Rectangular(boolean](http://slidetodoc.com/presentation_image_h/7f57b9a3683404db747a6e8e945becfb/image-24.jpg)
Example 1 public boolean get. Rectangular() { return rectangular; } public void set. Rectangular(boolean flag) { this. rectangular = flag; repaint(); }
![Example 1 public void change() { color = random. Color(); repaint(); } private Color Example 1 public void change() { color = random. Color(); repaint(); } private Color](http://slidetodoc.com/presentation_image_h/7f57b9a3683404db747a6e8e945becfb/image-25.jpg)
Example 1 public void change() { color = random. Color(); repaint(); } private Color random. Color() { int r = (int)(255*Math. random()); int g = (int)(255*Math. random()); int b = (int)(255*Math. random()); return new Color(r, g, b); }
![Example 1 public void paint(Graphics g) { Dimension d = get. Size(); int h Example 1 public void paint(Graphics g) { Dimension d = get. Size(); int h](http://slidetodoc.com/presentation_image_h/7f57b9a3683404db747a6e8e945becfb/image-26.jpg)
Example 1 public void paint(Graphics g) { Dimension d = get. Size(); int h = d. height; int w = d. width; g. set. Color(color); if(rectangular) { g. fill. Rect(0, 0, w-1, h-1); } else { g. fill. Oval(0, 0, w-1, h-1); } } }
![Run Example 1 Keep java source in the appropriate package and compile Example : Run Example 1 Keep java source in the appropriate package and compile Example :](http://slidetodoc.com/presentation_image_h/7f57b9a3683404db747a6e8e945becfb/image-27.jpg)
Run Example 1 Keep java source in the appropriate package and compile Example : c: beansdemosunwdemocolors Create a Manifest File in c: beansdemo directory Example Name: sunw/demo/colors/Colors. class Java-Bean: True Generate a JAR file(if you are in c: beansdemo then use following example) Example : jar cfm. . jarscolors. jar colors. mft sunwdemocolors*. class Start the BDK and test. You can test with existing Our. Button Bean for event handling for change method
![Deployment of Bean • All java classes can be converted to a bean • Deployment of Bean • All java classes can be converted to a bean •](http://slidetodoc.com/presentation_image_h/7f57b9a3683404db747a6e8e945becfb/image-28.jpg)
Deployment of Bean • All java classes can be converted to a bean • Bean is compressed and saved in the format of jar file which contains manifest file, class files, gif files, and other information customization files • Sun Net. Beans, BDK, Visual Café, JBuilder, Visual Age are the bean builder tools
![JAR file • JAR file allows you to efficiently deploy a set of classes JAR file • JAR file allows you to efficiently deploy a set of classes](http://slidetodoc.com/presentation_image_h/7f57b9a3683404db747a6e8e945becfb/image-29.jpg)
JAR file • JAR file allows you to efficiently deploy a set of classes and their associated resources. • JAR file makes it much easier to deliver, install, and download. It is compressed.
![Manifest file Manifest. tmp Name: Simple. Bean. class Java-Bean: True. . . Manifest file Manifest. tmp Name: Simple. Bean. class Java-Bean: True. . .](http://slidetodoc.com/presentation_image_h/7f57b9a3683404db747a6e8e945becfb/image-30.jpg)
Manifest file Manifest. tmp Name: Simple. Bean. class Java-Bean: True. . .
![Creating and extract a jar file • Create a jar file jar cfm simplebean. Creating and extract a jar file • Create a jar file jar cfm simplebean.](http://slidetodoc.com/presentation_image_h/7f57b9a3683404db747a6e8e945becfb/image-31.jpg)
Creating and extract a jar file • Create a jar file jar cfm simplebean. jar manifest. tmp • Extracting files from a jar file jar xf simplebean. jar *. class
![Summary • Java. Bean is a platform-neutral component architecture for reusable software component • Summary • Java. Bean is a platform-neutral component architecture for reusable software component •](http://slidetodoc.com/presentation_image_h/7f57b9a3683404db747a6e8e945becfb/image-32.jpg)
Summary • Java. Bean is a platform-neutral component architecture for reusable software component • It is a black box component to be used to build large component or application • Property, method, event, introspector, customizer are parts of javabean interface
- Slides: 32