Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Java 120 February 2004Sifei HE © 2004 Introduction to Java for CS381, EE4.Web By Sifei HE Department of Computing.

Similar presentations


Presentation on theme: "Introduction to Java 120 February 2004Sifei HE © 2004 Introduction to Java for CS381, EE4.Web By Sifei HE Department of Computing."— Presentation transcript:

1 Introduction to Java 120 February 2004Sifei HE © 2004 Introduction to Java for CS381, EE4.Web By Sifei HE S.He@eim.surrey.ac.uk Department of Computing School of EPS February 2004

2 Introduction to Java 220 February 2004Sifei HE © 2004 Beans Baked haricot beans, typically cooked in tomato sauce and tinned? An edible seed growing in long pods on certain plants. The hard seed of a coffee or cocoa plant.

3 Introduction to Java 320 February 2004Sifei HE © 2004 Java Bean “A Java Bean is a reusable software component that can be manipulated visually in a builder tool” Sun Microsystems Sun’s tutorial on Java Beans http://java.sun.com/docs/books/tutorial/javabeans Package: java.beans

4 Introduction to Java 420 February 2004Sifei HE © 2004 Components Components are self-contained, reusable software units that can be visually composed into composite components, applets, applications, and servlets using visual application builder tools. JavaBean components are known as Beans.

5 Introduction to Java 520 February 2004Sifei HE © 2004 JavaBeans, a component model JavaBeans is Java’s component model The model is made up of an architecture and an API The API makes it possible to write component software in Java The architecture provides the framework (services and rules) that allows components to participate properly

6 Introduction to Java 620 February 2004Sifei HE © 2004 Some Distinctions Features that distinguish a Java Bean from Java objects are: –Support for introspection –Support for customization –Support for events –Support for properties –Support for persistence Bean requires a zero-argument constructor. Bean does not have public member variables. –Access through pre-defined methods

7 Introduction to Java 720 February 2004Sifei HE © 2004 Sources of Builder Tools Bean Builder from Sun: –http://java.sun.com/products/javabeans/beanbuilder/ NetBeans: –http://www.netbeans.org JBuilder: –http://www.borland.com/jbuilder/ Note, you also need to have the Java SDK installed.

8 Introduction to Java 820 February 2004Sifei HE © 2004

9 Introduction to Java 920 February 2004Sifei HE © 2004

10 Introduction to Java 1020 February 2004Sifei HE © 2004

11 Introduction to Java 1120 February 2004Sifei HE © 2004 Bean Books Developing Java Beans Robert Englander O’ReillyRecommended JavaBeans Programming J. O’Neil and H. Schildt McGraw HillGood. Contains many examples. JavaBeans by Example Henri JubinPrentice HallSimpler than the above, but with useful examples NetBeans – the Definitive Guide Boudreau, Glick, Greene, Spurlin, Woehr O’ReillyNot so good on Beans. More about the IDE.

12 Introduction to Java 1220 February 2004Sifei HE © 2004 The Component Model Discovery and Registration Raising and Handling of Events Persistence Visual Presentation Support for Visual Programming

13 Introduction to Java 1320 February 2004Sifei HE © 2004 The Component Model Raising and Handling of Events –Beans (or JavaBeans components) use events to communicate with other Beans –A Bean that wants to receive events (a listener Bean) registers its interest with the Bean that triggers the event (a source Bean)

14 Introduction to Java 1420 February 2004Sifei HE © 2004 The Component Model Persistence –Persistence enables Beans to save and restore their state –JavaBeans uses Java Object Serialization to support persistence –implements java.io.Serializable

15 Introduction to Java 1520 February 2004Sifei HE © 2004 The Component Model Visual Presentation –The Bean is free to choose its own visual presentation (fonts, colours, shape, etc) –Many of these characteristics will be properties of the Bean (some might be persistent too) Note: Not all beans are visual beans. Beans need not to be visual !

16 Introduction to Java 1620 February 2004Sifei HE © 2004 The Component Model Support of Visual Programming –User can select a component from the toolbox and place it into a container –Properties of the component can then be edited to create the desired behaviour

