views:

78

answers:

1

hi,

I am creating a java API for an addressbook Is it a good practice to use Enums in my API ?

I am using it as follows:

public enum AddressType {

 WORK,HOME

}

public class AddressBook implements Function{

  Map<String, Details> byName = new TreeMap<String,Details>();


    public void addNewContact(String name,  String address, AddressType 
            addressType) {

        byName.put(name, new Details(name,new Address(address,addressType)));
// addressType is my enum AddressType
    }

Please advise if there is a better way to do this?

Also could you guide on how I could determine which methods I should declare as protected and private? I want this API to be accessible for public so my understanding was that all the methods, classes, enums I create should be public. But would that not be overlooking encapsulation property of Java??

Please help.

Thank you }

A: 

Enums as a general rule are a good idea. For any data that can be represented as a small number of unchanging strings, it's a very useful datatype to have around. (It's especially nice that you can attach metadata and behaviors to them if desired)

That said, the one hesitation I have about your idea of using it for contact types such as WORK and HOME is that it makes it impossible for the user to have their own custom contact type. For example, what if I have two part time jobs and want to categorize people based on which job they are at?

If you're okay with that limitation, I'd say go for it.

If you want the additional flexibility, I'd suggest either storing the contact type as a string or going for a database solution. For something like this, it may make sense to store your data in a SQLite database as that allows you to easily build a relational model.

Steven Schlansker
is there some better way you can suggest by which i can incorporate the flexibility?
jillika iyer