To install click the Add extension button. That's it.

The source code for the WIKI 2 extension is being checked by specialists of the Mozilla Foundation, Google, and Apple. You could also do it yourself at any point in time.

4,5
Kelly Slayton
Congratulations on this excellent venture… what a great idea!
Alexander Grigorievskiy
I use WIKI 2 every day and almost forgot how the original Wikipedia looks like.
Live Statistics
English Articles
Improved in 24 Hours
Added in 24 Hours
Languages
Recent
Show all languages
What we do. Every page goes through several hundred of perfecting techniques; in live mode. Quite the same Wikipedia. Just better.
.
Leo
Newton
Brights
Milds

Immutable interface

From Wikipedia, the free encyclopedia

In object-oriented programming, "immutable interface" is a pattern for designing an immutable object.[1] The immutable interface pattern involves defining a type which does not provide any methods which mutate state. Objects which are referenced by that type are not seen to have any mutable state, and appear immutable.

YouTube Encyclopedic

  • 1/3
    Views:
    6 741
    359
    717
  • C++Now 2017: Juanpe Bolivar “Postmodern Immutable Data Structures"
  • Ep03 (Part2) Visualizations -- bkase's Rust/WASM Gameboy Emulator Dev
  • Introduction to Scala - Part 3 | Big Data Hadoop Spark | CloudxLab

Transcription

Example

Java

Consider a Java class which represents a two-dimensional point.

public class Point2D {
    private int x;
    private int y;
    public Point2D(int x, int y) { this.x = x; this.y = y; }

    public int getX() { return this.x; }
    public int getY() { return this.y; }

    public void setX(int newX) { this.x = newX; }
    public void setY(int newY) { this.y = newY; }
}

The class Point2D is mutable: its state can be changed after construction, by invoking either of the setter methods (setX() or setY()).

An immutable interface for Point2D could be defined as:

public interface ImmutablePoint2D {
    public int getX();
    public int getY();
}

By making Point2D implement ImmutablePoint2D, client code could now reference a type which does not have mutating methods, and thus appears immutable. This is demonstrated in the following example:

ImmutablePoint2D point = new Point2D(0,0);  // a concrete instance of Point2D is referenced by the immutable interface
int x = point.getX(); // valid method call
int y = point.setX(42); // compile error: the method setX() does not exist on type ImmutablePoint2D

By referencing only the immutable interface, it is not valid to call a method which mutates the state of the concrete object.

Advantages

  • Clearly communicates the immutable intent of the type.
  • Unlike types implementing the Immutable Wrapper pattern, does not need to "cancel out" mutating methods by issuing a "No Operation" instruction, or throwing a runtime exception when a mutating method is invoked.

Disadvantages

  • It is possible for instances referenced by the immutable interface type to be cast to their concrete, mutable type, and have their state mutated. For example:
    public void mutate(ImmutablePoint2D point) {
        ((Point2D)point).setX(42); // this call is legal, since the type has
                                   // been converted to the mutable Point2D class
    }
    
  • Concrete classes have to explicitly declare they implement the immutable interface. This may not be possible if the concrete class "belongs to" third-party code, for instance, if it is contained within a library.
  • The object is not really immutable and hence not suitable for use in data structures relying on immutability like hash maps. And the object could be modified concurrently from the "mutable side".
  • Some compiler optimizations available for immutable objects might not be available for mutable objects.

Alternatives

An alternative to the immutable interface pattern is the immutable wrapper pattern.

Persistent data structures are effectively immutable while allowing modified views of themselves.

References

This page was last edited on 1 January 2024, at 14:00
Basis of this page is in Wikipedia. Text is available under the CC BY-SA 3.0 Unported License. Non-text media are available under their specified licenses. Wikipedia® is a registered trademark of the Wikimedia Foundation, Inc. WIKI 2 is an independent company and has no affiliation with Wikimedia Foundation.