質問

I am learning Objective-C, and as one of the lessons, we create a class Employee, and its superclass Person, along with an Asset class, whose instances are owned by Employee. To test the classes, and their instance functions, I wrote the following code

int main(int argc, const char *argv[]){
@autoreleasepool{
    NSMutableArray *employees = [[NSMutableArray alloc] init];

    for (int i = 0; i < 10; i++){
        Employee *person = [[Employee alloc]init];
        [person setWeightInKilos:90 + i];
        [person setHeightInMeters:1.8 - i/10.00];
        [person setEmployeeID:i];

        [employees addObject:person];
    }

    .../*code continues from here, to assign [person]
          's  at random indexes in *employees, instances of the Asset class */
  }
} 

I was wondering what are the benefits or disadvantages to setting the [person] with one method, which in turn calls the other methods, like so:

main.m:  
....
for (int i = 0; i < 10; i++){
            Employee *person = [[Employee alloc]init];
            [person setWeightInKilos:90 + i withAHeightOf:1.8 - i/10.00 andId:i];
            [employees addObject:person];
        }
....

Thanks.

役に立ちましたか?

解決

Basically there isn't any right and wrong here, objective-c syntax and coding conventions allow you to have long messages with multiple parameters.

There are a couple of options here:

  1. Setting these parameters on init (e.g. initWithId: height: weight:)
  2. Creating a property for each of them
  3. Creating an individual function for each of them
  4. Creating a setProperties function

To decide this you should first ask yourself:

  1. Does these triplets (Height, Weight, Id) belong together logically?
  2. Does it make sense to set just one of them?
  3. Does it makes sense that this object is missing these values?
  4. Is there a need to make setting these parameters an atomic action?
  5. Does Immutability of the object matters?

Most of the time I choose to make my object immutable, which means I create a readonly property for them, and set them via init function.

他のヒント

Your Person class has several properties including weightInKilos, heightInMeters, and employeeID.

You can, of course, set each property individually. As a convenience, you can provide special "helper" methods that make it easier to set several properties in one call. This is completely optional. Whether you choose to add these extra methods is strictly a matter of personal taste and style.

One consideration for such "helper" methods is where to draw the line. Which properties should be parameters to such methods? What happens if you later add another property? Do you update the "helper" methods to take another parameter and then update all code calling those methods?

In many cases it's better to just set each one individually. One guideline I use for deciding on whether to create such a "helper" method is if the object requires a certain set of properties to be set to be valid. If there is such a case then I usually create a special init method that takes the required properties as parameters. Then all of the optional ones can be set individually.

You neglected to show how you've defined the Employee object. Are weight, height and identification held as properties? If so, they generally will have accessor methods automatically generated for them so you will not need to write those routines. Properties have nice conveniences associated with them.

As to why you would use one method over another, the most compelling reason is a matter of personal style. Something to consider -- what happens if you choose to change the stored weight of a person; do you have a method to set only that parameter, or do you need to set everything about them?

It's common to have an initializer that receives the information to be initialized about an object, but something like - (id)initWithWeight:andHeight:andId:andSomethingElse:andMoreStuff: gets ugly far sooner than I've stretched it out. To solve this, the NSDictionary class can be helpful, where the dictionary contains all the fields you choose to include and the method becomes - (id)initUsingDictionary:(NSDictionary *)dictionary;. A nice benefit to this is you can easily make fields optional and a single method signature handles it.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top