Presentation is loading. Please wait.

Presentation is loading. Please wait.

SWE 316: Software Design and Architecture Objectives Lecture # 19 Java Beans SWE 316: Software Design and Architecture  Understand  what JavaBeans (“Beans”)

Similar presentations


Presentation on theme: "SWE 316: Software Design and Architecture Objectives Lecture # 19 Java Beans SWE 316: Software Design and Architecture  Understand  what JavaBeans (“Beans”)"— Presentation transcript:

1 SWE 316: Software Design and Architecture Objectives Lecture # 19 Java Beans SWE 316: Software Design and Architecture  Understand  what JavaBeans (“Beans”) are?  the life-cycle of a Bean  Bean containers  Be able to  create JavaBeans  connect Beans in BeanBox  create applications that use Beans Ch 11 Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

2 SWE 316: Software Design and Architecture Java Beans Design Goals 1  Create a component technology within Java  capitalize on Java portability  Include GUI components  but not limited to GUI (e.g. server bean)  Compete with other visual programming and component systems  (which are often specific to an O.S.)  usually Windows  require installation of some kind Intro Creating Bean ClassesMultiple-Class BeansDeploying BeansConnecting BeansBeans in JSP 2/29

3 SWE 316: Software Design and Architecture Beans Design Goals 2  “Light weight” for Internet applications  Secure  use Java security model  Easy & efficient to distribute  Provide mechanism which enables development environment (“container”) to determine methods, properties & events Facilitate the easy reuse of Java code. KEY CONCEPT Design Goal: Reusability Intro Creating Bean ClassesMultiple-Class BeansDeploying BeansConnecting BeansBeans in JSP 3/29

4 SWE 316: Software Design and Architecture Phase 1. Create Bean Classes Source subject to rules Phase 3. Create Bean Instance Instantiate object(s), usually in a Bean environment (container) Phase 4a. Combine Beans in Bean Container to Make Application Combine with other Beans to produce application Bean Phases Phase 2. Create Bean from Multiple Bean Classes Combine Bean classes to make new Beans; create manifest; compile Phase 4b. Deploy Bean and Use in Applications Place application, Beans and required software on target platform - or - Design / implementation time. Instance creation time. Assembly time. Deployment time. -------- Intro Creating Bean ClassesMultiple-Class BeansDeploying BeansConnecting BeansBeans in JSP 4/29

5 SWE 316: Software Design and Architecture Amenities Afforded by Bean Environments  Detection of the Bean’s properties Read only – or- Writeable  Detection of listeners supported So events on the Bean can be handled  Ability to easily create instances and display an image if an awt or swing object Set property values visually  Ability to store instances Intro Creating Bean ClassesMultiple-Class BeansDeploying BeansConnecting BeansBeans in JSP 5/29

6 SWE 316: Software Design and Architecture Required Bean Rules 1 of 2  Java source consists of Java classes  containing null constructor … MyClass() { … }  implementing Serializable interface  obeying standards shown below for … … accessor methods … Listener registration … Event classes Intro Creating Bean ClassesMultiple-Class BeansDeploying BeansConnecting BeansBeans in JSP 6/29

7 SWE 316: Software Design and Architecture Required Bean Rules 2 of 2  To have property myProp, include methods: getMyProp(){ … } // to access myP rop void setMyProp( p ) // to change  For boolean property: boolean isMyProp()  Name for event classes to be XXXEvent  extends Event  Listeners must implement java.util.EventListener  Name must end in Listener as in XXXListener  added with public void addXXXListener(...)  removed with public void removeXXXListener(...) Intro Creating Bean ClassesMultiple-Class BeansDeploying BeansConnecting BeansBeans in JSP 7/29

8 SWE 316: Software Design and Architecture Phase 1: creating bean classes (cont...)  Compile the bean: javac Bean0.java  Create manifest (text file): specifies the name of the class file comprising the bean and indicates that they are indeed JavaBeans (other required files can also be included).  The manifest file becomes part of the JAR file Name: Bean0.class Java-Bean: True 11.3 Intro Creating Bean Classes Multiple-Class BeansDeploying BeansConnecting BeansBeans in JSP 8/29

