SlideShare a Scribd company logo
Java Beans
Shivasubramanian A
What is a Java Bean?
 Ensures that developers can
 Capture the state of a given entity
 Discover the state of a given entity
 Achieved by writing Java classes that follow
the JavaBean specification
 Belongs to the java.desktop module
 Two packages
 java.beans
 java.beans.context
When is a Java class a JavaBean?
 When the class follows the JavaBean spec
 The class should only contain getters and/or
setters.
 The class should not extend any other class or
interface.
 Getters returning boolean values must begin with
‘is’.
When is a Java class a JavaBean?
class Employee {
private Integer age;
public void getAge() { return this.age; }
public void setAge(Integer age) {
this.age = age;
}
}
Java Bean properties
 Any aspect of a Java Bean available via
getter/setter methods is called a “Java bean
property”.
Java Bean properties
class Employee {
private Integer age;
public void getAge() { return this.age; }
public void setAge(Integer age) {
this.age = age;
}
public void isValidAge() {
return age <= 100 && age >= 0;
}
}
Properties – age, validAge
Java Bean properties
Read-only properties – only getter method
Write-only properties – only setter method
Read/write properties – getter & setter
Types of Java Bean properties
Indexed properties
Bound properties
Constrained properties
Indexed properties
Basically arrays
 Getters return array
 Setters take in an array
Must provide getter/setter for each element of
the property
Indexed properties...
Student.java
public class Student {
private String name;
private String[] subjects = new String[] { “Maths”,
“Science”, “Geography”};
public void getSubjects { return this.subjects;}
public void setSubjects(String[] sub) { this.subjects =
sub; }
public void getSubject(int index) { return
this.subjects[index]; }
public void setSubject(int index, String subject) {
this.subjects[index] = subject; }
}
Indexed properties...
Main.java
public class Main {
public static void main(String args[]) {
Student student = new Student();
student.getSubject(2); //returns “Geography”
student.setSubject(2, “Civics”);
student.getSubject(2); //returns “Civics”
}
}
Bound properties
Java bean which has listeners
 Changes to the bean triggers the listeners
You have to write code to:
 Add/remove listeners
 Notify listeners
You can have listeners for
 All properties
 Specific properties
Bound properties -
Add/remove listeners
java.beans.PropertyChangeSupport
 Provides methods to maintain a list of
listeners
 Provides methods to notify listeners of
