10. Accessors

The Accessors pattern (also known as “setters and getters”) describes a technique for funneling all access to an object’s properties through well-defined and easy to recognize methods called accessors. Properties are often stored as instance variables but in some cases may be stored a different way or calculated as needed. The Accessors pattern maximizes implementation flexibility while minimizing opportunities for errors. The Accessors pattern provides the following benefits:

Implementation flexibility. Properties can be stored as instance variables or using other techniques such as the Associative Storage pattern, and the implementation can be changed without breaking other code.

Minimum maintenance burden. All uses of an ...

Get Cocoa Design Patterns now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.