9 SWE 316: Software Design and Architecture Bean0 source code public class Bean0 implements java.io.Serializable // required for Beans { private int myInt = 0; // ("myInt" is not necessarily a property yet!) public Bean0() // Null constructor presence required for all Beans { } public int getIntgr() // Introduces property "intgr" and makes gettable {return myInt; } public void setIntgr( int anInteger ) // Makes property "intgr" settable {myInt = anInteger; } Intro Creating Bean Classes Multiple-Class BeansDeploying BeansConnecting BeansBeans in JSP 9/29

10 SWE 316: Software Design and Architecture JAR’ing a Bean jar cfm Bean0.jar manifest.txt Bean0.class Creating a JAR file First argument is name of the new JAR file Second argument is name of manifest file List all.class files to be included Intro Creating Bean Classes Multiple-Class BeansDeploying BeansConnecting BeansBeans in JSP 10/29

11 SWE 316: Software Design and Architecture BeanBox Environment Intro Creating Bean Classes Multiple-Class BeansDeploying BeansConnecting BeansBeans in JSP 11/29

12 SWE 316: Software Design and Architecture Adding a Bean to the BeanBox Intro Creating Bean Classes Multiple-Class BeansDeploying BeansConnecting BeansBeans in JSP 12/29

13 SWE 316: Software Design and Architecture Bean1 Intro Creating Bean Classes Multiple-Class BeansDeploying BeansConnecting BeansBeans in JSP 13/29

