6.3. Need Determines Class

 <  Day Day Up  >  

If a class contains all the data required by a method, that method belongs in that class. Another class should not extract data from a class, manipulate it, and then put it back into that class.

Conversely, if a method does not require any data from an object, the method is probably not needed as a member of the class. It is a candidate for a helper class or a general library class. With Sam's system, we might need to display the title of a CDRelease in multiple lines. We might leave the task of breaking up the lines to the display widget, or we might do it ourselves if we want more control over how the title appears. Suppose that CDRelease had a get_title_by_lines( ) method to return the title in separate lines:

 class CDRelease         {         String title;         static int CHARS_PER_LINE = 60;         String [] get_title_by_lines( )             {             return break_into_lines(title, CHARS_PER_LINE);             }         String [] break_into_lines(String text_to_break_up, int chars_per_line)             {             // Break text into separate strings             }         } 

The break_into_lines( ) method does not need to be part of the class. It does not manipulate any attributes directly. However, it looks like a useful tool for other classes. So the method seems appropriate for a separate utility class. For example:

 class StringHelper         {         static String [] break_into_lines(String text_to_break_up, int chars_per_         line)             {             // Break into separate strings             }         } 

With this method placed in StringHelper , we would write the get_title_by_lines( ) method as follows :

 String [] get_title_by_lines( )             {             return StringHelper.break_into_lines(title, CHARS_PER_LINE);             } 

As an alternative to placing break_into_lines( ) into a utility class, you could create a class that inherits from String (if your language allows deriving from String ). The line-breaking functionality would be an additional method. For example:

 class MyString extends String         {         String [] break_into_lines(int chars_per_line);         } 

A call to the break_into_lines( ) method would look like this:

 class CDRelease         {         MyString title;         static int CHARS_PER_LINE = 60;         String [] get_title_by_line( )             {             return title.break_into_lines(CHARS_PER_LINE);             }         } 

It's up to you what method you choose. The disadvantage of inheritance is that MyString can become crowded with methods as more functionality is added (e.g., break_into_words( ) , set_to_all_capitals( ) , etc.). On the other hand, it might feel more natural asking title to break itself up, instead of asking another class to do the work.

Once you decide on the approach (a package of methods or inheritance) for adding functionality to strings, you can employ the same approach for adding functionality to other predefined classes.

PLACE METHODS IN CLASSES BASED ON WHAT THEY NEED

If a method does not require instance data, it should not be a member of the class. Conversely, if all that the method requires is the instance data, it should be a member of the class .


 <  Day Day Up  >  


Prefactoring
Prefactoring: Extreme Abstraction, Extreme Separation, Extreme Readability
ISBN: 0596008740
EAN: 2147483647
Year: 2005
Pages: 175
Authors: Ken Pugh

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net