Menu

#1059 Change rule name "Use Singleton" should be "Use Utility class"

PMD-5.1.0
closed
None
1
2015-02-24
2013-01-16
No

The name of the rule is incorrect…

It's description:
"If you have a class that has nothing but static methods, consider making it a Singleton. Note that this doesn't apply to abstract classes, since their subclasses may well include non-static methods. Also, if you want this class to be a Singleton, remember to add a private constructor to prevent instantiation."

This rule should be called "Use Utility class".

A Singleton is where the class is instantiated, but once.
A Singleton therefore often does have member methods.
Also despite the formal definition, in practice Singletons don't necessary enforce instantiation through private constructors either, for example spring beans are singletons (when not prototype beans) yet they typically have public contructors.

On the other hand a Utility class is never instantiated.
And therefore only ever has static methods. It seems that this rule is rather indicating that the code should be made into a Utility class.

(ref: https://sourceforge.net/p/pmd/discussion/188194/thread/06a3caa5#0946 )

Related

Issues: #1339

Discussion

  • Andreas Dangel

    Andreas Dangel - 2013-01-19
    • Description has changed:

    Diff:

    --- old
    +++ new
    @@ -1,4 +1,3 @@
    -
     The name of the rule is incorrect…
    
     It's description:
    
    • status: open --> accepted
    • assigned_to: Andreas Dangel
    • milestone: PMD-5.0.x --> PMD-5.1.0
     
  • Andreas Dangel

    Andreas Dangel - 2013-01-19
    • status: accepted --> closed
     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.