Immutability and Java | how to create Immutable Objects | Pitfalls & Anti patterns

Immutability and Java | how to create Immutable Objects | Pitfalls & Anti patterns

ProgrammingWithPassion

3 года назад

101 Просмотров

What are Immutable objects ?
Immutable object (unchangeable object) is an object whose state cannot be modified after it is created.

Immutable objects in Java?
String, Wrapper classes like Integer, Long, Boolean etc

How to create Immutable Objects ?
1. Declaring class and properties as final
2. taking constructor agrs
3. remoiving setters
4. creating defensive copies

What are the Pitfalls and Anti patterns while using immutable objects.
1. Use immutable objects when using hash data structures
2. Do not use violate immutability when using functional programming
Ссылки и html тэги не поддерживаются


Комментарии: