Return
Design pattern for reusing immutable methods in object-oriented languages
DOI:10.1016/j.cola.2026.101385.png)
Abstract
En 中文
Programming concepts from functional programming have grown in popularity and have been introduced in object-oriented programming. Among these concepts, immutability is a core concept of functional programming, which brings advantages to software development. However, introducing immutability in object-oriented programming languages presents some challenges. Problem: One of these challenges, in the concept of inheritance, is overriding of non-destructive mutators. Mutators are necessary for updating data as a software is running. Non-destructive mutators are mutators on immutable objects, which return new objects instead of modifying the receiver. When inheriting non-destructive mutators, a naive implementation will yield code duplication and have code scalability issues. Contribution: We analyze an example of overriding non-destructive mutators, discuss the challenges, and propose a solution that takes the form of a new design pattern, inspired from the Factory Method pattern. We also discuss the advantages and limitations of this pattern, as well as implementations in eight languages (Clojure, Common Lisp, C#, Java, Kotlin, OCaml, Rust, and Scala). We finally identify and discuss the language features that mostly affect the implementation of this pattern, and present a language extension to Common Lisp to demonstrate the effects of those features on the implementation. Conclusion: Our proposed design pattern helps reduce the code duplication and improve the code scalability of a naive implementation of inherited non-destructive mutators. However, using a language which includes features for functional updating is required to completely remove the code scalability issues of the implementation. We conclude that object-oriented programming languages should consider adding functional updating to their language features to better support their new functional programming features.
Keywords:
Object-oriented programming
Design patterns
Functional programming
Immutability
Language features
Journal
J
IF:
1.8
Papers:
28
Citations:
323

