views:

84

answers:

4

Here is an initial specification for a simple Address class. This is a simplification as it ignores complications such as apartments in the same building potentially having the same ‘number’,
e.g. 29a, 29b.

 class Address
    {
    private:
    int number;
    string name;
    string postcode;
    public:
    //getters implemented but ommited 
    };

If Address is suppose to served as an utility class (possible future use in other projects by other developers):

//QUESTION
1. For each attribute of Address given in the specification above, state whether it would be appropriate to equip the Address class with a setter method for the corresponding instance variable. Give a brief justification in each case.

Guys this is a question from my assignment so please do not question the way class Address is designed.

+5  A: 

To me that should be an immutable class, with all fields set at construction time, and getters for each.

Visage
+7  A: 

Depends on the source of Address. If, say, you read it from a database, then I wouldn't implement setters as you don't want people changing your database values without the correct permissions. If, however, you read this data in from the user, then you will have to account for the fact that users make typos and adjustments or realize they entered their old address or any of that, and you must provide for the changes.

DeadMG
+1 As far as I'm concerned you cannot possibly foresee all posible way this class will be used so restricting this class to be only immutable is like restricting the users of the class to the users who only need an immutable Address class. The rest will have to write their own mutable class.
+1  A: 

I think an address should be immutable, as an address itself cannot change. So if a person changes his address, a new object should be attached.

Not sure what "name" means here, if it is a mis-named street or the name of the person.

Eiko
A: 

Should be an immutable class in my pov, with all fields set at construction time, getter for each field.

If you really need to alter the address after construction, think about a setter for the complete set of fields to avoid modification of a single field making the objects data inconsistent. Depends completely on the use of the object.

endevour