14 SWE 316: Software Design and Architecture Bean1 source code import java.awt.*; public class Bean1 extends Canvas implements java.io.Serializable { private Color color = Color.red; // color of interior rectangle // Set background (color) and size // properties public Bean1() {setBackground( Color.green ); setSize( 80,40 ); } // Establish property "color" public Color getColor() {return color; } public void setColor( Color aColor ) {color = aColor; } // Override paint: rectangle and // message within this Canvas object // Called initially and when developer // changes a property in BeanBox public void paint ( Graphics g ) { // Draw rectangle in "color" g.setColor( color ); // Starting from top left within // this: 20 pixels across, 5 down // draw a filled rectangle 20 // across and 30 down g.fillRect( 30,5,20,30 ); // Write "HELLO WORLD" in the // foreground color g.setColor( getForeground() ); g.drawString( "HELLO WORLD", 5, 20 ); } Intro Creating Bean Classes Multiple-Class BeansDeploying BeansConnecting BeansBeans in JSP 14/29

15 SWE 316: Software Design and Architecture Setting Color Intro Creating Bean Classes Multiple-Class BeansDeploying BeansConnecting BeansBeans in JSP 15/29

16 SWE 316: Software Design and Architecture Phase 2: creating multiple-class beans  initial Form of Properties Panel  To create a bean consisting of more than one class:  Create these classes in conformance with Bean rules  Declare them in the manifest file  Jar them all  E.g.: jar cfm MyBean.jar manifestListingAll.txt MyBean1.class MyBean2.class … MyBeanN.class 11.4 IntroCreating Bean Classes Multiple-Class Beans Deploying BeansConnecting BeansBeans in JSP 16/29

17 SWE 316: Software Design and Architecture Phase 3: creating bean instance  BeanBox helps the user to create instances of Bean classes by allowing the user to set some properties visually. (green in color) 11.5 IntroCreating Bean Classes Multiple-Class Beans Deploying BeansConnecting BeansBeans in JSP 17/29

18 SWE 316: Software Design and Architecture Phase 4: combining and deploying beans  Phase 4a: combining Beans in Bean environment  Beginning to Use ChairMaker IntroCreating Bean ClassesMultiple-Class Beans Deploying Beans Connecting BeansBeans in JSP 18/29

19 SWE 316: Software Design and Architecture Phase 4: combining and deploying beans  Phase 4a: combining Beans in Bean environment  Setting ChairMaker to Add a Chair Leg IntroCreating Bean ClassesMultiple-Class Beans Deploying Beans Connecting BeansBeans in JSP 19/29

20 SWE 316: Software Design and Architecture Phase 4: combining and deploying beans  Phase 4a: combining Beans in Bean environment,  An Output Of ChairMaker Bean IntroCreating Bean ClassesMultiple-Class Beans Deploying Beans Connecting BeansBeans in JSP 20/29

21 SWE 316: Software Design and Architecture Phase 4: combining and deploying beans  Phase 4a: combining Beans in Bean environment  Output Of ChairMaker Bean From Button Action IntroCreating Bean ClassesMultiple-Class Beans Deploying Beans Connecting BeansBeans in JSP 21/29

22 SWE 316: Software Design and Architecture Phase 4: combining and deploying beans  Phase 4b: using Beans in applications: Output Object bean1 = Beans.instantiate(null, “Bean1”); Bean1 bean2 = (Bean1)objectInputStream.readObject(); We want to associate Beans even when there is no external event such as a mouse click. KEY CONCEPT Reusability IntroCreating Bean ClassesMultiple-Class Beans Deploying Beans Connecting BeansBeans in JSP 22/29

23 SWE 316: Software Design and Architecture Connecting Beans via property changes: “bound” variables Property Change Event Demonstration 11.7 IntroCreating Bean ClassesMultiple-Class BeansDeploying Beans Connecting Beans Beans in JSP 23/29

24 SWE 316: Software Design and Architecture Connecting Beans via property changes: “bound” variables  Property Change Event Demonstration (cont...) IntroCreating Bean ClassesMultiple-Class BeansDeploying Beans Connecting Beans Beans in JSP 24/29

25 SWE 316: Software Design and Architecture Connecting beans via property changes: “bound” variables  bound property demonstration -- causes a Beans to react when a property in another Bean changes value. KEY CONCEPT Bound Properties IntroCreating Bean ClassesMultiple-Class BeansDeploying Beans Connecting Beans Beans in JSP 25/29

26 SWE 316: Software Design and Architecture Embedding Beans in JSP <jsp:useBean id="object name" 1 scope="page|request|session|application" 2 class="fully qualified classname" 3  1 Bean instance name as in MyClass myName = ….  2 // Choose one; when instance is destroyed; optional; default is page  3 // e.g., a.b.MyClass 11.8 IntroCreating Bean ClassesMultiple-Class BeansDeploying BeansConnecting Beans Beans in JSP 26/29

27 SWE 316: Software Design and Architecture Scope of a Bean in a JSP  page - new object created and destroyed for every page view.  request - the newly created object created and bound to the request object.  session - the newly created object bound to the session object. -- every visitor coming to the site will have a separate session for it, so you will not have to create a new object every time for it -- can retrieve that object later again from the session object when wanted  application - object will stay as long as the application remains loaded. E.g., you want to count page views or daily sessions for your site. Source: http://stardeveloper.com:8080/articles/072001-1.shtml IntroCreating Bean ClassesMultiple-Class BeansDeploying BeansConnecting Beans Beans in JSP 27/29

28 SWE 316: Software Design and Architecture Setting and Getting a Bean Property in a JSP: Introduction  <jsp:setProperty  name=“account17"  property=“bal"  value=“3211“  />  <jsp:getProperty  name=“account17"  property=“bal"  /> IntroCreating Bean ClassesMultiple-Class BeansDeploying BeansConnecting Beans Beans in JSP 28/29

29 SWE 316: Software Design and Architecture Summary of This Chapter  A Java Bean is a compiled collection of Java classes and required files  JAR’d to reduce to a single file  Beans are used at various phases, often in a Bean container  Creating from scratch  Creating instances of  Connecting  Deploying as part of an application IntroCreating Bean ClassesMultiple-Class BeansDeploying BeansConnecting BeansBeans in JSP 29/29


Download ppt "SWE 316: Software Design and Architecture Objectives Lecture # 19 Java Beans SWE 316: Software Design and Architecture  Understand  what JavaBeans (“Beans”)"

Similar presentations


Ads by Google