
Data Abstraction and Mutability in Programming
Explore the concepts of data abstraction, mutability, and API adequacy in programming. Learn about creators, producers, mutators, observers, and the transformation from mutable to immutable objects. Dive into the world of mutable and immutable examples, understanding the advantages and disadvantages of each approach.
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
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 2
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 3
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 4
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? 5
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 6
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. 7
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) 8