| Summary: | [templates] add refactoring: singletonize | ||
|---|---|---|---|
| Product: | [Eclipse Project] JDT | Reporter: | Patrice Kerremans <patrice_kerremans> |
| Component: | UI | Assignee: | JDT-UI-Inbox <jdt-ui-inbox> |
| Status: | RESOLVED WONTFIX | QA Contact: | |
| Severity: | enhancement | ||
| Priority: | P3 | CC: | dannydig, dmitry.astu, markus.kell.r, nobody, pshivale |
| Version: | 2.1 | Keywords: | helpwanted |
| Target Milestone: | --- | ||
| Hardware: | All | ||
| OS: | All | ||
| Whiteboard: | |||
What's about providing the code ;-). Without help this will very likely not be in 2.1 ok, here's a thought: add the following template to the list of editor
templates, and we can create singletons using code completion which is -almost-
as good as refactoring :)
-----------8<--------------8<--------------------
/** the singleton instance */
private static final ${enclosing_type} INSTANCE =
new ${enclosing_type}();
/**
* Returns the singleton instance (not necessarily be of this class
* unique - may come from a pool read GoF-book).
* Return the singleton instance of this class (not necessarily be
* unique - may come from a pool read GoF-book)
*/
public static final ${enclosing_type} getInstance() {
return INSTANCE;
}
/**
* Default constructor.
*/
private ${enclosing_type}() {
super();
}
------------8<--------------8<---------------
Patrice, what you provided in comment 2 is a template. We could add a template but that's not a refactoring. This will not - give a preview - change occurrences of new Class() to use the singleton Note: you can add the template yourself since templates is configurable. Yes, you're absolutely right. I just gave it to you as a workaround. If and when I come up with a real refactoring I'll come back to you. reopen to reassing . As of now 'LATER' and 'REMIND' resolutions are no longer supported. Please reopen this bug if it is still valid for you. I also would appreciate a refactoring for singleton instance usage or at least a template delivered. I offer my help if desired. *** Bug 67192 has been marked as a duplicate of this bug. *** Still no plans to do this. "Introduce Singleton" would most of the time not be a refactoring (as soon as you have two instantiations, it cannot be behavior-preserving). The main work can already be done with "Introduce Factory". The rest can easily be done manually. Furthermore, there are multiple styles how singletons can be created - each with its own benefits and drawbacks. An editor template with your preferred style is the way to go! *** Bug 302434 has been marked as a duplicate of this bug. *** *** Bug 393000 has been marked as a duplicate of this bug. *** |
add a refactoring menuitem to create a singleton from a class by: say you right click the class MySingleton and select to singletonize it: 1. add (or modify if already existing) a private default constructor private MySingleton() { } 2. add a member variable private static final MySingletonClass INSTANCE = new MySingleton(); 3. add a static getinstance method public static MySingleton getInstance() { return INSTANCE; } 4. show preview done. THANKS a 1000K !