所有不可变对象都是单例实例吗? [英] Are all immutable objects singleton instances?

查看:98
本文介绍了所有不可变对象都是单例实例吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所有不可变对象都是单例实例吗?

Are all immutable objects singleton instances?

推荐答案

否.

不变的对象是不可更改的,通常的做法是通过构造函数传递(注入)所有必需的值(即,在实例化对象时指定它们),然后在对象的整个生命周期中从外部进行更改.

Immutable objects are unchangeable, the normal practice for these is to pass (inject) all required values via the constructor (i.e. they are specified when the object is instantiated), these are then externally unchangeable for the lifetime of the object.

单个对象是只有一个实例时,该实例可能是不变的,但很少是.

Singleton objects are when there is a single instance, that instance could be immutable but seldom is.

这篇关于所有不可变对象都是单例实例吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