Results 1 to 2 of 2
  1. #1
    mac's Avatar
    mac
    mac is offline Member
    Join Date
    Dec 2009
    Location
    St. Petersburg, Russia
    Posts
    81
    Rep Power
    0

    Default classes, true purpose of

    If variables store current state and methods implement behaviour and classes are basically a collections of methods grouped logically together ... i wonder how to better structure my classes content.

    Say job is to take a look at 'n' files, read them into memory, do something and make a report - knowing that i will have to be reading more and more files in the future for other purposes, should i be placing method that actually reads a file and returns its contents into it's own utils.java class in my own com.mac.utils package so i can import it later into the main one?

    In the industry, is this how things are done, or perhaps, you just make a local copy of that utils java and place it into the main package? To dwell more on this example a little bit - i don't know what type of return data my later code will need, so some overloading may be in order.

    Am i thinking in the right direction? Please advise

    Let me add that my current role in the organization has to do with supporting a large number of distributed applications. What this means is that master collection of tools is set to grow substantially over time. This means reusability is a must. And that begs the question, what should be reused? Should i concern myself with making a collection of reusable classes in my group's package com.group.utils?

    Thanks for reading,
    mac

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,524
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by mac View Post
    If variables store current state and methods implement behaviour and classes are basically a collections of methods grouped logically together ... i wonder how to better structure my classes content.

    Say job is to take a look at 'n' files, read them into memory, do something and make a report - knowing that i will have to be reading more and more files in the future for other purposes, should i be placing method that actually reads a file and returns its contents into it's own utils.java class in my own com.mac.utils package so i can import it later into the main one?

    In the industry, is this how things are done, or perhaps, you just make a local copy of that utils java and place it into the main package? To dwell more on this example a little bit - i don't know what type of return data my later code will need, so some overloading may be in order.

    Am i thinking in the right direction? Please advise
    You're halfway there; there is an important 'rule of thumb' that says 'code to the interface, not to the implementation'. To accomplish that task design interfaces first and try to make them 'fit' your use cases; just define what needs to be done, not how they should do it (of course you can keep that in the back of your mind but don't let it influence your interface design).

    Next, if you're certain that implementations of those interfaces can do the job, implement the classes. If an interface A depends on an interface B make an implementation of A depend on interface B, not an implementation thereof.

    Don't make your interfaces too general or you'll end up with implementations of that interface that can be best described as 'God classes' or 'Swiss Army knifes'. You don't want to maintain those molochs.

    But first of all: practice and study because designing interfaces is something that swings you back and forth between generalizations for the sake of it to the need to get something implemented. Finding a balance between those two opposing forces is the thin line you have to walk.

    kind regards,

    Jos

Similar Threads

  1. interface purpose?
    By frejon26 in forum New To Java
    Replies: 5
    Last Post: 06-27-2014, 09:41 AM
  2. while(true)
    By ravian in forum New To Java
    Replies: 7
    Last Post: 06-29-2011, 06:05 AM
  3. true && !true
    By SirFalcon in forum New To Java
    Replies: 3
    Last Post: 10-29-2009, 02:33 AM
  4. which one of the following is true about interface?
    By makpandian in forum New To Java
    Replies: 3
    Last Post: 06-30-2009, 12:23 AM
  5. Over-riding purpose fails..
    By udayadas in forum New To Java
    Replies: 7
    Last Post: 08-24-2008, 04:14 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •