Immutable interface
Encyclopedia
In object-oriented programming
, "Immutable Interface" is a pattern
for designing an immutable object
. 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.
The class Point2D is mutable: its state can be changed after construction, by invoking either of the setter methods (
An immutable interface for Point2D could be defined as:
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:
By referencing only the immutable interface, it is not valid to call a method which mutates the state of the concrete object.
Object-oriented programming
Object-oriented programming is a programming paradigm using "objects" – data structures consisting of data fields and methods together with their interactions – to design applications and computer programs. Programming techniques may include features such as data abstraction,...
, "Immutable Interface" is a pattern
Design pattern (computer science)
In software engineering, a design pattern is a general reusable solution to a commonly occurring problem within a given context in software design. A design pattern is not a finished design that can be transformed directly into code. It is a description or template for how to solve a problem that...
for designing an immutable object
Immutable object
In object-oriented and functional programming, an immutable object is an object whose state cannot be modified after it is created. This is in contrast to a mutable object, which can be modified after it is created...
. The immutable interface pattern involves defining a type which does not provide any methods
Method (computer science)
In object-oriented programming, a method is a subroutine associated with a class. Methods define the behavior to be exhibited by instances of the associated class at program run time...
which mutate state. Objects which are referenced by that type are not seen to have any mutable state, and appear immutable.
Java
Consider a Java class which represents a 2 dimensional point.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:
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:
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 OperationNOPIn computer science, NOP or NOOP is an assembly language instruction, sequence of programming language statements, or computer protocol command that effectively does nothing at all....
" 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 castType conversionIn computer science, type conversion, typecasting, and coercion are different ways of, implicitly or explicitly, changing an entity of one data type into another. This is done to take advantage of certain features of type hierarchies or type representations...
to their concrete, mutable type, and have their state mutated. For example:
- 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.