this quick question may be very easy. What's generally the best approach in object oriented programming? When you start a project, do you base your classes around nouns or verbs?
Let's say i want make a program that connects to a database, perform a query, get the results, print them. What's the best way to build the classes around this? I currently have two possible approaches:
- Wrap classes around a variable/object (nouns). I send messages to the class to manipulate and interact with the variable/object inside: I make a Connection object, and write public methods to Connect, Disconnect, Prepare a query, etc. The last one returns me a Statement object: i write public methods to interact in any way with the Statement object: Bind, Execute, etc.
- Build classes based on actions i want to perform on variable/objects (verbs), where each class has only one job: I make a ConnectER that connects to the database, and i pass it to a PreparER that prepares the query, which gets passed to a BindER that binds the parameters, that gets passed to an ExecutER that executes the query, and so on.
The first one is the approach i think you usually see in existing libraries. The second one more strictly adheres to the Single Responsibility Principle found in SOLID, since each class does one job.
I think this question is at the base of good programming, at a lower level than Object Oriented Design Patterns even, and could make a difference between well-structured and unmanageable projects
Thank you
Aucun commentaire:
Enregistrer un commentaire