























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Object oriented programming 2 Material Type: Notes; Class: OBJECT-ORIENTED PROG II; Subject: Computer Science; University: University of Maryland; Term: Unknown 1989;
Typology: Study notes
1 / 31
This page cannot be seen from the preview
Don't miss anything!
























Effective Java
By Joshua Bloch (Sun)
Duplicate Object Creation 2.
Defensive Copying 3.
Immutable Classes
Object Duplication Example
public class Person {
private final Date birthDate; public Person(Date birthDate){
this.birthDate = birthDate; } // UNNECESSARY OBJECT CREATION public boolean bornBefore2000(){
Calendar gmtCal = Calendar.getInstance(
TimeZone.getTimeZone("GMT")); gmtCal.set(2000,Calendar.JANUARY,1,0,0,0); Date MILLENIUM = gmtCal.getTime(); return birthDate.before(MILLENIUM); } }
Object Duplication Example (cont.)
public class Person {
… // STATIC INITIALIZATION CREATES OBJECT ONCE private static final Date MILLENIUM; static {
Calendar gmtCal = Calendar.getInstance(
TimeZone.getTimeZone("GMT")); gmtCal.set(2000,Calendar.JANUARY,1,0,0,0); Date MILLENIUM = gmtCal.getTime(); } public boolean bornBefore2000(){ // FASTER!
return birthDate.before(MILLENIUM); } }
2) Defensive Copying
Immune to buffer overruns, wild pointers, etc… Unlike C, C++
Correctness doesn’t depend on other modules Even in safe language, requires effort
Defensive Programming
May actually be true More likely – honest mistakes
The Problem – Date is Mutable
// ATTACK INTERNALS OF PERSONDate today = new Date();Person p = new Person(today);today.setYear(78);
The Solution – Defensive Copying
public class Person { private final Date birthDate;
// REPAIRED CONSTRUCTOR // DEFENSIVELY COPIES PARAMETERS public Person(Date birthDate){
this.birthDate =
new Date(birthDate.getTime());
} // RETURNS birthDate public Date bday() { return birthDate; } }
Another Important Detail
Necessary because Date class is nonfinal
Records reference to each instance in list Provides attacker with access to instance list
Another Important Detail (cont.)
public class MaliciousDate extends Date {
public long getTime(){
sendToAttacker(this); return super.getTime(); } public int compareTo(Object o){
sendToAttacker(o); return super.compareTo(o);
}
MaliciousDate
myBad = new MaliciousDate( );
Person p = new Person( mybad );
More Defensive Copying
// REPAIRED ACCESSOR DEFENSIVELY COPY FIELDSpublic class Person {
// RETURNS CLONE (COPY) OF birthDatepublic Date bday() {
return (Date) birthDate.clone( ); } }
Defensive Copying Summary
Constructors Static factories Pseudo-constructors Mutators
Accessors
Eliminates the need for defensive copying
How to Write an Immutable Class
Immutable Fval Class Example
public final class Fval
private final float f; public Fval(float f) {
this.f = f; } // ACCESSORS WITHOUT CORRESPONDING MUTATORS public float value( ) { return f; } // ALL OPERATIONS RETURN NEW Fval public Fval
add(Fval x) {
return new Fval(f + x.f); } // SUBTRACT, MULTIPLY, ETC. SIMILAR TO ADD