I came across the analogy of the class being a cookie cutter and the cookies being objects while reading Code Complete. I fail to see why this analogy was drawn.How is it related to the concept of objects being run time instances of their classes?
-
5A cookie cutter defines the shape and size of cookies. A class defines its objects.– yannisCommented Mar 2, 2013 at 15:40
-
1As Yannis says, the class describes the "shape", while the object is an actual thing you created from it. That said it's (my humble opinion) one of the worst and most misleading ways to describe OO concepts.– thorsten müllerCommented Mar 2, 2013 at 15:47
-
@thorstenmüller yes I share the same feelings .This is why I posted this question.– GeekCommented Mar 2, 2013 at 15:52
1 Answer
The analogy is a bad one, because it can convey the essence of classes and objects if you apply the right mental mapping, but that mapping is not obvious to arrive at, and perhaps not even the most convincing one.
A class is like a cookie-cutter in that it has the power to create multiple items that are alike in some ways: they all have the same number and type of fields, while their values can differ. You can compare this to all cookies having the same shape, but it is unclear how the differences between individual cookies map to the differences between objects that are class-mates; in my mind, two Cartesian coordinates with different X and Y values are much more different than similar, certainly much less alike than two cookies with the same shape.
-
2Still, two Cartesian coordinates are much more similar then a Cartesian coordinate and a cookie. Bad analogy is good enough.– scrwtpCommented Mar 2, 2013 at 20:45
-
It's not a good analogy because the cookie cutter is as physical of an item as the cookies it produces. I prefer to compare a class to a dictionary entry- it is more conceptual and tells you what a cookie is through text. The actual cookies would comply with the definition that is given.– Chris CCommented Mar 4, 2013 at 22:50