Principles of Software Construction: Objects, Design and ...

20
toad Fall 2013 © 2012-13 C Garrod, J Aldrich, and W Scherlis School of Computer Science Principles of Software Construction: Objects, Design and Concurrency Mutability and Java Potpourri Jonathan Aldrich Charlie Garrod 15-214

Transcript of Principles of Software Construction: Objects, Design and ...

Page 1: Principles of Software Construction: Objects, Design and ...

 toad    

Fall  2013  

© 2012-13 C Garrod, J Aldrich, and W Scherlis

School of Computer Science

Principles of Software Construction: Objects, Design and Concurrency Mutability and Java Potpourri

Jonathan Aldrich Charlie Garrod

15-214

Page 2: Principles of Software Construction: Objects, Design and ...

2 15-­‐214    Garrod  

Administrivia

• Charlie's office hours moved to Sunday this week only: § Sunday 12:00 to 2:00 p.m., Wean 5101

Page 3: Principles of Software Construction: Objects, Design and ...

3 15-­‐214    Garrod  

Key concepts from Tuesday

Page 4: Principles of Software Construction: Objects, Design and ...

4 15-­‐214    Garrod  

Key concepts from Tuesday

• java.lang.Object behavioral contracts § Challenges of inheritance

• Java Exceptions

Page 5: Principles of Software Construction: Objects, Design and ...

5 15-­‐214    Garrod  

Key concepts for today

• Mutability

• Java potpourri §  The static keyword §  Inner classes § Scope and variable shadowing §  Java Generics

Page 6: Principles of Software Construction: Objects, Design and ...

6 15-­‐214    Garrod  

One possible String implementation public  class  String  {          private  char  value[];          …          public  void  concat(String  s)  {                  char  newValue[]  =  new  char[value.length  +  s.value.length];                  for  (int  i  =  0;  i  <  value.length;  ++i)  {                          newValue[i]  =  value[i];                  }                  for  (int  i  =  0;  i  <  s.value.length;  ++i)  {                          newValue[value.length+i]  =  s.value[i];                  }                  value  =  newValue;          }          public  void  replace(char  old,  char  new)  {                  for  (int  i  =  0;  i  <  value.length;  ++i)  {                          if  (value[i]  ==  old)  {                                  value[i]  =  new;                          }                  }          }  }  

Page 7: Principles of Software Construction: Objects, Design and ...

7 15-­‐214    Garrod  

Another possible String implementation public  class  String  {          private  final  char  value[];          …          public  String  concat(String  s)  {                  char  newValue[]  =  new  char[value.length  +  s.value.length];                  for  (int  i  =  0;  i  <  value.length;  ++i)  {                          newValue[i]  =  value[i];                  }                  for  (int  i  =  0;  i  <  s.value.length;  ++i)  {                          newValue[value.length+i]  =  s.value[i];                  }                  return  new  String(newValue);          }          public  String  replace(char  old,  char  new)  {                  char  newValue[]  =  new  char[value.length];                  for  (int  i  =  0;  i  <  value.length;  ++i)  {                          newValue[i]  =  value[i];                          if  (value[i]  ==  old)  {                                  newValue[i]  =  new;                          }                    }                  return  new  String(newValue);          }  }  

Page 8: Principles of Software Construction: Objects, Design and ...

8 15-­‐214    Garrod  

Mutability and immutability

• Data is mutable if it can change over time. Otherwise it is immutable. § Variables declared as final are enforced to be immutable

• …but data they reference can still be mutable

final  List<Integer>  vals  =  new  ArrayList<Integer>();    vals.add(42);    //  OK  even  though  it  changes  the  list                                  //  Not  OK  because  it  changes  vals:  vals  =  new  ArrayList<Integer>();  

Page 9: Principles of Software Construction: Objects, Design and ...

9 15-­‐214    Garrod  

Advantages: Mutability vs. Immutability

Page 10: Principles of Software Construction: Objects, Design and ...

10 15-­‐214    Garrod  

Immutable advice

• Make your classes and fields immutable if possible § Make all data private and final  § Guarantee exclusive access to your data

• Defensively copy your data • Don't leak your data

§ Don't provide public mutator methods § Make your classes final  

Page 11: Principles of Software Construction: Objects, Design and ...

11 15-­‐214    Garrod  

Key concepts for today

• Mutability

• Java potpourri §  The static keyword §  Inner classes § Scope and variable shadowing §  Java Generics

Page 12: Principles of Software Construction: Objects, Design and ...

12 15-­‐214    Garrod  

Static data and methods

• A property is static if it is associated with the class (as opposed to being associated with an instance of the class) § A.k.a. class fields and methods

• Examples § A simple Counter example §  The main method – why is this static? §  The java.lang.String  valueOf methods

Page 13: Principles of Software Construction: Objects, Design and ...

13 15-­‐214    Garrod  

Inner classes in Java

• Classes can be defined inside other classes, or even inside class methods §  e.g., A LinkedList.Node class accessible only from within a LinkedList class: ! public class LinkedList {!! private static class Node {!

public int val;! public Node next;! public Node(int v, Node n) {! val = v; ! next = n;! }! }! Node head;! // …! }!

Page 14: Principles of Software Construction: Objects, Design and ...

14 15-­‐214    Garrod  

Variable shadowing in Java

• Variable shadowing: when a name within some program inner scope matches a name in some outer scope §  e.g., !public class Dog {!

! ! String name;! public Dog (String name) {! this.name = name;! ! }! }!

• Java has class variables, instance variables, and local variables §  Local variables may shadow instance or class variables § A subclass’s variables may shadow superclass variables

Page 15: Principles of Software Construction: Objects, Design and ...

15 15-­‐214    Garrod  

Inner scopes in Java

• Curly braces define a new scope §  e.g., public void printAsInt(String s) {!

! try {! ! ! Integer x = Integer.valueOf(s);!! !} catch (NumberFormatException e) {!! ! // we ignore the exception!! !}!! !System.out.println(x); // x is undefined!! }! ! ! ! !// in this scope!! !

Page 16: Principles of Software Construction: Objects, Design and ...

16 15-­‐214    Garrod  

Recall the Java Collection API (excerpt)

Collection

List Set AbstractCollection

AbstractList

LinkedList

Vector

HashSet

AbstractSequentialList

AbstractSet

Cloneable

ArrayList

interfaces

Page 17: Principles of Software Construction: Objects, Design and ...

17 15-­‐214    Garrod  

Consider the java.util.Stack!

public class Stack {!

public void push(Object obj) { … }!

public Object pop() { …}!

}

• Some possible client code?: Stack stack = new Stack();!String s = “Hello!”;!stack.push(s);!String t = stack.pop();!

Page 18: Principles of Software Construction: Objects, Design and ...

18 15-­‐214    Garrod  

Consider the java.util.Stack!

public class Stack {!

public void push(Object obj) { … }!

public Object pop() { …}!

}

• Some possible client code: Stack stack = new Stack();!String s = “Hello!”;!stack.push(s);!String t = (String) stack.pop();!

To fix the type error

Page 19: Principles of Software Construction: Objects, Design and ...

19 15-­‐214    Garrod  

Type polymorphism via Java Generics

• The java.util.Stack instead § A stack of some type T: public class Stack<T> {! public void push(T obj) { … }! public T pop() { … }! }!

• Improves typechecking, simplifies(?) client code: Stack<String> stack = new Stack<String>();!String s = “Hello!”;!stack.push(s);!String t = stack.pop();!!

Page 20: Principles of Software Construction: Objects, Design and ...

20 15-­‐214    Garrod  

Next week: design and testing