]> git.proxmox.com Git - rustc.git/blob - src/doc/book/src/ch17-01-what-is-oo.md
New upstream version 1.57.0+dfsg1
[rustc.git] / src / doc / book / src / ch17-01-what-is-oo.md
1 ## Characteristics of Object-Oriented Languages
2
3 There is no consensus in the programming community about what features a
4 language must have to be considered object oriented. Rust is influenced by many
5 programming paradigms, including OOP; for example, we explored the features
6 that came from functional programming in Chapter 13. Arguably, OOP languages
7 share certain common characteristics, namely objects, encapsulation, and
8 inheritance. Let’s look at what each of those characteristics means and whether
9 Rust supports it.
10
11 ### Objects Contain Data and Behavior
12
13 The book *Design Patterns: Elements of Reusable Object-Oriented Software* by
14 Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides (Addison-Wesley
15 Professional, 1994), colloquially referred to as *The Gang of Four* book, is a
16 catalog of object-oriented design patterns. It defines OOP this way:
17
18 > Object-oriented programs are made up of objects. An *object* packages both
19 > data and the procedures that operate on that data. The procedures are
20 > typically called *methods* or *operations*.
21
22 Using this definition, Rust is object oriented: structs and enums have data,
23 and `impl` blocks provide methods on structs and enums. Even though structs and
24 enums with methods aren’t *called* objects, they provide the same
25 functionality, according to the Gang of Four’s definition of objects.
26
27 ### Encapsulation that Hides Implementation Details
28
29 Another aspect commonly associated with OOP is the idea of *encapsulation*,
30 which means that the implementation details of an object aren’t accessible to
31 code using that object. Therefore, the only way to interact with an object is
32 through its public API; code using the object shouldn’t be able to reach into
33 the object’s internals and change data or behavior directly. This enables the
34 programmer to change and refactor an object’s internals without needing to
35 change the code that uses the object.
36
37 We discussed how to control encapsulation in Chapter 7: we can use the `pub`
38 keyword to decide which modules, types, functions, and methods in our code
39 should be public, and by default everything else is private. For example, we
40 can define a struct `AveragedCollection` that has a field containing a vector
41 of `i32` values. The struct can also have a field that contains the average of
42 the values in the vector, meaning the average doesn’t have to be computed
43 on demand whenever anyone needs it. In other words, `AveragedCollection` will
44 cache the calculated average for us. Listing 17-1 has the definition of the
45 `AveragedCollection` struct:
46
47 <span class="filename">Filename: src/lib.rs</span>
48
49 ```rust,noplayground
50 {{#rustdoc_include ../listings/ch17-oop/listing-17-01/src/lib.rs}}
51 ```
52
53 <span class="caption">Listing 17-1: An `AveragedCollection` struct that
54 maintains a list of integers and the average of the items in the
55 collection</span>
56
57 The struct is marked `pub` so that other code can use it, but the fields within
58 the struct remain private. This is important in this case because we want to
59 ensure that whenever a value is added or removed from the list, the average is
60 also updated. We do this by implementing `add`, `remove`, and `average` methods
61 on the struct, as shown in Listing 17-2:
62
63 <span class="filename">Filename: src/lib.rs</span>
64
65 ```rust,noplayground
66 {{#rustdoc_include ../listings/ch17-oop/listing-17-02/src/lib.rs:here}}
67 ```
68
69 <span class="caption">Listing 17-2: Implementations of the public methods
70 `add`, `remove`, and `average` on `AveragedCollection`</span>
71
72 The public methods `add`, `remove`, and `average` are the only ways to access
73 or modify data in an instance of `AveragedCollection`. When an item is added
74 to `list` using the `add` method or removed using the `remove` method, the
75 implementations of each call the private `update_average` method that handles
76 updating the `average` field as well.
77
78 We leave the `list` and `average` fields private so there is no way for
79 external code to add or remove items to the `list` field directly; otherwise,
80 the `average` field might become out of sync when the `list` changes. The
81 `average` method returns the value in the `average` field, allowing external
82 code to read the `average` but not modify it.
83
84 Because we’ve encapsulated the implementation details of the struct
85 `AveragedCollection`, we can easily change aspects, such as the data structure,
86 in the future. For instance, we could use a `HashSet<i32>` instead of a
87 `Vec<i32>` for the `list` field. As long as the signatures of the `add`,
88 `remove`, and `average` public methods stay the same, code using
89 `AveragedCollection` wouldn’t need to change. If we made `list` public instead,
90 this wouldn’t necessarily be the case: `HashSet<i32>` and `Vec<i32>` have
91 different methods for adding and removing items, so the external code would
92 likely have to change if it were modifying `list` directly.
93
94 If encapsulation is a required aspect for a language to be considered object
95 oriented, then Rust meets that requirement. The option to use `pub` or not for
96 different parts of code enables encapsulation of implementation details.
97
98 ### Inheritance as a Type System and as Code Sharing
99
100 *Inheritance* is a mechanism whereby an object can inherit from another
101 object’s definition, thus gaining the parent object’s data and behavior without
102 you having to define them again.
103
104 If a language must have inheritance to be an object-oriented language, then
105 Rust is not one. There is no way to define a struct that inherits the parent
106 struct’s fields and method implementations. However, if you’re used to having
107 inheritance in your programming toolbox, you can use other solutions in Rust,
108 depending on your reason for reaching for inheritance in the first place.
109
110 You choose inheritance for two main reasons. One is for reuse of code: you can
111 implement particular behavior for one type, and inheritance enables you to
112 reuse that implementation for a different type. You can share Rust code using
113 default trait method implementations instead, which you saw in Listing 10-14
114 when we added a default implementation of the `summarize` method on the
115 `Summary` trait. Any type implementing the `Summary` trait would have the
116 `summarize` method available on it without any further code. This is similar to
117 a parent class having an implementation of a method and an inheriting child
118 class also having the implementation of the method. We can also override the
119 default implementation of the `summarize` method when we implement the
120 `Summary` trait, which is similar to a child class overriding the
121 implementation of a method inherited from a parent class.
122
123 The other reason to use inheritance relates to the type system: to enable a
124 child type to be used in the same places as the parent type. This is also
125 called *polymorphism*, which means that you can substitute multiple objects for
126 each other at runtime if they share certain characteristics.
127
128 > ### Polymorphism
129 >
130 > To many people, polymorphism is synonymous with inheritance. But it’s
131 > actually a more general concept that refers to code that can work with data
132 > of multiple types. For inheritance, those types are generally subclasses.
133 >
134 > Rust instead uses generics to abstract over different possible types and
135 > trait bounds to impose constraints on what those types must provide. This is
136 > sometimes called *bounded parametric polymorphism*.
137
138 Inheritance has recently fallen out of favor as a programming design solution
139 in many programming languages because it’s often at risk of sharing more code
140 than necessary. Subclasses shouldn’t always share all characteristics of their
141 parent class but will do so with inheritance. This can make a program’s design
142 less flexible. It also introduces the possibility of calling methods on
143 subclasses that don’t make sense or that cause errors because the methods don’t
144 apply to the subclass. In addition, some languages will only allow a subclass
145 to inherit from one class, further restricting the flexibility of a program’s
146 design.
147
148 For these reasons, Rust takes a different approach, using trait objects instead
149 of inheritance. Let’s look at how trait objects enable polymorphism in Rust.