chapter 11 javabeans

35
Chapter 11 JavaBeans

Upload: naoko

Post on 22-Jan-2016

67 views

Category:

Documents


5 download

DESCRIPTION

Chapter 11 JavaBeans. Process Phases Discussed in This Chapter. Requirements Analysis. Design. Framework. Architecture. Detailed Design. Implementation. Key:. = main emphasis. = secondary emphasis. x. x. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Chapter 11 JavaBeans

Chapter 11JavaBeans

Page 2: Chapter 11 JavaBeans

Process Phases Discussed in This ChapterRequirementsAnalysis

Design

Implementation

ArchitectureFramework Detailed Design

xKey: = secondary emphasisx = main emphasis

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 3: Chapter 11 JavaBeans

Learning Goals for This Chapter

… what JavaBeans (“Beans”) are

… the life-cycle of a Bean

… Bean containers

… create JavaBeans

… connect Beans in BeanBox

… create applications that use Beans

Be able to …

Understand …

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 4: Chapter 11 JavaBeans

Java Beans Design Goals 1

Create a component technology within Javao capitalize on Java portability

Include GUI componentso but not limited to GUI (e.g. server bean)

Compete with other visual programming and

component systemso (which are often specific to an O.S.)

o usually Windows

o require installation of some kind

Page 5: Chapter 11 JavaBeans

Beans Design Goals 2

“Light weight” for Internet applications

Secure

o use Java security model

Easy & efficient to distribute

Provide mechanism which enables development

environment (“container”) to determine methods,

properties & events

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 6: Chapter 11 JavaBeans

Design Goal At Work: Reusability

Facilitate the easy reuse of Java code.

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 7: Chapter 11 JavaBeans

Output Of Chair Maker Estimator

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 8: Chapter 11 JavaBeans

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.

--------

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 9: Chapter 11 JavaBeans

Key Concept: Ways to use Beans

-- within environments; connected to other Beans; within applications.

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 10: Chapter 11 JavaBeans

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

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 11: Chapter 11 JavaBeans

Required Bean Rules 1 of 2

Java source consists of Java classes

-containing null constructor

… MyClass() { … }

- implementing Serializable interface

- obeying standards shown below for …

o … accessor methods

o … Listener registration

o … Event classes

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 12: Chapter 11 JavaBeans

To have property myProp, include methods:

<type> getMyProp(){ … } // to access myProp

void setMyProp( <type> p ) // to change

For boolean property:

boolean isMyProp()

Name for event classes to be XXXEvent

o extends Event

Listeners must implement java.util.EventListenero Name must end in Listener as in XXXListener

o added with public void addXXXListener(...)

o removed with public void removeXXXListener(...)

Required Bean Rules

2 of 2

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 13: Chapter 11 JavaBeans

List all .class files to be included

JAR’ing a Bean

jar cfm Bean0.jar manifest.txt Bean0.class

Second argument is name of manifest file

First argument is name of the new JAR file

Creating a JAR file

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 14: Chapter 11 JavaBeans

BeanBox Environment

Page 15: Chapter 11 JavaBeans

Adding a Bean to the BeanBox

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 16: Chapter 11 JavaBeans

Bean1

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 17: Chapter 11 JavaBeans

Setting Color

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 18: Chapter 11 JavaBeans

Design Goal At Work: Reusability

Be able to use Chairmaker Bean alone. Avoid having it refer to any other non-API class.

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 19: Chapter 11 JavaBeans

Initial Form of Properties Panel

(green in color)

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 20: Chapter 11 JavaBeans

Design Goal At Work: Reusability

Utilize Chairmaker with TicTock events. This avoids compromising either Bean.

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 21: Chapter 11 JavaBeans

Beginning to Use ChairMaker

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 22: Chapter 11 JavaBeans

Setting ChairMaker to Add a Chair Leg

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 23: Chapter 11 JavaBeans

An Output Of ChairMaker Bean

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 24: Chapter 11 JavaBeans

Output Of ChairMaker Bean From Button Action

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 25: Chapter 11 JavaBeans

Using a Bean in an Application: Output

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 26: Chapter 11 JavaBeans

Design Goal At Work: Reusability

We want to associate Beans even when there is no external event such as a mouse click.

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 27: Chapter 11 JavaBeans

Property Change Event Demonstration

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 28: Chapter 11 JavaBeans

Property Change Event Demonstration #2

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 29: Chapter 11 JavaBeans

Bound Property Demonstration

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 30: Chapter 11 JavaBeans

Key Concept: Bound Properties

-- causes a Beans to react when a property in another Bean changes value.

Page 31: Chapter 11 JavaBeans

<jsp:useBean id="object name" 1

scope="page|request|session|application" 2

class="fully qualified classname" 3 </ jsp:useBean >

1 Bean instance name as in MyClass myName = ….2 // Choose one; when instance is destroyed; optional; default is page3 // e.g., a.b.MyClass

Embedding Beans in JSP

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 32: Chapter 11 JavaBeans

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

Page 33: Chapter 11 JavaBeans

Setting and Getting a Bean Property in a JSP:

Introduction

<jsp:setProperty name=“account17" property=“bal" value=“3211“

/>

<jsp:getProperty name=“account17" property=“bal"

/>

Page 34: Chapter 11 JavaBeans

Summary of This Chapter

A Java Bean is a compiled collection

of Java classes and required files o JAR’d to reduce to a single file

Beans are used at various phases,

often in a Bean containero Creating from scratch

o Creating instances of

o Connecting

o Deploying as part of an applicationAdapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 35: Chapter 11 JavaBeans

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.