Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (538)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (600)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  newSomething() instead of new Something  (Read 1700 times)
0 Members and 1 Guest are viewing this topic.
Offline zingbat

Senior Devvie




Java games rock!


« Posted 2005-10-23 23:02:10 »

I was looking at one of the nio demonstration examples and noticed this line:

1  
2  
3  
// Charset and decoder for ISO-8859-15
private static Charset charset = Charset.forName("ISO-8859-15");
private static CharsetDecoder decoder = charset.newDecoder();


The first one is moderatly understandable since the method won't create a new object. But the second line is it really necessary ?

What would be the problem if they simply use this:

1  
private static CharsetDecoder decoder = new CharsetDecoder(charset);


Off topic: Don't you wish sometimes you could use C++ syntax like this?

1  
private static CharsetDecoder decoder(charset);

Offline swpalmer

JGO Coder


Exp: 12 years


Where's the Kaboom?


« Reply #1 - Posted 2005-10-24 01:18:56 »

The difference is that new CharsetDecoder(charset); must return a class of type CharsetDecoder, where as charset.newDecorder() can return a subclass.

Often this technique is used when the class in question is abstract and you are trying to hide the implementation details.

Offline Vorax

Senior Devvie


Projects: 1


System shutting down in 5..4..3...


« Reply #2 - Posted 2005-10-24 01:27:15 »

It's utilzing the Factory Method Design Pattern.  Definetly one of the more useful deign patterns in my experience.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Markus_Persson

JGO Wizard


Medals: 16
Projects: 19


Mojang Specifications


« Reply #3 - Posted 2005-10-24 08:16:21 »

I never actually do this myself (mostly out of laziness), but I still think all constructors should be forced private. Factory methods are cute.

Play Minecraft!
Offline zingbat

Senior Devvie




Java games rock!


« Reply #4 - Posted 2005-10-24 09:20:29 »

Strange how the "new" operator is looking so limited at the moment: it must allways return a new object (not an already existing link to some static object) and it must be a class of the type used in the operator (not a subclass of that type).

I wonder why using a "public new" at all to create a new object ?
Offline DaveLloyd

Junior Devvie




Making things happen fast with Java!


« Reply #5 - Posted 2005-10-26 18:46:28 »

The usual naming is createSomething rather than newSomething is it not? getSomething seems to be used where there is a backing cache and you don't expect it to return a unique instance.

Offline zingbat

Senior Devvie




Java games rock!


« Reply #6 - Posted 2005-10-28 14:58:38 »

The usual naming is createSomething rather than newSomething is it not? getSomething seems to be used where there is a backing cache and you don't expect it to return a unique instance.

The most intuitive name for me would be simply make(args) or create(args). No need for the extra word. Java is already clutered with unnecessary long names.
Offline arne

Senior Devvie




money is the worst drug- we should not let it rule


« Reply #7 - Posted 2005-10-28 17:15:04 »

The usual naming is createSomething rather than newSomething is it not? getSomething seems to be used where there is a backing cache and you don't expect it to return a unique instance.

The most intuitive name for me would be simply make(args) or create(args). No need for the extra word. Java is already clutered with unnecessary long names.

Ey, in your code you can do what you want, simply use create(), nobody restricts you there, if you think this is enough. But imagine you've got a factory-class that can create A-objects and B-objects. Then it would for sure be a good way to call the methods

A createA(); and
B createB();

About this long name issue: I rather have long names than
cnoiasw();

Arne

PS: cnoiasw means createNewObjectInAShortWay

:: JOODE :: Xith3d :: OdeJava ::
Offline zingbat

Senior Devvie




Java games rock!


« Reply #8 - Posted 2005-10-28 21:21:15 »

But imagine you've got a factory-class that can create A-objects and B-objects. Then it would for sure be a good way to call the methods

When you create an object from a factory isn't it irrelevant what the real class of the object is ? You are going to work with it using the common base class interface anyway. If you need to make that distinction then perhaps you shouldn't use a factory and create the object directly from its class.

About this long name issue: I rather have long names than
cnoiasw();

When i said unnvecessarly long names i wasn't refereing to abreviated names. I think a name should be as short as possible but wthout being cryptic.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

rwatson462 (29 views)
2014-12-15 09:26:44

Mr.CodeIt (20 views)
2014-12-14 19:50:38

BurntPizza (40 views)
2014-12-09 22:41:13

BurntPizza (75 views)
2014-12-08 04:46:31

JscottyBieshaar (37 views)
2014-12-05 12:39:02

SHC (50 views)
2014-12-03 16:27:13

CopyableCougar4 (47 views)
2014-11-29 21:32:03

toopeicgaming1999 (113 views)
2014-11-26 15:22:04

toopeicgaming1999 (100 views)
2014-11-26 15:20:36

toopeicgaming1999 (30 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!