Wiki source code of Code Example: Data Structure Style vs. Object Style
Show last authors
author | version | line-number | content |
---|---|---|---|
1 | ### Use Cases | ||
2 | |||
3 | This example is intended to demonstrate the extensibility differences between data structures and objects by extending them with | ||
4 | |||
5 | 1. an Area() function (extending behavior) | ||
6 | 1. a rectangle type (extending data type) | ||
7 | |||
8 | ### Data Structure Style | ||
9 | |||
10 | interface GeometricObject {} | ||
11 | |||
12 | class Square implements GeometricObject { | ||
13 | length float | ||
14 | } | ||
15 | |||
16 | class Circle implements GeometricObject { | ||
17 | radius float | ||
18 | } | ||
19 | |||
20 | function Circumference(g GeometricObject) float { | ||
21 | switch type(g): | ||
22 | case Square: | ||
23 | return 4 * g.length | ||
24 | case Circle: | ||
25 | return 2 * PI * g.radius | ||
26 | } | ||
27 | |||
28 | 1. Adding an Area() function with a very similar anatomy to Circumference() is easy because it only requires adding new code. | ||
29 | 1. Adding a new datatype, Rectangle, is harder because it requires touching existing code, namely any functions like Circumference() or Area() that need to be enabled handle this datatype. | ||
30 | |||
31 | #### | ||
32 | |||
33 | #### Object-Oriented Style | ||
34 | |||
35 | interface GeometricObject { | ||
36 | Circumference() float | ||
37 | } | ||
38 | |||
39 | class Square implements GeometricObject { | ||
40 | length float | ||
41 | |||
42 | constructor(length float) { | ||
43 | this.length = length | ||
44 | } | ||
45 | |||
46 | func Circumference() float { | ||
47 | return 4 * this.length | ||
48 | } | ||
49 | } | ||
50 | |||
51 | class Circle implements GeometricObject { | ||
52 | radius float | ||
53 | |||
54 | constructor(radius float) { | ||
55 | this.radius = radius | ||
56 | } | ||
57 | |||
58 | func Circumference() float { | ||
59 | return 2 * PI * this.radius | ||
60 | } | ||
61 | } | ||
62 | |||
63 | 1. Adding a function Area() to the GeometricObject interface is hard because it requires touching existing code, namely extending all Square and Circle classes with the new function. | ||
64 | 1. Adding a new datatype, Rectangle, is easy because only new code is added, namely a Rectangle class. | ||
65 | |||
66 | ### Conclusion | ||
67 | |||
68 | * The lesson learned here is that data structures are easy to extend with functions, and objects are easy to extend with data types. | ||
69 | * Although the difference between the two styles may seem insignificant in this simplified example, it has serious implications in complex production codebases with multiple data types and behaviors. |