Quote:
Originally Posted by studentlost
I had a question about abstraction in Objective C regarding good coding practice.
I'm going to ask my question by using a clear example I made.
Lets say that I want to make an application involving amino acids. Each amino acid interacts differently with other amino acids. A very primitive way would be to create a separate class for each amino acid. This would take more time and make the code inefficient.
My question is, in this case, how would you create the classes? Would you create a class for amino acids and then another class that involves their interactions? Or would you create a class for amino acids and subclass each amino acid in particular? Or if there are better ways that I haven't touched on.
Please let me know your thoughts and ideas.
Thank you.
|
It depends on the specifics.
It often does make sense to have a common base class that defines the common characteristics of all the objects in the class.
It might also make sense to further break them down into sub-groups with particular traits - say the sulphur containing amino acids in their own group. I only took one year of Chemistry, and that was in High school. No organic bio, so I don't have a lot of depth to draw from on this particular idea. Plus, it was over 30 years ago, so I'm a little rusty.
The classic teaching example for object-oriented programning is an object-oriented drawing program.
It manages objects. Objects have size and position, and have a draw method. They all have a bounding box that you can use to drag them around.
At the very high level, there are raster objects and vector objects.
All vector objects have a line color and thickness, and a fill color.
Among vector objects, there are curved shapes and polygons.
Among curved shapes, there's ellipses, and a subclass circles.
Then there's polygons. There's convex and concave polygons. Among convex polygons, there's quadrilaterals, triangles, and a whole host of n-gons. Within quadrilaterals, there's parallelograms, trapezoids, and rectangles. Rectangles could be a subclass of parallelograms. Squares could be a subclass of rectangles.