(2004-10-27) When To Use Classes
Kent S Johnson on when to use classes (in Python). (Object Oriented Programming) When: Several functions share state... More than one copy of the same state variables... To extend the behavior of an existing class... A callback function needs persistent state.
Related: column by Robert C Martin discussing use of public variables. A class can play two roles in Java. The first is as an object, in which case we usually make the variables private and provide a few getters and setters for the most important variables. The other role a class can play is as a Data Structure, in which case we make the variables public and provide no methods... A class that has getters and setters but no other methods isn't an object at all-it's just a data structure that someone has wasted a lot of time and effort trying to encapsulate. That encapsulation buys nothing in the end. Data structures, by definition, aren't encapsulated... Objects are encapsulated because they contain methods that change their variables. They don't have a lot of getters and setters, because the data in an object remains hidden for the most part.
Edited: | Tweet this! | Search Twitter for discussion
No backlinks!
No twinpages!