4 ms·
I think in practice most OO programs end up with two general kinds of classes. Some are more for data structures, e.g. User or Address classes, and others are m
by guelo 14y ago
I think in practice most OO programs end up with two general kinds of classes. Some are more for data structures, e.g. User or Address classes, and others are more for functions like IOWriter or DAOs.
But there aren't strict dividing lines between these two types, you end up mixing and matching functions and data as you see fit to help organize things. So you might put state into your function classes, such as the DB connection in a DAO:
db = DBGetter.get("serverAddress;dbName;username;password;")
userDao = new UserDao(db)
User user = userDao.getUserById(123)
You might also end up putting functions in your data classes, but you might not. As an example, to calculate the distance between two addresses you could put the function in the Address class:
float miles = address1.distanceTo(address2)
Or you could create a DistanceCalculator class that mostly just holds functions:
float miles = distanceCalculator.distanceBetween(address1, address2)
There are pros and cons to either design, personally I would go with the second one, but the point is that you have a choice. OO provides a flexible framework and there are many ways to design your classes. That's why there are a bunch of books on OO patterns and best practices, to help design the structure of classes. You can even work in a very functional/stateless style if you want to.
What OO gives you are tools for organizing your code but you can organize in many ways, some ways that help and some that hinder. As others have said, it's just a tool.
- klawed 14y agoThe use of ORMs should/does eliminate the need to write any of the data structure classes. They still exist but they're automatically generated and updated. And the use of your second implementation allows you to separate your state from your business logic.