Jump to content

Immutable interface

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Grundlefleck (talk | contribs) at 23:43, 3 October 2010. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.


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 or functions which mutate state. Objects which are referenced by that type are not seen to have any mutable state, and appear immutable.

Example

Java

Consider a Java class which represents a 2 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; } }

References