如何使特定对象的可变类成为不可变类?

huangapple go评论101阅读模式
英文:

Given a mutable class, how to make immutable a specific object of this class?

问题

我有这个类,显然对于我创建的每个实例都是可变的,但我想知道是否有一种包装器(或类似的东西)可以使得只有一个特定的这个类的对象是不可变的。例如 Collections.unmodifiableList(beanList)

  1. class Animal {
  2. private String name;
  3. private String commentary;
  4. public Animal(String nombre, String comentario) {
  5. this.name = nombre;
  6. this.commentary = comentario;
  7. }
  8. @Override
  9. public boolean equals(Object o) {
  10. if (this == o) return true;
  11. if (o == null || getClass() != o.getClass()) return false;
  12. Animal animal = (Animal) o;
  13. return Objects.equals(name, animal.name);
  14. }
  15. @Override
  16. public int hashCode() {
  17. return Objects.hash(name);
  18. }
  19. public String getName() {
  20. return name;
  21. }
  22. public String getCommentary() {
  23. return commentary;
  24. }
  25. // 此处省略了setCommentary和setName方法,以及其他方法
  26. }
英文:

I got THIS class which is obviously mutable for every instance I create, but I want to know if there´s
some kind of wrapper (or something) to make just ONE specific object of THIS class immutable. e.g Collections.unmodifiableList(beanList).

  1. class Animal {
  2. private String name;
  3. private String commentary;
  4. public Animal(String nombre, String comentario) {
  5. this.name = nombre;
  6. this.commentary = comentario;
  7. }
  8. @Override
  9. public boolean equals(Object o) {
  10. if (this == o) return true;
  11. if (o == null || getClass() != o.getClass()) return false;
  12. Animal animal = (Animal) o;
  13. return Objects.equals(name, animal.name);
  14. }
  15. @Override
  16. public int hashCode() {
  17. return Objects.hash(name);
  18. }
  19. public String getName() {
  20. return name;
  21. }
  22. public String getCommentary() {
  23. return commentary;
  24. }
  25. public void setCommentary(String commentary) {
  26. this.commentary = commentary;
  27. }
  28. public void setName(String name) {
  29. this.name = name;
  30. }
  31. }

答案1

得分: 6

我所知道的唯一方法是实例化它并重写能够修改特定实例的方法:

  1. Animal animal = new Animal("name", "commentary") {
  2. @Override
  3. public void setCommentary(String commentary) {
  4. throw new UnsupportedOperationException("The Animal is immutable");
  5. }
  6. @Override
  7. public void setName(String name) {
  8. throw new UnsupportedOperationException("The Animal is immutable");
  9. }
  10. };

这也满足了只有 类的一个特定实例 具有特殊行为的条件。


如果你需要更多类似的实例,可以创建一个充当装饰者(并非完全如此)的包装类。不要忘记 将这个类声明为 final,否则你可能会按照我上面描述的方式重写它的方法,从而破坏其不可变性。

  1. Animal animal = new ImmutableAnimal(new Animal("name", "commentary"));
  1. final class ImmutableAnimal extends Animal {
  2. public ImmutableAnimal(Animal animal) {
  3. super(animal.getName(), animal.getCommentary());
  4. }
  5. @Override
  6. public void setCommentary(String commentary) {
  7. throw new UnsupportedOperationException("The Animal is immutable");
  8. }
  9. @Override
  10. public void setName(String name) {
  11. throw new UnsupportedOperationException("The Animal is immutable");
  12. }
  13. }
英文:

The only way I am aware of is to instantiate it and override the methods that are able to modify the particular instance:

  1. Animal animal = new Animal("name", "commentary") {
  2. @Override
  3. public void setCommentary(String commentary) {
  4. throw new UnsupportedOperationException("The Animal is immutable");
  5. }
  6. @Override
  7. public void setName(String name) {
  8. throw new UnsupportedOperationException("The Animal is immutable");
  9. }
  10. };

This also satisfied the condition that only one specific instance of the class has a special behavior.


If you need more of them, create a wrapping class that acts as a decorator (isn't exactly). Do not forget to make the class as final otherwise you would be able to override its method in the way I described above and its immutability might break.

  1. Animal animal = new ImmutableAnimal(new Animal("name", "commentary"));
  1. final class ImmutableAnimal extends Animal {
  2. public ImmutableAnimal(Animal animal) {
  3. super(animal.getName(), animal.getCommentary());
  4. }
  5. @Override
  6. public void setCommentary(String commentary) {
  7. throw new UnsupportedOperationException("The Animal is immutable");
  8. }
  9. @Override
  10. public void setName(String name) {
  11. throw new UnsupportedOperationException("The Animal is immutable");
  12. }
  13. }

huangapple
  • 本文由 发表于 2020年8月23日 05:12:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/63541155.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定