scala 中何时使用单例对象以及何时使用实际对象
我想知道在 scala 中使用实际对象与在 scala 中使用单例对象是否有任何特定规则或者是否有要遵循的经验法则
说我有一个这样的类
class GetDataInput {
def getNameInput() {
println("Enter the name value: ")
}
def getTypeInput() {
println("Enter the type value: ")
}
def getBothInput() {
println("Enter the name value: ")
println("Enter the type value: ")
}
}
,最好按常规术语使用它,例如
val someval = new GetDataInput()
someval.getNameInput()
或者为此类创建一个单例对象并使用
object GetDataInput {
def getNameInp() = getNameInput()
}
该类上的任何指针访问方法是否很好?
I would like to know if there is any specific rule or if there is a rule of thumb to be followed on using actual objects in scala vs singleton objects in scala
say i have a class like this
class GetDataInput {
def getNameInput() {
println("Enter the name value: ")
}
def getTypeInput() {
println("Enter the type value: ")
}
def getBothInput() {
println("Enter the name value: ")
println("Enter the type value: ")
}
}
is it better to use it in regular terms like
val someval = new GetDataInput()
someval.getNameInput()
or is it good to create a singleton object for this class and access the methods using that
object GetDataInput {
def getNameInp() = getNameInput()
}
Any pointers on this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
通常,您在以下情况下创建
对象
:拥有潜在类的不同实例绝对没有意义,例如,包含多个纯函数(如数学函数、工厂方法)
您想要编写与 java
static
等效的代码方法或静态最终
常数。 (请参阅 Companion 对象)。您想要一个更简单的枚举值替代方案(由
对象
实例扩展的密封特征
)。在您的示例中,所有函数都是纯函数,并且类是无状态的。因此,所有实例都将严格相等。把它变成一个对象是有意义的:
如果你做出了错误的选择,不用担心,它很容易重构。通常,您可以保留对对象的所有现有调用,只需提取特征中的方法即可:
Generally you make an
object
when:It makes absolutely no sense of having different instances of a potential class, for example, to enclose several pure functions (like methematical functions, factory methods)
You want to write the equivalent of java
static
method orstatic final
constants. (see Companion objects).You want a simpler alternative for enum values (a
sealed trait
extended byobject
instances).In your example, all the functions are pure, and the class is stateless. Therefore all instances will be strictly equal. It makes sense to turn it into an object:
If you made the wrong choice, don't worry, it is easy to refactor. Usually you can keep all existing calls to the object, simply by extracting the methods in a trait:
问题是:“你需要一个类型的不同实例吗?”如果是这样,那么就选择一个类(或一个特征),如果不是,就选择一个单例。顺便说一句,对于您所使用的语言没有具体的指导方针,只是因为它内置了单例模式。
The question is rather: "Do you need different instances of a type?" If so, then go for a class (or a trait), if not go for a singleton. And btw there are no specific guidelines for the language you are using only because it has the singleton pattern built into it.
在 scala 中,对象的主要用途之一是充当单例的角色。如果你想使用一个类作为单例,只需将类本身声明为一个对象即可。然后你可以这样做:
在内部,scala将惰性地创建你的类的单个实例,并在程序运行期间保持它的活动状态,所以每当你调用对象上的方法时,你实际上是在调用该类的单例实例的方法。由 scala 运行时管理的类。
In scala, one primary use of objects is to fill the role of singletons. If you want to use a class as a singleton, just declare the class itself as an object. Then you could do:
Internally, scala will lazily create a single instance of your class and keep it alive for the duration of the program, so anytime you call a method on the object, you're really calling methods of a singleton instance of the class managed by the scala runtime.