Mutability SWE 332 Fall 2011 Paul Ammann. SWE 3322 Data Abstraction Operation Categories Creators Create objects of a data abstraction Producers Create.

Slides:



Advertisements
Similar presentations
Identity and Equality Based on material by Michael Ernst, University of Washington.
Advertisements

Singleton vs utility class  at first glance, the singleton pattern does not seem to offer any advantages to using a utility class  i.e., a utility class.
Data Abstraction II SWE 619 Software Construction Last Modified, Spring 2009 Paul Ammann.
Chapter 10 THINKING IN OBJECTS 1 Object Oriented programming Instructor: Dr. Essam H. Houssein.
Inheritance Inheritance Reserved word protected Reserved word super
Effective Java Gruppe Gul Items vi vil uddybe: # 15Minimize Mutability # 23 Don’t use raw types in new code # 47Know and use Libraries # 63Include failure-capture.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Immutable Objects and Classes.
Abstract Data Types II. 2 Sufficient operations Operations on an ADT are sufficient if they meet all the requirements They must be able to create all.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Abstract Data Types II. 2 Sufficient operations Operations on an ADT are sufficient if they meet all the requirements They must be able to create all.
Abstract Data Types II. Sufficient operations Operations on an ADT are sufficient if they meet all the requirements –They must be able to create all the.
CSE 331 Software Design & Implementation Dan Grossman Fall 2014 Data Abstraction: Abstract Data Types (ADTs) (Based on slides by Mike Ernst, David Notkin,
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 12 Object-Oriented Design.
Classes and Class Members Chapter 3. 3 Public Interface Contract between class and its clients to fulfill certain responsibilities The client is an object.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
Cs2220: Engineering Software Class 8: Implementing Data Abstractions Fall 2010 University of Virginia David Evans.
Data Abstraction CS 201j: Engineering Software University of Virginia Computer Science Nathanael Paul
Design Patterns SWE 619 Software Construction Fall 2008.
Effective Java: Creating and Destroying Objects Last Updated: Fall 2012.
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
Best Practices. Contents Bad Practices Good Practices.
Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.
332 Final Review Last updated Fall 2013 Professor Ammann.
Design Patterns Gang Qian Department of Computer Science University of Central Oklahoma.
CSSE501 Object-Oriented Development. Chapter 4: Classes and Methods  Chapters 4 and 5 present two sides of OOP: Chapter 4 discusses the static, compile.
Programming in Java CSCI-2220 Object Oriented Programming.
Sharing Objects  Synchronization  Atomicity  Specifying critical sections  Memory visibility  One thread’s modification seen by the other  Visibility.
 All calls to method toString and earnings are resolved at execution time, based on the type of the object to which currentEmployee refers.  Known as.
Mutability: C++ const usage SWE 332 Fall 2011 Paul Ammann.
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Abstract Data Types – Examples / Summary (Based on slides by Mike Ernst and David Notkin)
CSCI 1100/1202 April 1-3, Program Development The creation of software involves four basic activities: –establishing the requirements –creating.
User Interface Principles in API Design Elliotte Rusty Harold
Design Patterns Software Engineering CS 561. Last Time Introduced design patterns Abstraction-Occurrence General Hierarchy Player-Role.
Design Patterns SE464 Derek Rayside images from NetObjectives.com & Wikipedia.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 11 Object-Oriented.
COMP 121 Week 8: Generic Collections. Objectives To understand type variables and how they are used in generic programming To be able to implement and.
Data Abstraction SWE 619 Software Construction Last Modified, Spring 2009 Paul Ammann.
Effective Java, Chapter 4: Classes and Interfaces Last Updated: Fall 2011.
Iteration Abstraction SWE Software Construction Fall 2009.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Mutability SWE 332 Fall 2015 Paul Ammann. SWE 3322 Data Abstraction Operation Categories Creators Create objects of a data abstraction Producers Create.
619 Final Review Last updated Fall 2011 Paul Ammann.
David Evans CS201J: Engineering Software University of Virginia Computer Science Lecture 5: Implementing Data Abstractions.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Fall 2013 Chapter 10 Thinking.
Lecture 10 – Polymorphism Nancy Harris with additional slides Professor Adams from Lewis & Bernstein.
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
Chapter 10 Thinking in Objects
OOP: Encapsulation &Abstraction
More Sophisticated Behavior
Chapter 10 Thinking in Objects
Chapter 10 Thinking in Objects
Abstract Data Types II.
Chapter 11 Object-Oriented Design
The hashCode method.
Abstraction Functions and Representation Invariants
Object Based Programming
Chapter 9 Thinking in Objects
Corresponds with Chapter 7
CSE 1030: Data Structure Mark Shtern.
Effective Java: Classes and Interfaces
Chapter 9 Thinking in Objects
Java Programming, Second Edition
Java Inheritance.
CS 112 Programming 2 Lecture 02 Abstract Classes & Interfaces (2)
SWE 619 Last modified Fall 2007 Saket Kaushik, Paul Ammann
Abstract Data Types II.
Basic Mutability Paul Ammann.
Presentation transcript:

Mutability SWE 332 Fall 2011 Paul Ammann

SWE 3322 Data Abstraction Operation Categories Creators Create objects of a data abstraction Producers Create objects of their type based on existing objects Typically used in immutable data types Mutators Modify objects of their type Typically used in mutable data types Observers Take objects of their type as inputs and return results of other types

SWE 3323 Adequacy of a API Should provide enough operations so that users can manipulate objects conveniently and efficiently Should have at least three of the four category operations Should be fully populated Possible to obtain every possible abstract object state

SWE 3324 Some Examples Polynomials – We’ve already seen this Poly.java Complex numbers (pp in Bloch) Complex.java Note use of producers instead of mutators

SWE 3325 Mutable/Immutable Transform Mutable Stack example in Bloch (page 56) Stack.java Goal: Transform to an immutable version ImmutableStack s = new ImmutableStack(); s = s.push(“cat”); s = s.push(“dog”); What do these objects look like?

SWE 3326 Mutator  Producer Consider a void mutator method in class C: public void mutator1(…) Corresponding producer method: public C producer1(…) Consider a non-void mutator method in class C: public S mutator2(…) Corresponding observer/producer methods are: public S observerPart(…) public C producerPart(…) Note that non-void mutator needs to be split into two methods. Example: pop() in Stack vs. pop(), top() in ImmutableStack

SWE 3327 Bloch Item 15: Minimize Mutability Reasons to make a class immutable: Easier to design Easier to implement Easier to use Less prone to error More secure Easier to share Thread safe

SWE Rules to Make a Class Immutable 1. Don’t provide any mutators 2. Ensure that no methods can be overridden (more detail…) 3. Make all fields final (more detail…) 4. Make all fields private 5. Ensure exclusive access to any mutable components (more detail…)

SWE 3329 Rule 2: No Overridden Methods Prevents careless or malicious subclasses from compromising the immutable behavior of the class How to implement Make class final (easiest) Make methods final (allows subclassing) Make all constructors private or package- private Requires static factories, which are better anyway

SWE Rule 3: Make All Fields Final Clearly expresses intent System enforcement of immutability Issues with object instantiation and thread access Sometimes, making fields final is too strong Lazy initialization may be preferable

SWE Rule 5: Exclusive Access to Mutable Components Never initialize a mutable field to a client provided reference Never return a reference to a mutable field Make defensive copies

SWE Typical Transformation Typical method in mutable class Foo: public void foo(T1 t1, T2, t2, …) {modify “this”} Immutable version of Foo: public Foo foo(T1 t1, T2, t2, …) { Foo f = … … return f; } Functional programming vs. procedural programming.

SWE Disadvantage: Performance Typical approach: Provide immutable class Provide mutable companion class for situations where performance is an issue Clients choose on performance needs Example in Java Library: String (Immutable) StringBuilder (Companion Mutable Class) Static factories can cache frequently used items

SWE Caching Frequently Used Items Goals: Control creation of immutable objects Limit occurrence of given object to 0 or 1 Return existing object if possible, Else construct new object Flyweight Pattern Requires a table of existing items Table design is a key decision Requires hiding constructors Enables “fast” equals(), hashCode() Memory leakage may be an issue

SWE Flyweight example public class Word { private static Map map = new HashMap(); // Strings to Words private String s; // returns word corresponding to s public static Word makeWord (String s) // factory { if (map.containsKey(s)) return map.get(s); else …} // creates this to be Word corresponding to s private Word (String s) { this.s = s; } // why private ??? // producer to join two words public Word join (Word w1; Word w2); { String res = w1.toString() + w2.toString(); if (map.containsKey(res)) return map.get(res); else …}