Why does GWT's DateTimeFormat use static factory methods?
UPDATE: I开发者_如何学Python'm sorry, this should be a question about GWT's DateTimeFormat not Java's DateFormat.
Hi. I'm trying to extend GWT's DateTimeFormat. Does anyone know why we have to use static factory methods to obtain GWT's DateTimeFormat objects? What is the purpose of hiding the class's constructors? Is there any problem if I create new static factory methods that simply delegate to protected constructors?
Factory methods provides "named constructors". You use it when other option will be to provide lot of constructors with different argument sets which will be not that convenient - you can only guess which one to use since all constructors will have the same name.
Moreover in factory method you don't have to create new object every time. This gives you freedom to implement some sort of caching easily and have control over the created instances.
java.util.Date is a testament to the fact that even brilliant programmers can screw up. java.util.Calendar, which Sun licensed to rectify the Date mess, is a testament to the fact that average programmers can screw up, too.
Why is the Java date API (java.util.Date, .Calendar) such a mess?
精彩评论