oop - Singleton with Arguments in Java -


I was reading a singleton article on Wikipedia and I came to this example:

  Public Class Singleton {// Private Constructor Private Singleton () {} / ** * Singleton Holder Singleton. Gesture is loaded on first execution of * (*) or first access to singleton handlers. INSTANCE, Not Before * / Private Static Class Singleton Holder {Personal Static Last Singleton INSTANCE = New Singleton (); } Public Stable Singleton getInstance () {Return Singleton Holder. INSTANCE; }}  

Although I really like the method of behavior of this singleton, I can not see how the constructor should be able to adapt it to incorporating logic. What is the best way to do this in Java? Will I have to do something like that?

  Public class singletons {Private stationary singletons singleton = zero; Personal Final X; Private Singleton (int x) {this.x = x; } Public synchronized static singleton getInstance (int x) {if (singleton == blank) singleton = new singleton (x); Return singleton; }}  

Thanks!


EDIT: I think I have started a controversy with my wish to use singleton. Let me explain my motivation and hopefully someone can suggest a better idea. I am using the grid computing framework to execute the work in parallel. In general, I have something like this:

  // Sarratask implies SerialJetable public square work Abstractsk {actually real object object for reference; Public work (Referralolibic object object) {this.object = Object; } Public Zero Moves () {// Some objects (what is irreversible)}}  

with the object, even if I pass only a reference in all references to my data When the tasks are sorted, the data gets more copied what I want to do is to divide the object into all the tasks. Naturally, I can modify this type of class:

  // Sarratask applies Serializable Public Class Task Abstractsk {Personal Static ReferenceSafetyBig Object Object = Zero; Private final string filePath; Public work (strings phylax) {this.filePath = filePath; } Public Zero Run () {synchronize (this) {if (object == empty) {Object reader reader = new object reader (file path); Object = reader.read (); }} // Some things with the object (which is irreversible)}}  

As you can see, even I have a problem that will allow a different file path Passing means that there is nothing after passage for the first time. That's why I liked the idea for the store posted in the reply. Either way, instead of including the argument to load the file in the run method, I wanted to summarize this argument in a single class. I will not provide another example, but I hope you get this idea. Please listen to your thoughts in a more elaborate way to fulfill what I am trying to do. Thanks again!

I will make my point very clear: A singleton with the criteria is not a singleton < / Strong>.

By definition, a singleton is an object that you want to institute more than once. If you are trying to feed the parameter to the manufacturer, then what does the singleton mean?

You have two options if you want to start your singleton with some data, you can load it with immediately after data:

  SingletonObject Singleton = Singleton obje. .get instance (); Singletoin (ultimate, paramount); // init with data init  

If you are displaying your one hand, then it is recurring, and every time with different parameters, you also execute the parameter with the main method Can pass:

  Singleton Objec Singleton = Singleton Objez. Just Instance (); Singleton Dom (Parama, Ultimate); // Parameter on execution  

In any case, the instant will always be scalable otherwise your singleton is not singleton.


Comments

Popular posts from this blog

c++ - Linux and clipboard -

What is expire header and how to achive them in ASP.NET and PHP? -

sql server - How can I determine which of my SQL 2005 statistics are unused? -