Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > returning type

Reply
Thread Tools

returning type

 
 
-
Guest
Posts: n/a
 
      03-27-2005
which is the recommended way?

1)
private Action createExitAction() {
return new ExitAction();
}

2)
private AbstractAction createExitAction() {
return new ExitAction();
}

3)
private ExitAction createAction() {
return new ExitAction();
}


if no. 1 is recommended, why then do most people do a

private JPanel createSomePanel() {
....
}

rather than a

private JComponent createSomePanel() {
....
}


thank you.
 
Reply With Quote
 
 
 
 
KiLVaiDeN
Guest
Posts: n/a
 
      03-27-2005

"-" <> a écrit dans le message de
news:...
> which is the recommended way?
>
> 1)
> private Action createExitAction() {
> return new ExitAction();
> }
>
> 2)
> private AbstractAction createExitAction() {
> return new ExitAction();
> }
>
> 3)
> private ExitAction createAction() {
> return new ExitAction();
> }
>
>
> if no. 1 is recommended, why then do most people do a
>
> private JPanel createSomePanel() {
> ...
> }
>
> rather than a
>
> private JComponent createSomePanel() {
> ...
> }
>
>
> thank you.


If you want to make your application the most "evolutive" possible, you must
use the 1st solution.
The JPanel example, comes from code that's not going to evolve about the
type returned, this method will always return a JPanel, so the conceptors
didnt find it coherent to return a JComponent, which would therefore imply
that someday eventually that method could return something else.

my guess,
K


 
Reply With Quote
 
 
 
 
Tony Morris
Guest
Posts: n/a
 
      03-27-2005
"-" <> wrote in message news:...
> which is the recommended way?
>
> 1)
> private Action createExitAction() {
> return new ExitAction();
> }
>
> 2)
> private AbstractAction createExitAction() {
> return new ExitAction();
> }
>
> 3)
> private ExitAction createAction() {
> return new ExitAction();
> }
>
>
> if no. 1 is recommended, why then do most people do a
>
> private JPanel createSomePanel() {
> ...
> }
>
> rather than a
>
> private JComponent createSomePanel() {
> ...
> }
>
>
> thank you.


Use the most abstract type.
Where some twit has failed to use interfaces appropriately, you'll have to
concede and use a class reference type, or write the dependant framework
yourself.

--
Tony Morris
http://xdweb.net/~dibblego/



 
Reply With Quote
 
Thomas G. Marshall
Guest
Posts: n/a
 
      03-27-2005
KiLVaiDeN coughed up:
> "-" <> a écrit dans le message de
> news:...
>> which is the recommended way?
>>
>> 1)
>> private Action createExitAction() {
>> return new ExitAction();
>> }
>>
>> 2)
>> private AbstractAction createExitAction() {
>> return new ExitAction();
>> }
>>
>> 3)
>> private ExitAction createAction() {
>> return new ExitAction();
>> }
>>
>>
>> if no. 1 is recommended, why then do most people do a
>>
>> private JPanel createSomePanel() {
>> ...
>> }
>>
>> rather than a
>>
>> private JComponent createSomePanel() {
>> ...
>> }
>>
>>
>> thank you.

>
> If you want to make your application the most "evolutive" possible,
> you must use the 1st solution.


Not necessarily in this particular (weird) case. His method is specifically
for returning an exit action.

*If* there is a reason (I'm dubious of this in your design for some reason)
for making a method hardwired to returning a specific type of action, then
there are likely things required in the return type that are only found
there (in ExitAction). If he chooses a class/interface upstream in the
hierarchy he must downcast later in order to access that functionality
specific to ExitAction.

It is true that in general you are best off using the highest possible base
class that provides all the functionality desired. In that way: 1. you
don't accidentally "stray" into functionality you don't need to use, and 2.
you have gained the greatest possible mobility in changing the datatype
later.





> The JPanel example, comes from code that's not going to evolve about
> the type returned, this method will always return a JPanel, so the
> conceptors didnt find it coherent to return a JComponent, which would
> therefore imply that someday eventually that method could return
> something else.
>
> my guess,
> K




--
"It's easier to be terrified by an enemy you admire."
-Thufir Hawat, Mentat and Master of Assassins to House Atreides


 
Reply With Quote
 
Thomas G. Marshall
Guest
Posts: n/a
 
      03-27-2005
Tony Morris coughed up:
> "-" <> wrote in message
> news:...
>> which is the recommended way?
>>
>> 1)
>> private Action createExitAction() {
>> return new ExitAction();
>> }
>>
>> 2)
>> private AbstractAction createExitAction() {
>> return new ExitAction();
>> }
>>
>> 3)
>> private ExitAction createAction() {
>> return new ExitAction();
>> }
>>
>>
>> if no. 1 is recommended, why then do most people do a
>>
>> private JPanel createSomePanel() {
>> ...
>> }
>>
>> rather than a
>>
>> private JComponent createSomePanel() {
>> ...
>> }
>>
>>
>> thank you.

>
> Use the most abstract type.
> Where some twit has failed to use interfaces appropriately, you'll
> have to concede and use a class reference type, or write the
> dependant framework yourself.


Again, this depends. Take for example the following declaration:

List myList = new ArrayList();

or

Collection myList = new ArrayList();

I use these all the time when what I'm doing is fairly simple, like mostly
add(), because it allows changing this to any other kind of list later.
*HOWEVER*, if my algorithm is *without a doubt* going to need random access
into this collection, then I would chose this:

ArrayList myList = new ArrayList();

Because I specifically would not want a LinkedList() to find its way in
there.

--
"It's easier to be terrified by an enemy you admire."
-Thufir Hawat, Mentat and Master of Assassins to House Atreides


 
Reply With Quote
 
Joona I Palaste
Guest
Posts: n/a
 
      03-27-2005
- <> scribbled the following:
> which is the recommended way?


> 1)
> private Action createExitAction() {
> return new ExitAction();
> }


> 2)
> private AbstractAction createExitAction() {
> return new ExitAction();
> }


> 3)
> private ExitAction createAction() {
> return new ExitAction();
> }


As a general rule, use the most abstract type possible that gives you
full access to every method you need (not necessarily every method there
is) without casting.
This is an "if in doubt" rule. For special needs, this rule might have
to be broken.

--
/-- Joona Palaste () ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"Outside of a dog, a book is a man's best friend. Inside a dog, it's too dark
to read anyway."
- Groucho Marx
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
type(d) != type(d.copy()) when type(d).issubclass(dict) kj Python 5 12-26-2010 06:48 PM
#define ALLOCIT(Type) ((Type*) malloc (sizeof (Type))) Yevgen Muntyan C Programming 10 02-13-2007 02:52 AM
returning none when it should be returning a list? randomtalk@gmail.com Python 11 05-02-2006 10:26 AM
Re: Type casting- a larger type to a smaller type pete C Programming 4 04-02-2004 05:19 PM
Re: Type casting- a larger type to a smaller type heyo C Programming 3 04-01-2004 06:35 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57