简体   繁体   English

重用对象的最佳方法

[英]The best way to reuse objects

I've been looking through some of my classes and it seems that every time I need a new input I make a new scanner (if the class doesn't have a scanner). 我一直在浏览一些班级,似乎每次我需要一个新输入时,我都会创建一个新的扫描仪(如果班级没有扫描仪)。 Is there some way to reuse scanners, or any other object between multiple classes? 是否有某种方法可以重用扫描程序,或在多个类之间重用其他任何对象?

Premature optimization is a bad thing (tm). 过早的优化是一件坏事(tm)。 As are singletons. 和单身人士一样。

Too often the performance issues you worry about are not the ones that your program bogs down in. So just create the scanners and enjoy the readable code. 很多时候,您担心的性能问题不是程序陷入的问题。因此,只需创建扫描程序并享受可读的代码即可。

If you run into performance issues, analyze the code. 如果遇到性能问题,请分析代码。 If it turns out that scanners are the performance bottleneck cause all your application does is create and discard scanners, then is the time to start thinking about reusing them at that particular part of the code. 如果事实证明扫​​描仪是性能瓶颈,导致您的应用程序所做的全部工作就是创建和丢弃扫描仪,那么该是时候考虑在代码的特定部分重新使用它们了。

Or more likely, replacing them with a bit more low-level objects, like BufferedReader and Pattern and Matcher. 或更可能的是,将它们替换为更底层的对象,例如BufferedReader和Pattern and Matcher。

也许将其放入实用程序类或类似的单例。

The singleton pattern is one way to do this. 单例模式是执行此操作的一种方法。 Another is to implement something like Java string internalization. 另一个是实现类似Java字符串内部化的方法。 You can maintain a static set of existing scanners and only create a new scanner if it is not already in the set. 您可以维护一组静态的现有扫描仪,并且仅在不存在的情况下创建一个新的扫描仪。

Creating multiple scanners does not need to be a problem unless you want them to access the same file. 除非您希望多个扫描器访问相同的文件,否则创建多个扫描器不必成问题。 One option is to make a util class that either contains a static scanner or using a singleton. 一种选择是使util类包含静态扫描器或使用单例。

A singleton is made like this : 单例制作如下:

class Singleton 
Singleton singleton 
private Singleton(){
//constructon
}

public synchronised singleton getsingleton(){
   if (singleton==null)
       singleton=new Singleton()
 return singleton 
}

your other option is to make it like this 您的另一个选择是使它像这样

class utils {
     public static scanner scan
}

which can be accessed using 可以使用

utils.scan 

instead of your normal variable name. 而不是您的普通变量名。

Do however keep in mind that 2 scanners behave differend then 1 scanner. 但是请记住,2个扫描仪的行为与1个扫描仪的不同。 For example let's say we have a file scanner that is midway trough a file now another object reads the next word, if you split over 2 scanners then the origenal will read the same word but if you have one scanner it will not. 例如,假设我们有一个文件扫描仪位于文件的中间,现在另一个对象读取下一个单词,如果您将其分成两个扫描仪,则原始人将读取相同的单词,但是如果您使用一个扫描仪,则不会读取该单词。 For example 2 scanners A and B both read created for the text "this is an example" 例如,两个扫描仪A和B都读取了为文本“这是一个示例”而创建的

print(A.next())
print(B.next())
print(A.next())
print(B.next())
would give us "this this is is" 
if however we run it with just a single scanner we get 
print(A.next())
print(A.next())
print(A.next())
print(A.next())
which would give us "this is an example" 

So remember that a single scanner will not behave the same as 2 scanners, this is especially notable if you try and do something like multitreading. 因此请记住,一台扫描仪的行为将与两台扫描仪的行为不同,如果您尝试进行多行扫描等操作,这一点尤其明显。 So if it's just to try and save those variable names you might not want to do this. 因此,如果只是尝试保存这些变量名,则可能不希望这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM