Grouping variables as classes

Discussion in 'Java' started by -, Jul 14, 2005.

  1. -

    - Guest

    If I have a class that has, among others, several fields such as

    public class SomeClass {

    private long startTime...
    private long endTime...

    // some other fields here

    ...

    public SomeClass() {
    }

    public long getStartTime() {
    return startTime;
    }

    ...
    }

    is it a good idea if I do this:


    public class SomeClass {

    private TimeInfo timeInfo = new TimeInfo();

    // some other fields here

    ...

    public SomeClass() {
    }

    public TimeInfo getTimeInfo() {
    return timeInfo;
    }

    protected static class TimeInfo {

    private long startTime ...
    private long endTime ...

    ...

    public TimeInfo() {
    }

    public long getStart() {
    return start;
    }

    ...
    }
    }

    This way, I can use 'getStart()' and 'getEnd()' rather than
    'getStartTime()' and 'getEndTime()'.

    Anything I should know of?
     
    -, Jul 14, 2005
    #1
    1. Advertising

  2. - wrote:
    > If I have a class that has, among others, several fields such as
    >
    > public class SomeClass {
    >
    > private long startTime...
    > private long endTime...
    >
    > // some other fields here
    >
    > ...
    >
    > public SomeClass() {
    > }
    >
    > public long getStartTime() {
    > return startTime;
    > }
    >
    > ...
    > }
    >
    > is it a good idea if I do this:
    >
    >
    > public class SomeClass {
    >
    > private TimeInfo timeInfo = new TimeInfo();
    >
    > // some other fields here
    >
    > ...
    >
    > public SomeClass() {
    > }
    >
    > public TimeInfo getTimeInfo() {
    > return timeInfo;
    > }
    >
    > protected static class TimeInfo {
    >
    > private long startTime ...
    > private long endTime ...
    >
    > ...
    >
    > public TimeInfo() {
    > }
    >
    > public long getStart() {
    > return start;
    > }
    >
    > ...
    > }
    > }
    >
    > This way, I can use 'getStart()' and 'getEnd()' rather than
    > 'getStartTime()' and 'getEndTime()'.
    >
    > Anything I should know of?


    Why don't you just use the shorter names in the original class?
    Unless TimeInfo is re-used somehow this appears to be a pointless change.

    Ray

    --
    XML is the programmer's duct tape.
     
    Raymond DeCampo, Jul 14, 2005
    #2
    1. Advertising

  3. -

    Wibble Guest

    - wrote:
    > If I have a class that has, among others, several fields such as
    >
    > public class SomeClass {
    >
    > private long startTime...
    > private long endTime...
    >
    > // some other fields here
    >
    > ...
    >
    > public SomeClass() {
    > }
    >
    > public long getStartTime() {
    > return startTime;
    > }
    >
    > ...
    > }
    >
    > is it a good idea if I do this:
    >
    >
    > public class SomeClass {
    >
    > private TimeInfo timeInfo = new TimeInfo();
    >
    > // some other fields here
    >
    > ...
    >
    > public SomeClass() {
    > }
    >
    > public TimeInfo getTimeInfo() {
    > return timeInfo;
    > }
    >
    > protected static class TimeInfo {
    >
    > private long startTime ...
    > private long endTime ...
    >
    > ...
    >
    > public TimeInfo() {
    > }
    >
    > public long getStart() {
    > return start;
    > }
    >
    > ...
    > }
    > }
    >
    > This way, I can use 'getStart()' and 'getEnd()' rather than
    > 'getStartTime()' and 'getEndTime()'.
    >
    > Anything I should know of?

    Its a fine pattern, particularly if the nested class has meaning, like a
    time range. Consider initializing timeinfo like:

    private TimeInfo timeInfo = newTimeInfo();
    protected TimeInfo newTimeInfo() { return(new TimeInfo()); }

    so that subclasses can override.
     
    Wibble, Jul 14, 2005
    #3
  4. -

    Wibble Guest

    Raymond DeCampo wrote:
    > - wrote:
    >
    >> If I have a class that has, among others, several fields such as
    >>
    >> public class SomeClass {
    >>
    >> private long startTime...
    >> private long endTime...
    >>
    >> // some other fields here
    >>
    >> ...
    >>
    >> public SomeClass() {
    >> }
    >>
    >> public long getStartTime() {
    >> return startTime;
    >> }
    >>
    >> ...
    >> }
    >>
    >> is it a good idea if I do this:
    >>
    >>
    >> public class SomeClass {
    >>
    >> private TimeInfo timeInfo = new TimeInfo();
    >>
    >> // some other fields here
    >>
    >> ...
    >>
    >> public SomeClass() {
    >> }
    >>
    >> public TimeInfo getTimeInfo() {
    >> return timeInfo;
    >> }
    >>
    >> protected static class TimeInfo {
    >>
    >> private long startTime ...
    >> private long endTime ...
    >>
    >> ...
    >>
    >> public TimeInfo() {
    >> }
    >>
    >> public long getStart() {
    >> return start;
    >> }
    >>
    >> ...
    >> }
    >> }
    >>
    >> This way, I can use 'getStart()' and 'getEnd()' rather than
    >> 'getStartTime()' and 'getEndTime()'.
    >>
    >> Anything I should know of?

    >
    >
    > Why don't you just use the shorter names in the original class?
    > Unless TimeInfo is re-used somehow this appears to be a pointless change.
    >
    > Ray
    >

    Yeah, I dont understand the name change either, especially since the
    long names are more clear. Its generally a bad idea to have more than
    one name for anything. Saving typing is a bad excuse.

    If the containing class is the only consumer, particularly if the
    semantics are local, then it can make sense to nest. It can make code
    more clear and makes it easy to promote the class to standalone later.
     
    Wibble, Jul 14, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David Wier
    Replies:
    0
    Views:
    456
    David Wier
    Aug 8, 2003
  2. lonelyplanet999
    Replies:
    1
    Views:
    2,267
    VisionSet
    Nov 13, 2003
  3. Razvan
    Replies:
    11
    Views:
    939
    Andrew Thompson
    Jul 17, 2004
  4. Dilip

    Grouping global variables

    Dilip, Feb 15, 2007, in forum: C++
    Replies:
    6
    Views:
    326
    Gavin Deane
    Feb 15, 2007
  5. Replies:
    9
    Views:
    997
Loading...

Share This Page