changes
Bound properties -
Add/remove listeners
Student.java
public class Student {
private String name;
private int[] marks = new int[3];
private int total;
//getters & setters
PropertyChangeSupport pcs = new
PropertyChangeSupport(this);
public void addPropertyListener(PropertyChangeListener pcl)
{
this.pcs.addPropertyChangeListener(pcl);
}
//remove property listener
Bound properties –
Notify listeners
To notify listeners, use methods
 PropertyChangeSupport.firePropert
yChange
 PropertyChangeSupport.fireIndexed
PropertyChange
Bound properties –
Notify listeners
Student.java
public class Student {
private String name;
private int[] marks = new int[3];
private int total;
//add/remove property listener
PropertyChangeSupport pcs = new
PropertyChangeSupport(this);
public void setMarks(int[] marks) {
this.marks = marks;
int oldTotal = total;
this.total = sum(marks);
pcs.firePropertyChange(“total”, oldTotal, this.total);
Bound properties – listener code
Listener classes
 Implements PropertyChangeListener
 Single method,
propertyChange(PropertyChangeEven
t evt)
Bound properties – listener code
StudentRanker.java
public class StudentRanker implements PropertyChangeListener {
private List<Student> studentsRanking = new ArrayList<>();
public void propertyChange(PropertyChangeEvent evt) {
Student source = (Student) evt.getSource();
studentsRanking.add(source);
studentsRanking.sort(studentsTotalComparator);
}
}
Constrained properties
Bound properties that have veto power over
the new value being set
 Throw
java.beans.PropertyVetoException
to veto
You have to write code to:
 Add/remove listeners
 Notify listeners
You can have listeners for
Constrained properties -
Add/remove listeners
java.beans.VetoableChangeSupport
 Provides methods to maintain a list of
listeners
 Provides methods to notify listeners of
changes
Constrained properties -
Add/remove listeners
Student.java
public class Student {
private String name;
private int[] marks = new int[3];
private int total;
//getters & setters
VetoableChangeSupport vcs = new
VetoableChangeSupport(this);
public void
addVetoableChangeListener(VetoableChangeListener pcl) {
this.vcs.addVetoableChangeListener(pcl);
}
//remove vetoable change listener
Constrained properties –
Notify listeners
To notify listeners, use methods
 PropertyChangeSupport.fireVetoabl
eChange
Constrained properties –
Notify listeners
Student.java
public class Student {
private String name;
private int[] marks = new int[3];
private int total;
//add/remove property listener
VetoableChangeSupport vcs = new
VetoableChangeSupport(this);
public void setMarks(int[] marks) {
vcs.fireVetoableChange(“marks”, this.marks, marks);
this.marks = marks;
int oldTotal = total;
this.total = sum(marks);
Constrained properties – listener
code
Listener classes
 Implements VetoableChangeListener
 Single method,
vetoableChange(PropertyChangeEven
t evt)
Constrained properties – listener
code
MarksValidator.java
public class MarksValidator implements VetoableChangeListener {
public void propertyChange(PropertyChangeEvent evt) {
int[] marks = (int[]) evt.getNewValue();
for (int mark : marks) {
if (mark < 0 || mark > 100) {
throw new PropertyVetoException(“Mark is
invalid.”, evt);
}
}
}
}
Bean methods
Any methods that don’t meet the JavaBean
spec are called bean methods
Long term bean persistence
Store a bean as an XML file
Useful for sharing state of beans
java.beans.XMLEncoder will convert the bean
into an XML file
java.beans.XMLDecoder will build the bean
from the XML file
Long term bean persistence
– XMLEncoder
Student.java
public class Student {
private String name;
private String[] subjectsTaken;
//getters & setters
}
Long term bean persistence
– XMLEncoder
StudentEncoder.java
Student student = new Student();
student.setName(“Student1”);
student.setSubjects(new String[] {“Maths”, “Physics”});
XMLEncoder encoder = new XMLEncoder(
new BufferedOutputStream(
new FileOutputStream(“Student.xml”)));
encoder.writeObject(student);
encoder.close();
Long term bean persistence
– XMLEncoder
Student.xml
<object class=”Student”>
<void property=”name”>
<string>Student1</string>
</void>
<void property=”subjectsTaken”>
<array class=”java.lang.String” length=”2”>
<void index=”0”>
<string>Maths</string>
</void>
<void index=”1”>
<string>Science</string>
Long term bean persistence
– XMLDecoder
StudentDecoder.java
XMLDecoder decoder = new XMLDecoder(
new BufferedInputStream(
new FileInputStream(“Student.xml”)));
Student student = (Student) decoder.getObject();

More Related Content

What's hot

Introduction to JPA and Hibernate including examples
Introduction to JPA and Hibernate including examplesIntroduction to JPA and Hibernate including examples
Introduction to JPA and Hibernate including examples
ecosio GmbH
 
Introduction to JPA (JPA version 2.0)
Introduction to JPA (JPA version 2.0)Introduction to JPA (JPA version 2.0)
Introduction to JPA (JPA version 2.0)
ejlp12
 
Introduction to JPA Framework
Introduction to JPA FrameworkIntroduction to JPA Framework
Introduction to JPA Framework
Collaboration Technologies
 
Java Course 14: Beans, Applets, GUI
Java Course 14: Beans, Applets, GUIJava Course 14: Beans, Applets, GUI
Java Course 14: Beans, Applets, GUI
Anton Keks
 
Introduction to JDBC and database access in web applications
Introduction to JDBC and database access in web applicationsIntroduction to JDBC and database access in web applications
Introduction to JDBC and database access in web applications
Fulvio Corno
 
Hibernate inheritance and relational mappings with examples
Hibernate inheritance and relational mappings with examplesHibernate inheritance and relational mappings with examples
Hibernate inheritance and relational mappings with examples
Er. Gaurav Kumar
 
jpa-hibernate-presentation
jpa-hibernate-presentationjpa-hibernate-presentation
jpa-hibernate-presentationJohn Slick
 
hibernate with JPA
hibernate with JPAhibernate with JPA
hibernate with JPA
Mohammad Faizan
 
Hibernate
HibernateHibernate
Hibernate
Prashant Kalkar
 
Spring (1)
Spring (1)Spring (1)
Spring (1)
Aneega
 
13 java beans
13 java beans13 java beans
13 java beanssnopteck
 
Packages and inbuilt classes of java
Packages and inbuilt classes of javaPackages and inbuilt classes of java
Packages and inbuilt classes of java
kamal kotecha
 
Hibernate ppt
Hibernate pptHibernate ppt
Hibernate ppt
Aneega
 

What's hot (20)

Introduction to JPA and Hibernate including examples
Introduction to JPA and Hibernate including examplesIntroduction to JPA and Hibernate including examples
Introduction to JPA and Hibernate including examples
 
Introduction to JPA (JPA version 2.0)
Introduction to JPA (JPA version 2.0)Introduction to JPA (JPA version 2.0)
Introduction to JPA (JPA version 2.0)
 
Introduction to JPA Framework
Introduction to JPA FrameworkIntroduction to JPA Framework
Introduction to JPA Framework
 
Java Course 14: Beans, Applets, GUI
Java Course 14: Beans, Applets, GUIJava Course 14: Beans, Applets, GUI
Java Course 14: Beans, Applets, GUI
 
Deployment
DeploymentDeployment
Deployment
 
Introduction to JDBC and database access in web applications
Introduction to JDBC and database access in web applicationsIntroduction to JDBC and database access in web applications
Introduction to JDBC and database access in web applications
 
JPA and Hibernate
JPA and HibernateJPA and Hibernate
JPA and Hibernate
 
Hibernate inheritance and relational mappings with examples
Hibernate inheritance and relational mappings with examplesHibernate inheritance and relational mappings with examples
Hibernate inheritance and relational mappings with examples
 
jpa-hibernate-presentation
jpa-hibernate-presentationjpa-hibernate-presentation
jpa-hibernate-presentation
 
EJB Clients
EJB ClientsEJB Clients
EJB Clients
 
hibernate with JPA
hibernate with JPAhibernate with JPA
hibernate with JPA
 
Jdbc[1]
Jdbc[1]Jdbc[1]
Jdbc[1]
 
Hibernate
HibernateHibernate
Hibernate
 
Dacj 4 2-c
Dacj 4 2-cDacj 4 2-c
Dacj 4 2-c
 
Spring (1)
Spring (1)Spring (1)
Spring (1)
 
Dacj 4 2-b
Dacj 4 2-bDacj 4 2-b
Dacj 4 2-b
 
Jpa
JpaJpa
Jpa
 
13 java beans
13 java beans13 java beans
13 java beans
 
Packages and inbuilt classes of java
Packages and inbuilt classes of javaPackages and inbuilt classes of java
Packages and inbuilt classes of java
 
Hibernate ppt
Hibernate pptHibernate ppt
Hibernate ppt
 

Similar to Java beans

Object Oriented JavaScript
Object Oriented JavaScriptObject Oriented JavaScript
Object Oriented JavaScript
Donald Sipe
 
Observer design pattern
Observer design patternObserver design pattern
Observer design patternSara Torkey
 
First java-server-faces-tutorial-en
First java-server-faces-tutorial-enFirst java-server-faces-tutorial-en
First java-server-faces-tutorial-entechbed
 
Computer programming 2 -lesson 4
Computer programming 2  -lesson 4Computer programming 2  -lesson 4
Computer programming 2 -lesson 4
MLG College of Learning, Inc
 
Object Oriented Programming in PHP
Object Oriented Programming  in PHPObject Oriented Programming  in PHP
Object Oriented Programming in PHP
wahidullah mudaser
 
Lecture Notes
Lecture NotesLecture Notes
Lecture Notes
Dwight Sabio
 
Effective java-3rd-edition-ch4
Effective java-3rd-edition-ch4Effective java-3rd-edition-ch4
Effective java-3rd-edition-ch4
Matt
 
UNIT-2.pptx CS3391 Inheritance , types, packages and Interfaces
UNIT-2.pptx CS3391 Inheritance , types, packages and InterfacesUNIT-2.pptx CS3391 Inheritance , types, packages and Interfaces
UNIT-2.pptx CS3391 Inheritance , types, packages and Interfaces
SakkaravarthiS1
 
Java session4
Java session4Java session4
Java session4
Jigarthacker
 
Access modifiers
Access modifiersAccess modifiers
Access modifiers
Kasun Ranga Wijeweera
 
Observer pattern
Observer patternObserver pattern
Observer pattern
Somenath Mukhopadhyay
 
Object Oriented Programming using JAVA Notes
Object Oriented Programming using JAVA Notes Object Oriented Programming using JAVA Notes
Object Oriented Programming using JAVA Notes
Uzair Salman
 
14. Defining Classes
14. Defining Classes14. Defining Classes
14. Defining Classes
Intro C# Book
 
Defining classes-and-objects-1.0
Defining classes-and-objects-1.0Defining classes-and-objects-1.0
Defining classes-and-objects-1.0
BG Java EE Course
 
INHERITANCE.pptx
INHERITANCE.pptxINHERITANCE.pptx
INHERITANCE.pptx
AteeqaKokab1
 
Demystifying Object-Oriented Programming #phpbnl18
Demystifying Object-Oriented Programming #phpbnl18Demystifying Object-Oriented Programming #phpbnl18
Demystifying Object-Oriented Programming #phpbnl18
Alena Holligan
 
Computer programming 2 Lesson 6
Computer programming 2  Lesson 6Computer programming 2  Lesson 6
Computer programming 2 Lesson 6
MLG College of Learning, Inc
 
OOP using java (Variable in java)
OOP using java (Variable in java)OOP using java (Variable in java)
OOP using java (Variable in java)
omeed
 

Similar to Java beans (20)

Object Oriented JavaScript
Object Oriented JavaScriptObject Oriented JavaScript
Object Oriented JavaScript
 
Observer design pattern
Observer design patternObserver design pattern
Observer design pattern
 
First java-server-faces-tutorial-en
First java-server-faces-tutorial-enFirst java-server-faces-tutorial-en
First java-server-faces-tutorial-en
 
Computer programming 2 -lesson 4
Computer programming 2  -lesson 4Computer programming 2  -lesson 4
Computer programming 2 -lesson 4
 
Bean Intro
Bean IntroBean Intro
Bean Intro
 
Java beans
Java beansJava beans
Java beans
 
Object Oriented Programming in PHP
Object Oriented Programming  in PHPObject Oriented Programming  in PHP
Object Oriented Programming in PHP
 
Lecture Notes
Lecture NotesLecture Notes
Lecture Notes
 
Effective java-3rd-edition-ch4
Effective java-3rd-edition-ch4Effective java-3rd-edition-ch4
Effective java-3rd-edition-ch4
 
UNIT-2.pptx CS3391 Inheritance , types, packages and Interfaces
UNIT-2.pptx CS3391 Inheritance , types, packages and InterfacesUNIT-2.pptx CS3391 Inheritance , types, packages and Interfaces
UNIT-2.pptx CS3391 Inheritance , types, packages and Interfaces
 
Java session4
Java session4Java session4
Java session4
 
Access modifiers
Access modifiersAccess modifiers
Access modifiers
 
Observer pattern
Observer patternObserver pattern
Observer pattern
 
Object Oriented Programming using JAVA Notes
Object Oriented Programming using JAVA Notes Object Oriented Programming using JAVA Notes
Object Oriented Programming using JAVA Notes
 
14. Defining Classes
14. Defining Classes14. Defining Classes
14. Defining Classes
 
Defining classes-and-objects-1.0
Defining classes-and-objects-1.0Defining classes-and-objects-1.0
Defining classes-and-objects-1.0
 
INHERITANCE.pptx
INHERITANCE.pptxINHERITANCE.pptx
INHERITANCE.pptx
 
Demystifying Object-Oriented Programming #phpbnl18
Demystifying Object-Oriented Programming #phpbnl18Demystifying Object-Oriented Programming #phpbnl18
Demystifying Object-Oriented Programming #phpbnl18
 
Computer programming 2 Lesson 6
Computer programming 2  Lesson 6Computer programming 2  Lesson 6
Computer programming 2 Lesson 6
 
OOP using java (Variable in java)
OOP using java (Variable in java)OOP using java (Variable in java)
OOP using java (Variable in java)
 

Recently uploaded

A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...
sonjaschweigert1
 
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
James Anderson
 
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
Neo4j
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
SOFTTECHHUB
 
Video Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the FutureVideo Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the Future
Alpen-Adria-Universität
 
Elevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object CalisthenicsElevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object Calisthenics
Dorra BARTAGUIZ
 
Microsoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdfMicrosoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdf
Uni Systems S.M.S.A.
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
James Anderson
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
Alan Dix
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
ControlCase
 
Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?
Nexer Digital
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance
 
By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024
Pierluigi Pugliese
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance
 
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
Neo4j
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
91mobiles
 
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
Neo4j
 
Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
Kari Kakkonen
 

Recently uploaded (20)

A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...
 
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
 
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
 
Video Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the FutureVideo Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the Future
 
Elevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object CalisthenicsElevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object Calisthenics
 
Microsoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdfMicrosoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdf
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
 
Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
 
By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
 
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
 
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
 
Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
 

Java beans

  • 2. What is a Java Bean?  Ensures that developers can  Capture the state of a given entity  Discover the state of a given entity  Achieved by writing Java classes that follow the JavaBean specification  Belongs to the java.desktop module  Two packages  java.beans  java.beans.context
  • 3. When is a Java class a JavaBean?  When the class follows the JavaBean spec  The class should only contain getters and/or setters.  The class should not extend any other class or interface.  Getters returning boolean values must begin with ‘is’.
  • 4. When is a Java class a JavaBean? class Employee { private Integer age; public void getAge() { return this.age; } public void setAge(Integer age) { this.age = age; } }
  • 5. Java Bean properties  Any aspect of a Java Bean available via getter/setter methods is called a “Java bean property”.
  • 6. Java Bean properties class Employee { private Integer age; public void getAge() { return this.age; } public void setAge(Integer age) { this.age = age; } public void isValidAge() { return age <= 100 && age >= 0; } } Properties – age, validAge
  • 7. Java Bean properties Read-only properties – only getter method Write-only properties – only setter method Read/write properties – getter & setter
  • 8. Types of Java Bean properties Indexed properties Bound properties Constrained properties
  • 9. Indexed properties Basically arrays  Getters return array  Setters take in an array Must provide getter/setter for each element of the property
  • 10. Indexed properties... Student.java public class Student { private String name; private String[] subjects = new String[] { “Maths”, “Science”, “Geography”}; public void getSubjects { return this.subjects;} public void setSubjects(String[] sub) { this.subjects = sub; } public void getSubject(int index) { return this.subjects[index]; } public void setSubject(int index, String subject) { this.subjects[index] = subject; } }
  • 11. Indexed properties... Main.java public class Main { public static void main(String args[]) { Student student = new Student(); student.getSubject(2); //returns “Geography” student.setSubject(2, “Civics”); student.getSubject(2); //returns “Civics” } }
  • 12. Bound properties Java bean which has listeners  Changes to the bean triggers the listeners You have to write code to:  Add/remove listeners  Notify listeners You can have listeners for  All properties  Specific properties
  • 13. Bound properties - Add/remove listeners java.beans.PropertyChangeSupport  Provides methods to maintain a list of listeners  Provides methods to notify listeners of changes
  • 14. Bound properties - Add/remove listeners Student.java public class Student { private String name; private int[] marks = new int[3]; private int total; //getters & setters PropertyChangeSupport pcs = new PropertyChangeSupport(this); public void addPropertyListener(PropertyChangeListener pcl) { this.pcs.addPropertyChangeListener(pcl); } //remove property listener
  • 15. Bound properties – Notify listeners To notify listeners, use methods  PropertyChangeSupport.firePropert yChange  PropertyChangeSupport.fireIndexed PropertyChange
  • 16. Bound properties – Notify listeners Student.java public class Student { private String name; private int[] marks = new int[3]; private int total; //add/remove property listener PropertyChangeSupport pcs = new PropertyChangeSupport(this); public void setMarks(int[] marks) { this.marks = marks; int oldTotal = total; this.total = sum(marks); pcs.firePropertyChange(“total”, oldTotal, this.total);
  • 17. Bound properties – listener code Listener classes  Implements PropertyChangeListener  Single method, propertyChange(PropertyChangeEven t evt)
  • 18. Bound properties – listener code StudentRanker.java public class StudentRanker implements PropertyChangeListener { private List<Student> studentsRanking = new ArrayList<>(); public void propertyChange(PropertyChangeEvent evt) { Student source = (Student) evt.getSource(); studentsRanking.add(source); studentsRanking.sort(studentsTotalComparator); } }
  • 19. Constrained properties Bound properties that have veto power over the new value being set  Throw java.beans.PropertyVetoException to veto You have to write code to:  Add/remove listeners  Notify listeners You can have listeners for
  • 20. Constrained properties - Add/remove listeners java.beans.VetoableChangeSupport  Provides methods to maintain a list of listeners  Provides methods to notify listeners of changes
  • 21. Constrained properties - Add/remove listeners Student.java public class Student { private String name; private int[] marks = new int[3]; private int total; //getters & setters VetoableChangeSupport vcs = new VetoableChangeSupport(this); public void addVetoableChangeListener(VetoableChangeListener pcl) { this.vcs.addVetoableChangeListener(pcl); } //remove vetoable change listener
  • 22. Constrained properties – Notify listeners To notify listeners, use methods  PropertyChangeSupport.fireVetoabl eChange
  • 23. Constrained properties – Notify listeners Student.java public class Student { private String name; private int[] marks = new int[3]; private int total; //add/remove property listener VetoableChangeSupport vcs = new VetoableChangeSupport(this); public void setMarks(int[] marks) { vcs.fireVetoableChange(“marks”, this.marks, marks); this.marks = marks; int oldTotal = total; this.total = sum(marks);
  • 24. Constrained properties – listener code Listener classes  Implements VetoableChangeListener  Single method, vetoableChange(PropertyChangeEven t evt)
  • 25. Constrained properties – listener code MarksValidator.java public class MarksValidator implements VetoableChangeListener { public void propertyChange(PropertyChangeEvent evt) { int[] marks = (int[]) evt.getNewValue(); for (int mark : marks) { if (mark < 0 || mark > 100) { throw new PropertyVetoException(“Mark is invalid.”, evt); } } } }
  • 26. Bean methods Any methods that don’t meet the JavaBean spec are called bean methods
  • 27. Long term bean persistence Store a bean as an XML file Useful for sharing state of beans java.beans.XMLEncoder will convert the bean into an XML file java.beans.XMLDecoder will build the bean from the XML file
  • 28. Long term bean persistence – XMLEncoder Student.java public class Student { private String name; private String[] subjectsTaken; //getters & setters }
  • 29. Long term bean persistence – XMLEncoder StudentEncoder.java Student student = new Student(); student.setName(“Student1”); student.setSubjects(new String[] {“Maths”, “Physics”}); XMLEncoder encoder = new XMLEncoder( new BufferedOutputStream( new FileOutputStream(“Student.xml”))); encoder.writeObject(student); encoder.close();
  • 30. Long term bean persistence – XMLEncoder Student.xml <object class=”Student”> <void property=”name”> <string>Student1</string> </void> <void property=”subjectsTaken”> <array class=”java.lang.String” length=”2”> <void index=”0”> <string>Maths</string> </void> <void index=”1”> <string>Science</string>
  • 31. Long term bean persistence – XMLDecoder StudentDecoder.java XMLDecoder decoder = new XMLDecoder( new BufferedInputStream( new FileInputStream(“Student.xml”))); Student student = (Student) decoder.getObject();