HOME

TheInfoList



OR:

In
object-oriented programming Object-oriented programming (OOP) is a programming paradigm based on the concept of "objects", which can contain data and code. The data is in the form of fields (often known as attributes or ''properties''), and the code is in the form of pr ...
, "immutable interface" is a
pattern A pattern is a regularity in the world, in human-made design, or in abstract ideas. As such, the elements of a pattern repeat in a predictable manner. A geometric pattern is a kind of pattern formed of geometric shapes and typically repeated l ...
for designing an
immutable object In object-oriented and functional programming, an immutable object (unchangeable object) is an object whose state cannot be modified after it is created.Goetz et al. ''Java Concurrency in Practice''. Addison Wesley Professional, 2006, Section 3.4 ...
.Immutable Interface
c2.com

immutable
mindprod.com
- Peter Haggar
"Practical Java Praxis 65: Use Inheritance or Delegation to Define Immutable Classes"
''informIT'',January 22, 2001 The immutable interface pattern involves defining a type which does not provide any
methods Method ( grc, μέθοδος, methodos) literally means a pursuit of knowledge, investigation, mode of prosecuting such inquiry, or system. In recent centuries it more often means a prescribed process for completing a task. It may refer to: *Scien ...
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 two-dimensional point. public class Point2D 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 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 In object-oriented and functional programming, an immutable object (unchangeable object) is an object whose state cannot be modified after it is created.Goetz et al. ''Java Concurrency in Practice''. Addison Wesley Professional, 2006, Section 3. ...
pattern, does not need to "cancel out" mutating methods by issuing a "
No Operation In computer science, a NOP, no-op, or NOOP (pronounced "no op"; short for no operation) is a machine code, machine language instruction and its assembly language mnemonic, programming language statement, or protocol (computing), computer protoc ...
" 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 Cast may refer to: Music * Cast (band), an English alternative rock band * Cast (Mexican band), a progressive Mexican rock band * The Cast, a Scottish musical duo: Mairi Campbell and Dave Francis * ''Cast'', a 2012 album by Trespassers William * ...
to their concrete, mutable type, and have their state mutated. For example: public void mutate(ImmutablePoint2D point) * 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 In object-oriented and functional programming, an immutable object (unchangeable object) is an object whose state cannot be modified after it is created.Goetz et al. ''Java Concurrency in Practice''. Addison Wesley Professional, 2006, Section 3. ...
pattern.
Persistent data structures In computing, a persistent data structure or not ephemeral data structure is a data structure that always preserves the previous version of itself when it is modified. Such data structures are effectively Immutable object, immutable, as their ope ...
are effectively immutable while allowing modified views of themselves.


References

{{DEFAULTSORT:Immutable Interface Object-oriented programming Articles with example Java code