You've probably seen it -- application complexity is growing with each new project. For years, object-oriented programming (OOP) has helped developers go beyond the limits of simple field values. OOP is a staple in modern web technologies.
In this introduction to using objects, Mike McGarel, Senior Application Developer from Celina Insurance, explains how using objects within his XPages provided a dynamic user experience, reduced the development time, and eased future maintenance.
Note that the examples will primarily be using Java and JavaScript, but the concepts apply equally to other languages such as C#.
Convert to study guideBETA
Transform any presentation into a summarized study guide, highlighting the most important points and key insights.
1 of 38
Download to read offline
More Related Content
Object(ive) Thinking
1. MWLUG 2017
Moving Collaboration Forward
Object(ive) Thinking
Mike McGarel
Senior Application Developer
Celina Insurance Group
3. MWLUG 2017
Moving Collaboration Forward
About Me
Notes and Web developer since 1999
XPages developer the last several years
Java developer (within XPages) last 2-3
years
IBM Champion 2015 and 2017
Vice President of GRANITE
Help maintain web site
for MWLUG
5. MWLUG 2017
Moving Collaboration Forward
About Objects
Theyve been around a while
Object-oriented programming (OOP) is
everywhere
They normally contain properties
They often contain functions or methods
6. MWLUG 2017
Moving Collaboration Forward
Consider objects when:
You need to use more than a field value
You need to combine data from different
sources
Your data presentation needs
modification
You need enhanced flexibility, e.g.
sorting and grouping
You want to separate code from data
(which should be as often as possible)
7. MWLUG 2017
Moving Collaboration Forward
Separating Code from Data
Model-View-Controller (MVC) pattern
Model: data
View: user interface
Controller: business logic and code
needed for interation with above
8. MWLUG 2017
Moving Collaboration Forward
Object Property
Name-value pair
The value can be another object
Example: Conference speaker
Name
Job title
Session names (array of strings)
or
Sessions (array of session objects)
9. MWLUG 2017
Moving Collaboration Forward
Java Object Properties Example
public class Speaker() {
private String name;
private String jobTitle;
private List<Session> sessionsList;
public Speaker() {
}
}
10. MWLUG 2017
Moving Collaboration Forward
Object Functions or Methods
Getters and setters for property values
Data modification examples
Shopping cart totals
Filling in sequence of dates in a calendar
UI rendering example
Style class based on a property
14. MWLUG 2017
Moving Collaboration Forward
Java Object Building Example
// Used within loop of documents
Speaker speaker = new Speaker();
String n = doc.getItemValueString(Name);
speaker.setName(n);
String t = doc.getItemValueString (JobTitle);
speaker.setJobTitle(t);
// After adding other properties to speaker
this.getAllSpeakers().add(speaker);
15. MWLUG 2017
Moving Collaboration Forward
Client-side JavaScript Objects
Everything is in the browser
Objects surrounded by { }
Object properties are name-value pairs
Object properties separated by commas
Arrays surrounded by [ ]
Array values separated by commas
16. MWLUG 2017
Moving Collaboration Forward
JavaScript Object Example 1
var speakerObj = {
name: Chris Robinson,
jobTitle: VP of Development,
sessionsList: [Java Tips, JSON Tips]
};
17. MWLUG 2017
Moving Collaboration Forward
JavaScript Object Example 2
var allSpeakers = [ {
name: Chris Robinson,
jobTitle: VP of Development,
sessionsList: [Java Tips, JSON Tips]
}, {
name: Alex Wu,
jobTitle: CEO,
sessionsList: [Better Hiring, Less Firing]
} ];
18. MWLUG 2017
Moving Collaboration Forward
JavaScript Object Example 3
function speaker(name, jobTitle, sessionsList)
{
this.name = name;
this.jobTitle = jobTitle;
this.sessionsList = sessionsList;
}
var speaker079 = new speaker(Chris Robinson,
VP of Development,
[Java Tips, JSON Tips]);
19. MWLUG 2017
Moving Collaboration Forward
JS Object in HTML Example
<span id=title></span>
<script>
var t = document.getElementById(title);
t.value = speaker079.jobTitle;
</script>
20. MWLUG 2017
Moving Collaboration Forward
JSON (JavaScript Object Notation)
JSON is text
Objects surrounded by { }
Object properties are name-value pairs
Object properties separated by commas
Arrays surrounded by [ ]
Array values separated by commas
Text is used to send/store/retrieve data
21. MWLUG 2017
Moving Collaboration Forward
JSON to JavaScript Example
var sSpeaker = {
name: Chris Robinson,
jobTitle: VP of Development,
sessionsList: [Java Tips, JSON Tips]
};
var speakerObj = JSON.parse(sSpeaker);
speakerObj.name Chris Robinson
speakerObj.sessionsList[1] JSON Tips
22. MWLUG 2017
Moving Collaboration Forward
JavaScript to JSON Example
var speakerObj = {
name: Chris Robinson,
jobTitle: VP of Development,
sessionsList: [Java Tips, JSON Tips]
};
var sSpeaker = JSON.stringify(speakerObj);
doc.replaceItemValue(SpeakerInfo, sSpeaker);
25. MWLUG 2017
Moving Collaboration Forward
Heres the project . . .
Insurance policy form
Different types of answer fields:
Text box
Checkbox
Radio button
Some questions appear based on the
answers to previous questions
26. MWLUG 2017
Moving Collaboration Forward
Heres the thing . . .
Many, many questions (1100+)
Question/answers in flux
Content editing
Legal review
QA and testing
Need dynamic rendering of answer types
Only displayed questions and answers
should be printed
27. MWLUG 2017
Moving Collaboration Forward
All in all . . .
Hard-coding all the questions
maintenance nightmare
Programmers should be separated from
content
Code should be reusable
28. MWLUG 2017
Moving Collaboration Forward
Sample Application
Insurance form
Policy targets pet stores that focus on a
single type of pet (puppies, kittens, etc.)
Questions stored in Notes database
31. MWLUG 2017
Moving Collaboration Forward
Objects to the Rescue!
Question
Answer
Map of question objects
Question ID as key
Managed bean for convenience
Map of answer objects
Question ID as key
Part of page controller
32. MWLUG 2017
Moving Collaboration Forward
Special Help from a Java Enum
Set of Java constants
Allows easier use of Java switch
statement (similar to Select Case in
LotusScript)
Used here to target a specific object by
its ID
Can have its own properties and methods