17 Introduction to Java 1720 February 2004Sifei HE © 2004 Properties, Methods & Events A property is a subset of a component’s state. –Allow the user to control the behavior of that component so it operates as desired. Methods can be invoked to execute code in a component. An event is a notification that is generated by a component when there is some change in its state.

18 Introduction to Java 1820 February 2004Sifei HE © 2004 Properties Named attributes that can be read or set by other beans Accessibility of a bean’s properties is only via its get and set methods In the Java Beans model, properties will only be recognised if appropriate get and (or) set methods are defined –not necessarily both

19 Introduction to Java 1920 February 2004Sifei HE © 2004 A Pepper Bean public class Pepper { private String iKind; private int iHeatLevel; public Pepper( ) { } public String getKind( ) { return iKind; } public void setKind(String aKind) { iKind = aKind; }

20 Introduction to Java 2020 February 2004Sifei HE © 2004 Introspection and Properties The process by which builder tools discover a Bean’s features. By following specific naming conventions, the properties of a Bean that are “revealed” to the world can be identified: public class SignalGenerator { private double frequency; // … constructor methods here public double getFrequency( ) { return frequency; } public void setFrequency(double frequency) { this.frequency = frequency;} }

21 Introduction to Java 2120 February 2004Sifei HE © 2004 Conventions for Access Methods Simple Properties: –For a property of type Type and name Name: –public Type getName( ); –public void setName(Type value); Boolean Properties: –public boolean isName( ); –public void setName(boolean value); –public boolean getName(); Indexed Properties: –public Type getName(int index); –public Type[] getName(); –public void setName(int index, Type value); –public void setName(Type values[]);

22 Introduction to Java 2220 February 2004Sifei HE © 2004 Methods A Bean may be implemented by a Java Class That Class contains a number of methods that may be used to access and control the component These are generally all the public methods of the Class that implements the Bean

23 Introduction to Java 2320 February 2004Sifei HE © 2004 Events JavaBeans components interact by generating “Events” Several components may register an interest in an Event that is generated by a specific component Occurrence of the Event triggers methods to be called in all the components that are “listening” for it public void add ( listener); public void remove ( listener);

24 Introduction to Java 2420 February 2004Sifei HE © 2004 Quote Log Graph

25 Introduction to Java 2520 February 2004Sifei HE © 2004 Reacting to Change In general, a change in the value of a property of a bean will trigger some activity Could code this directly into the get and set methods, but this “hard-wires” application specific dependencies into the methods More flexible (improving reusability) if events are generated when the properties are accessed

26 Introduction to Java 2620 February 2004Sifei HE © 2004 e.g. setting Kind public void setKind(String aKind) throws java.lang.Exception { if( aKind == “Strawberry” ) { Exception ex = new Exception(“Not a chili pepper!”); throw ex; } else iKind = aKind; }

27 Introduction to Java 2720 February 2004Sifei HE © 2004 Application Code Pepper pepper = new Pepper( ); try { pepper.setKind(“Strawberry”); } catch (Exception ex) { System.out.println(“Exception - message is: ” + ex.getMessage( ) }; }

28 Introduction to Java 2820 February 2004Sifei HE © 2004 Introspection The process by which builder tools discover a Bean’s features. Beans support introspection in two ways: By adhering to specific rules (design patterns) By adhering to specific rules (design patterns) when naming properties, methods and events when naming properties, methods and events By explicitly providing property, method and By explicitly providing property, method and event info within a Bean Information class event info within a Bean Information class

29 Introduction to Java 2920 February 2004Sifei HE © 2004 Introspection Low-level reflection: –Follow Bean coding style (we have seen) –Analysis of the Bean’s class can then reveal properties and methods Revealing complex properties: –Implement a “BeanInfo” class

30 Introduction to Java 3020 February 2004Sifei HE © 2004 Introspection But the used within a visual development tool, a Bean must expose its: –properties, –methods –events Java reflection mechanism may not be sufficient

31 Introduction to Java 3120 February 2004Sifei HE © 2004 The BeanInfo Interface Sometimes use of the design patterns: –reveals insufficient information –reveals to much information –forces inappropriate design choices Sometimes the information we wish to expose cannot be represented by a design pattern –e.g. choice of a graphic icon to represent the Bean on a toolbar in a visual development tool

32 Introduction to Java 3220 February 2004Sifei HE © 2004 java.beans.BeanInfo MethodDescription getAdditionalBeanInfo( )Returns any additional BeanInfo objects that are relevant getBeanDescriptor( )Returns the Bean descriptor object getDefaultEventIndex( )Returns the default event index getDefaultPropertyIndex( )Returns the default property index getEventSetDescriptors( )Returns the event set descriptors getIcon( )Returns the specified icon for the Bean getMethodDescriptors( )Returns the method descriptors getPropertyDescriptors( )Returns the property descriptors

33 Introduction to Java 3320 February 2004Sifei HE © 2004 Implementing BeanInfo You do not have to provide a detailed implementation If any of the methods in the interface returns null, then low-level reflection will be used to gather all the information that is available If a non-null value is returned, then that is all the information on that aspect that is made available

34 Introduction to Java 3420 February 2004Sifei HE © 2004 Hiding Information public class Pepper implements java.io.Serializable { private String iKind; private int iHeatLevel; public Pepper() {} public String getKind() { return iKind; } public void setKind(String aKind) { iKind = aKind; } public int getHeatLevel() { return iHeatLevel; } public void setHeatLevel(int aHeatLevel) { iHeatLevel = aHeatLevel; } }

35 Introduction to Java 3520 February 2004Sifei HE © 2004 PepperBeanInfo.java import java.beans.*; import java.beans.SimpleBeanInfo; public class PepperBeanInfo extends SimpleBeanInfo { public PropertyDescriptor[] getPropertyDescriptors() { PropertyDescriptor[] pdArray = new PropertyDescriptor[1]; try { pdArray[0] = new PropertyDescriptor("heatLevel", Pepper.class); } catch (IntrospectionException e) { System.out.println("Error creating a PropertyDescriptor"); } return pdArray; }

36 Introduction to Java 3620 February 2004Sifei HE © 2004 Explanation SimpleBeanInfo implements BeanInfo It contains default implementations that return null Inheriting the default implementation tells the caller to use reflection Overriding the default implementation enables the developer to hide information by only providing descriptors for those features they wish to expose

37 Introduction to Java 3720 February 2004Sifei HE © 2004 Reflection vs. Introspection Reflection analyses the Bean itself to discover properties, methods and events Introspection relies (mostly) on the presence of a “meta-object” to provide information about the Bean (an instance of a BeanInfo class) What are the benefits of Introspection? What are the weaknesses?

38 Introduction to Java 3820 February 2004Sifei HE © 2004 Introspector java.beans.Introspector –seeks a BeanInfo for a Bean –if any descriptors in BeanInfo return null, then use reflection –if no BeanInfo, then use reflection The application always receives a BeanInfo object from the Introspector –(unless an exception occurs) The Introspector also “introspects” superclasses

39 Introduction to Java 3920 February 2004Sifei HE © 2004 Customisation Simple properties –Development tool will build property sheets dynamically –User may then edit the properties to customise the Bean For the Advanced User –Create a specific customiser for a Bean –This is kept separate to the Bean Class, as with a BeanInfo Class

40 Introduction to Java 4020 February 2004Sifei HE © 2004 Further Features Visibility –It is not necessary for a Bean to be visible at run-time (e.g. Bean controlling access to a device or data feed) –It is necessary however for a Bean to support the visual builder tool. Even an ‘invisible’ run- time Bean shall be shown on the builder tool

41 Introduction to Java 4120 February 2004Sifei HE © 2004 Further Features Multithreading –Always assume your code will be used in a multithreaded environment –Make sure that your Beans are thread-safe –Multithreading in JavaBeans is no different than multithreading in Java

42 Introduction to Java 4220 February 2004Sifei HE © 2004 Further Features Security –By default assume that your Beans are running in a non-trusted applet –Apply security restrictions such as Allow no access to the local file system Limit socket connections to the host system


Download ppt "Introduction to Java 120 February 2004Sifei HE © 2004 Introduction to Java for CS381, EE4.Web By Sifei HE Department of Computing."

Similar presentations


Ads by Google