Basic Mutability Paul Ammann.

Slides:



Advertisements
Similar presentations
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Strings.
Advertisements

Pointer Analysis – Part I Mayur Naik Intel Research, Berkeley CS294 Lecture March 17, 2009.
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.
Mutability SWE 332 Fall 2011 Paul Ammann. SWE 3322 Data Abstraction Operation Categories Creators Create objects of a data abstraction Producers Create.
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.
CSE 331 SOFTWARE DESIGN & IMPLEMENTATION ABSTRACT DATA TYPES II Autumn 2011.
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.
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.
CS 171: Introduction to Computer Science II Stacks Ymir Vigfusson.
CSE 331 Software Design & Implementation Dan Grossman Fall 2014 Data Abstraction: Abstract Data Types (ADTs) (Based on slides by Mike Ernst, David Notkin,
Cs205: engineering software university of virginia fall 2006 Semantics and Specifying Procedures David Evans
Classes and Class Members Chapter 3. 3 Public Interface Contract between class and its clients to fulfill certain responsibilities The client is an object.
Chapter 3 Introduction to Collections – Stacks Modified
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
COMP 121 Week 13: Stacks. Objectives Learn about the stack data type and how to use its four methods: push, pop, peek, and empty Understand how Java implements.
Designing and Exploring Classes We look further into classes and objects here, emphasizing the differences between OOP and procedural programming – Cohesion.
Effective C#, Chapter 1: C# Language Elements Last Updated: Fall 2011.
Checking Equality of Reference Variables. Arrays and objects are both “reference” types n They are allocated a chunk of memory in the address space n.
332 Final Review Last updated Fall 2013 Professor Ammann.
Design Patterns Gang Qian Department of Computer Science University of Central Oklahoma.
Defining Classes II. Today’s topics  Static methods  Static variables  Wrapper classes  References  Class parameters  Copy constructor.
Mixing Static and Non-static
Mutability: C++ const usage SWE 332 Fall 2011 Paul Ammann.
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Abstract Data Types – Examples / Summary (Based on slides by Mike Ernst and David Notkin)
Design Patterns Software Engineering CS 561. Last Time Introduced design patterns Abstraction-Occurrence General Hierarchy Player-Role.
Profiling Field Initialisation in Java Stephen Nelson, David J. Pearce & James Noble Victoria University of Wellington New Zealand.
Effective Java: Methods Common to All Objects SWE 619: Fall 2008 Paul Ammann.
Data Abstraction SWE 619 Software Construction Last Modified, Spring 2009 Paul Ammann.
Iteration Abstraction SWE Software Construction Fall 2009.
Introduction to Objects and Encapsulation Computer Science 4 Mr. Gerb Reference: Objective: Understand Encapsulation and abstract data types.
CSE 331 Software Design & Implementation Hal Perkins Winter 2015 Data Abstraction: Abstract Data Types (ADTs) (Based on slides by Mike Ernst, Dan Grossman,
Effective Java, Chapter 9: Exceptions Items Last modified Fall 2012 Paul Ammann.
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.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 10 Thinking in Objects.
David Evans CS201J: Engineering Software University of Virginia Computer Science Lecture 5: Implementing Data Abstractions.
David Stotts Computer Science Department UNC Chapel Hill.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Fall 2013 Chapter 10 Thinking.
Chapter 10 Thinking in Objects
Functions.
Introduction to CS/SWE 332
Chapter 10 Thinking in Objects
Chapter 10 Thinking in Objects
Java Programming: Guided Learning with Early Objects
Abstract Data Types II.
CSE 331 Software Design & Implementation
Lecture 4 D&D Chapter 5 Methods including scope and overloading Date.
Iteration Abstraction
Abstraction Functions and Representation Invariants
Chapter 9 Thinking in Objects
Corresponds with Chapter 7
Recursion Recursion is a math and programming tool
More on Thread Safety CSE451 Andrew Whitaker.
Effective Java: Classes and Interfaces
Introduction to CS/SWE 332
Effective Java, 3rd Edition Chapter 10: Exceptions
Iteration Abstraction
Chapter 9 Thinking in Objects
Lecture 4: Data Abstraction CS201j: Engineering Software
619 Final Review Fall 2017 Professor Ammann.
F II 3. Classes and Objects Objectives
SWE 619 Last modified Fall 2007 Saket Kaushik, Paul Ammann
Abstract Data Types II.
Effective Java, Chapter 9: Exceptions
Recursive Thinking.
Presentation transcript:

Basic Mutability Paul Ammann

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

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

Some Immutable Examples Polynomials – (from Liskov) Poly.java Sets of integer primitives (from Liskov) IntSet.java Complex numbers (from Bloch) Complex.java Note use of producers instead of mutators

Mutable/Immutable Transform Mutable Stack example in Bloch 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?

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

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.

Disadvantage: Performance Typical approach: Provide immutable class Provide mutable companion Clients choose on performance needs Example in Java Library: String (Immutable) StringBuilder (Companion Mutable Class) Static factories can cache frequently used items Notes Performance gain from mutable objects is often illusory (Bloch Item 88) Immutability requires additional rules (Bloch Item 17)