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

Slides:



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

1 Strings and Text I/O. 2 Motivations Often you encounter the problems that involve string processing and file input and output. Suppose you need to write.
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Winter 2013.
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.
1 Chapter 5 Threads 2 Contents  Overview  Benefits  User and Kernel Threads  Multithreading Models  Solaris 2 Threads  Java Threads.
Chapter 10 THINKING IN OBJECTS 1 Object Oriented programming Instructor: Dr. Essam H. Houssein.
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.
CSE 331 Software Design & Implementation Dan Grossman Fall 2014 Data Abstraction: Abstract Data Types (ADTs) (Based on slides by Mike Ernst, David Notkin,
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. The Factory Method Design Pattern (1) –A creational design.
Introduction to Object-oriented Programming CSIS 3701: Advanced Object Oriented Programming.
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
Introductory Software Engineering with a Focus on Dependency Management Christine Hofmeister.
1 Chapter 10: Data Abstraction and Object Orientation Aaron Bloomfield CS 415 Fall 2005.
Designing and Exploring Classes We look further into classes and objects here, emphasizing the differences between OOP and procedural programming – Cohesion.
Design Patterns SWE 619 Software Construction Fall 2008.
CSE 331 SOFTWARE DESIGN & IMPLEMENTATION MIDTERM REVIEW Autumn 2011.
Effective Java: Creating and Destroying Objects Last Updated: Fall 2012.
ArrayList, Multidimensional Arrays
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.
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.
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)
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.
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.
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.
Effective Java, Chapter 9: Exceptions Items Last modified Fall 2012 Paul Ammann.
Introduction to Object-oriented Programming CSIS 3701: Advanced Object Oriented Programming.
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.
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
Chapter 10 Thinking in Objects
More Sophisticated Behavior
Introduction to CS/SWE 332
Chapter 10 Thinking in Objects
Chapter 10 Thinking in Objects
Abstract Data Types II.
CSE 331 Software Design & Implementation
The hashCode method.
Chapter 6: Using Design Patterns
Abstraction Functions and Representation Invariants
Object Based Programming
Chapter 9 Thinking in Objects
Corresponds with Chapter 7
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
Lecture 14 - Environment Model (cont.) - Mutation - Stacks and Queues
F II 3. Classes and Objects Objectives
CS 112 Programming 2 Lecture 02 Abstract Classes & Interfaces (2)
SWE 619 Last modified Fall 2007 Saket Kaushik, Paul Ammann
Abstract Data Types II.
Effective Java, Chapter 9: Exceptions
Basic Mutability Paul Ammann.
Presentation transcript:

Mutability SWE 332 Fall 2015 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 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 3328 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 3329 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