1 / 28

14 - JavaBeans

14 - JavaBeans. Content. What is a Java Bean? BDK What makes Bean possible? Support for Java Bean References. Hvad er Java Beans?. Software komponent Specificerede interfaces. Uafhængig deployment Definition:

ash
Download Presentation

14 - JavaBeans

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. 14 - JavaBeans

  2. Content • What is a Java Bean? • BDK • What makes Bean possible? • Support for Java Bean • References

  3. Hvad er Java Beans? • Software komponent • Specificerede interfaces. • Uafhængig deployment • Definition: “A Java bean is a reusable software component that can be visually manipulated in builder tools.” Java Bean Tutorials

  4. What is JavaBeans? • JavaBeans er en portable, platform-uafhængig komponent model, som er skrevet i Java. • Den giver udviklere mulighed for at skrive genbrugelige komponenter “once and run them anywhere”. • Målet for JavaBeans er oprette et system hvor udviklere kan tage et sæt beans fra et bibliotek og strikke dem sammen til en applikation

  5. Software Komponenter “A software component is a unit of composition with contractually specified interfaces and explicit context dependencies only. A software component can be deployed independently and is subject to composition by third parties.” Szyperski ECOOP96

  6. Composition not Inheritance In OO languages, new objects are created from old using the inheritance mechanism. Pluggable components are connected together by composition rather than inheritance. Most of the flexibility of inheritance can be gained from various compositional tactics.

  7. Features i JavaBeans • Understøtter introspection • så udviklingsværktøjer kan undersøge hvordan en bean virker • Understøtter customization • så brugeren vha. udv.værktøj kan tilpasse udseende og opførsel af en bean • Understøtter events • som en simpel måde til at forbinde bean’en resten af applikationen • Understøtter properties • dels til tilpasning og dels til programmerings formål • Understøtter persistence • så en bean kan tilpasses i et IDE og have sin tilstand gemt, så den kan hentes frem senere

  8. Persistent lager • Formål: • at bruge eksisterende data formater og f.eks bruge OLE eller OpenDoc documents • at være triviel for i den normale situation med en lille bean (ved at gemme dens interne tilstand) • Solutions • Externalization: giver en Bean med fuld kontrol over det resulterende data layout. • Serialization: giver mulighed for automatisk at gemme og læse den interne tilstand af samling af Java objekter • Alle beans skal understøtte Serialization eller Externalization

  9. Software komponenter • Buttons • Text Fields • List Boxes • Scroll Bars • Dialogs • VBX, OLE

  10. Properties • Attributes. • Kan være read/write, read-only eller write-only. • Flere typer af properties: • Simple • Indexed • Bound • Constrained

  11. Design Pattern rules Constructors En bean har kun default konstruktør Simple Properties public T getN()public void setN ( T value) Boolean Properties public boolean isN()public boolean getN()public void setN(boolean value) Indexed Properties public T getN(int index)public T[] getN()public void setN(int index, T value)public void setN(T[] values)

  12. Properties eksempel public class alden2 extends Canvas { String ourString = “Hello”; public alden2() { setBackground (Color.red); setForeground (Color.blue); } public void setString (String newString) { ourString = newString; } public String getString() { return ourString; } public Dimension getMinimunSize() { return new Dimension (50, 50); } }

  13. Bound Properties • Genererer notification når en property ændres. public class propertDemo extends Canvas { String ourString = “Hello”; private PropertyChangeSupport changes = new PropertyChangeSupport(this); ... public void setString (String newString) { String oldString = ourString; ourString = newString; changes.firePropertyChange(“string”, oldString, newString); } ...

  14. Bound Properties... ... public void addPropertyChangeListener (PropertyChangeListener l) { changes.addPropertyChangeListener (l); } public void removePropertyChangeListener (PropertyChangeListener l) { changes.removePropertyChangeListener(l); } }

  15. Constrained Property • Konceptet er en vagt, der kan nedlægge veto • abonner på ‘VetoableChange’ • kan nedlægge veto til en property ændring ved at kaste en PropertyVetoException. public class JellyBean extends Canvas { private PropertyChangeSupport changes = new PropertyChangeSupport (this); private VetoableChangeSupportvetos = new VetoableChangeSupport (this); ... public void setColor (intnewColor) throws PropertyVetoException { intoldColor = currentColor; vetos.fireVetoableChange(“setColor”, newInteger(oldColor), newInteger(newColor)); currentColor = newColor;

  16. Constrained Property... changes.firePropertyChange(“setColor”, new Integer(oldColor) ,new Integer(newColor)); } public void addVetoableChangeListener (VetoableChangeListener l) { vetos.addVetoableChangeListener(l); } public void removeVetoableChangeListener(VetoableChangeListener l) { vetos.removeVetoableChangeListener(l); } }

  17. Bean Events • Definer en ny Event klasse som arver fra EventObject. XEvent • Definer et nyt interface som arver fra EventListener og som listeners skal implementere. XEventListener • Det skal være muligt at registrere og afregistrerelistenersf.eksaddXListener(), removeXListener(). • Observable skal have kode til at genere event’et og sende det til alle registrerede listeners. fireXEvent() • Listeneren skal implementere interfacet for at modtage event.changeX() • Listeneren skal registrere sig for at kunne modtage eventet.

  18. Events public class eventSource extends GenericEventGenerator { ... private Vector myListeners = new Vector(); ... public synchronized void addMyEventListeners ( MyEventListener l) { myListeners.addElement(l); } public synchronized void removeMyEventListeners ( MyEventListener l) { myListeners.removeElement(l); } ...

  19. Events... private void notifyEvents() { Vector l; synchronized (this) { l = (Vector) myListener.clone(); } for (int i = 0; i < l.size(); i++) { MyEventListener mel = (MyEventListener) l.elementAt(i); mel.handleThisEvent (this); } }

  20. Persistens • Giver IDE’et mulighed for at genskabe en beans tilstand public class Button implements java.io.Serializable { ... } • Attributter kan bypass’e serialisation ved bruge keywords transient eller static.

  21. Persistens... • writeObject og readObject private void writeObject (java.io.ObjectOutputStream s) throws java.io.IOException{} private void readObject (java.io.ObjectInputStream s) throws java.io.IOException, java.lang.ClassNotFoundException {} • Giver mulighed for tilpasning af objekter. • Udseende og opførsel kan gemmes og genskabes • Gem ikke referencer til andre beans

  22. Introspection • En mekanisme der giver IDE’et mulighed for at analysere en bean • To måder til analysere en bean: • low-level reflection APIs. F.eks Class.getSuperclass() • eksplicit information fra udbyder • Eksplicit information gives via BeanInfo klassen <classname>BeanInfo.

  23. BDK Example package acme.beans; import java.awt.*; import java.io.Serializable; public class Acme04Bean extends Canvas implements Serializable { public Acme04Bean() { resize(60,40); this.label="Bean"; setFont(new Font("Dialog", Font.PLAIN, 12)); }

  24. Tilpasning af bean • Lav din egen <classname>BeanInfoklasse som arver fra SimpleBeanInfo. • Lav evt. din egen <classname>Editor klasse som arver fra PropertyEditorSupport for lave en tilpasset property editor.

  25. Customization ...<classname>BeanInfo package sun.beanbox.beans; import java.beans.*; public class NervousText07BeanInfo extends SimpleBeanInfo { private final static Class beanClass = NervousText07.class; public BeanDescriptor getBeanDescriptor() { BeanDescriptor bd = new BeanDescriptor(beanClass); bd.setDisplayName("Uneasy Text 07"); return bd; }

  26. Customization... public PropertyDescriptor[] getPropertyDescriptors() { try { PropertyDescriptor textPD = new PropertyDescriptor("text", beanClass); PropertyDescriptor rv[] = {textPD}; textPD.setPropertyEditorClass(NervousText07TextPropertyEditor.class); return rv; } catch (IntrospectionException e) { throw new Error(e.toString()); } } }

  27. Customization...<classname>Editor package sun.beanbox.beans; import java.beans.*; public class NervousText07TextPropertyEditor extends PropertyEditorSupport { public String[] getTags() { String values[] = { "Nervous Text", "Anxious Text", "Funny Text", "Wobbly Text"}; return values; } }

  28. BDK Output

More Related