|
| 1 | +<!-- .slide: data-background="#111111" --> |
| 2 | + |
| 3 | +# Programowanie obiektowe |
| 4 | + |
| 5 | +## Dziedziczenie |
| 6 | + |
| 7 | +<a href="https://coders.school"> |
| 8 | + <img width="500" src="../coders_school_logo.png" alt="Coders School" class="plain"> |
| 9 | +</a> |
| 10 | + |
| 11 | +___ |
| 12 | + |
| 13 | +## Wprowadzenie do dziedziczenia |
| 14 | + |
| 15 | +Podczas implementacji klas, często możemy zauważyć, że część cech składowych klasy można wykorzystać także w innych klasach. |
| 16 | +<!-- .element: class="fragment fade-in" --> |
| 17 | + |
| 18 | +Weźmy pod lupę klasę `Computer`. Jeżeli chcielibyśmy utworzyć klasy: `Laptop`, `PC`, `Tablet`, to część metod oraz składowych klasy musielibyśmy powielić. |
| 19 | +<!-- .element: class="fragment fade-in" --> |
| 20 | + |
| 21 | +___ |
| 22 | + |
| 23 | +## `class Computer` |
| 24 | + |
| 25 | +```cpp |
| 26 | +class Computer { |
| 27 | +public: |
| 28 | + void turnOn(); |
| 29 | + void powerOff(); |
| 30 | + void restart(); |
| 31 | + |
| 32 | +private: |
| 33 | + Processor processor_; |
| 34 | + Drive drive_; |
| 35 | + Motherboard motherboard_; |
| 36 | + GraphicsCard graphics_card_; |
| 37 | + Memory memory_; |
| 38 | +}; |
| 39 | +``` |
| 40 | +
|
| 41 | +___ |
| 42 | +
|
| 43 | +## `class Laptop` |
| 44 | +
|
| 45 | +```cpp |
| 46 | +class Laptop { |
| 47 | +public: |
| 48 | + void turnOn(); |
| 49 | + void powerOff(); |
| 50 | + void restart(); |
| 51 | + void display(); |
| 52 | + void getUserInput(); |
| 53 | +
|
| 54 | +private: |
| 55 | + Processor processor_; |
| 56 | + Drive drive_; |
| 57 | + Motherboard motherboard_; |
| 58 | + GraphicsCard graphics_card_; |
| 59 | + Memory memory_; |
| 60 | + Screen screen_; |
| 61 | + Keyboard keyboard_; |
| 62 | +}; |
| 63 | +``` |
| 64 | + |
| 65 | +___ |
| 66 | + |
| 67 | +## `class Tablet` |
| 68 | + |
| 69 | +```cpp |
| 70 | +class Tablet { |
| 71 | +public: |
| 72 | + void turnOn(); |
| 73 | + void powerOff(); |
| 74 | + void restart(); |
| 75 | + void display(); |
| 76 | + void getUserInput(); |
| 77 | + |
| 78 | +private: |
| 79 | + Processor processor_; |
| 80 | + Drive drive_; |
| 81 | + Motherboard motherboard_; |
| 82 | + GraphicsCard graphics_card_; |
| 83 | + Memory memory_; |
| 84 | + Screen screen_; |
| 85 | +}; |
| 86 | +``` |
| 87 | +
|
| 88 | +___ |
| 89 | +<!-- .slide: style="font-size: 0.9em" --> |
| 90 | +
|
| 91 | +## Jak uprościć strukturę naszego programu? |
| 92 | +
|
| 93 | +```cpp |
| 94 | +class Computer { |
| 95 | +public: |
| 96 | + void turnOn(); |
| 97 | + void powerOff(); |
| 98 | + void restart(); |
| 99 | +
|
| 100 | +protected: |
| 101 | + Processor processor_; |
| 102 | + Drive drive_; |
| 103 | + Motherboard motherboard_; |
| 104 | + GraphicsCard graphics_card_; |
| 105 | + Memory memory_; |
| 106 | +}; |
| 107 | +
|
| 108 | +
|
| 109 | +class Laptop : public Computer { |
| 110 | +public: |
| 111 | + void display(); |
| 112 | + void getUserInput(); |
| 113 | +
|
| 114 | +private: |
| 115 | + Screen screen_; |
| 116 | + Keyboard keyboard_; |
| 117 | +}; |
| 118 | +
|
| 119 | +
|
| 120 | +class Tablet : public Computer { |
| 121 | +public: |
| 122 | + void display(); |
| 123 | + void getUserInput(); |
| 124 | +
|
| 125 | +private: |
| 126 | + Screen screen_; |
| 127 | +}; |
| 128 | +``` |
| 129 | +<!-- .element: class="fragment fade-in" --> |
| 130 | + |
| 131 | +___ |
| 132 | + |
| 133 | +## Klasy bazowe i pochodne |
| 134 | + |
| 135 | +Klasa, po której dziedziczymy, nazywają się <span class="fragment highlight-green">**klasą bazową (base class)**</span>. |
| 136 | +<!-- .element: class="fragment fade-in" --> |
| 137 | + |
| 138 | +Klasa, która dziedziczy nazywa się <span class="fragment highlight-green">**klasą pochodną (derived class)**</span>. |
| 139 | +<!-- .element: class="fragment fade-in" --> |
| 140 | + |
| 141 | +Inaczej, klasa, po której dziedziczymy to rodzic (parent class). |
| 142 | +<!-- .element: class="fragment fade-in" --> |
| 143 | + |
| 144 | +Klasa, która dziedziczy to dziecko (child class). |
| 145 | +<!-- .element: class="fragment fade-in" --> |
| 146 | + |
| 147 | +___ |
| 148 | + |
| 149 | +### Co z metodami klas `Laptop` i `Tablet`? |
| 150 | + |
| 151 | +#### Czy można wydzielić kolejną klasę? |
| 152 | +<!-- .element: class="fragment fade-in" --> |
| 153 | + |
| 154 | +```cpp |
| 155 | +void display(); |
| 156 | +void getUserInput(); |
| 157 | +``` |
| 158 | +<!-- .element: class="fragment fade-in" --> |
| 159 | + |
| 160 | +___ |
| 161 | + |
| 162 | +## Klasa `Screen` i `TouchScreen` |
| 163 | + |
| 164 | +Załóżmy, że dodajemy klasę `Screen`. Klasa ta wyświetla na bieżąco interfejs użytkownika. |
| 165 | +<!-- .element: class="fragment fade-in" --> |
| 166 | + |
| 167 | +Chcemy też stworzyć klasę reprezentującą ekran dotykowy - `TouchScreen`, który również umożliwia odczyt akcji od użytkownika i ich wyświetlanie. |
| 168 | +<!-- .element: class="fragment fade-in" --> |
| 169 | + |
| 170 | +<div class="multicolumn"> |
| 171 | +<div class="col"> |
| 172 | + |
| 173 | +```cpp |
| 174 | +class Screen { |
| 175 | +public: |
| 176 | + void display(); |
| 177 | + |
| 178 | +private: |
| 179 | + void process(); |
| 180 | + |
| 181 | + Monitor monitor_; |
| 182 | +}; |
| 183 | +``` |
| 184 | +<!-- .element: class="fragment fade-in" --> |
| 185 | +
|
| 186 | +</div> |
| 187 | +<div class="col"> |
| 188 | +
|
| 189 | +```cpp |
| 190 | +class TouchScreen { |
| 191 | +public: |
| 192 | + void display(); |
| 193 | + void getUserInput(); |
| 194 | +
|
| 195 | +private: |
| 196 | + void process(); |
| 197 | + void displayKeyboard(); |
| 198 | +
|
| 199 | + Monitor monitor_; |
| 200 | +}; |
| 201 | +``` |
| 202 | +<!-- .element: class="fragment fade-in" --> |
| 203 | + |
| 204 | +</div> |
| 205 | +</div> |
| 206 | + |
| 207 | +### Jak uprościć powyższy kod? |
| 208 | +<!-- .element: class="fragment fade-in" --> |
| 209 | + |
| 210 | +___ |
| 211 | + |
| 212 | +## Wykorzystanie dziedziczenia do uproszczenia kodu |
| 213 | + |
| 214 | +```cpp |
| 215 | +class Screen { |
| 216 | +public: |
| 217 | + void display(); |
| 218 | + |
| 219 | +private: |
| 220 | + void process(); |
| 221 | + |
| 222 | + Monitor monitor_; |
| 223 | +}; |
| 224 | +``` |
| 225 | +<!-- .element: class="fragment fade-in" --> |
| 226 | +
|
| 227 | +```cpp |
| 228 | +class TouchScreen : public Screen { |
| 229 | +public: |
| 230 | + void getUserInput(); |
| 231 | +
|
| 232 | +private: |
| 233 | + void displayKeyboard(); |
| 234 | +}; |
| 235 | +``` |
| 236 | +<!-- .element: class="fragment fade-in" --> |
| 237 | + |
| 238 | +___ |
| 239 | + |
| 240 | +## Wielodziedziczenie |
| 241 | + |
| 242 | +```cpp |
| 243 | +class Screen { |
| 244 | +public: |
| 245 | + void display(); |
| 246 | + |
| 247 | +private: |
| 248 | + void process(); |
| 249 | + |
| 250 | + Monitor monitor_; |
| 251 | +}; |
| 252 | + |
| 253 | +class TouchScreen : public Screen { |
| 254 | +public: |
| 255 | + void getUserInput(); |
| 256 | + |
| 257 | +private: |
| 258 | + void displayKeyboard(); |
| 259 | +}; |
| 260 | + |
| 261 | +class Computer { |
| 262 | +public: |
| 263 | + void turnOn(); |
| 264 | + void powerOff(); |
| 265 | + void restart(); |
| 266 | + |
| 267 | +protected: |
| 268 | + Processor processor_; |
| 269 | + Drive drive_; |
| 270 | + Motherboard motherboard_; |
| 271 | + GraphicsCard graphics_card_; |
| 272 | + Memory memory_; |
| 273 | +}; |
| 274 | + |
| 275 | +class Laptop : public Computer, |
| 276 | + public Screen { |
| 277 | + Keyboard keyboard_; |
| 278 | +}; |
| 279 | + |
| 280 | +class Tablet : public Computer, |
| 281 | + public TouchScreen { |
| 282 | +}; |
| 283 | +``` |
| 284 | +
|
| 285 | +___ |
| 286 | +
|
| 287 | +## Wielodziedziczenie - disclaimer |
| 288 | +
|
| 289 | +Wielodziedziczenie to dziedziczenie z kliku klas bazowych. |
| 290 | +
|
| 291 | +Wybór implementacji zależy od programisty. |
| 292 | +<!-- .element: class="fragment fade-in" --> |
| 293 | +
|
| 294 | +Nie zawsze wielodziedziczenie będzie najlepszym rozwiązaniem. |
| 295 | +<!-- .element: class="fragment fade-in" --> |
| 296 | +
|
| 297 | +Należy się zawsze zastanowić czy dziedziczenie po konkretnej klasie uprości nam program i czy nie będzie powodować żadnych komplikacji w dalszym procesie rozbudowy naszego programu. |
| 298 | +<!-- .element: class="fragment fade-in" --> |
| 299 | +
|
| 300 | +Najwyżej trzeba będzie zrefaktoryzować program ;) |
| 301 | +<!-- .element: class="fragment fade-in" --> |
| 302 | +
|
| 303 | +___ |
| 304 | +
|
| 305 | +## Dziedziczenie - problemy |
| 306 | +
|
| 307 | +```cpp |
| 308 | +struct Bird { |
| 309 | + fly(); |
| 310 | + makeSound(); |
| 311 | +}; |
| 312 | +
|
| 313 | +struct Penguin { |
| 314 | + swim(); |
| 315 | + makeSound(); |
| 316 | +}; |
| 317 | +
|
| 318 | +// Hummingbird is the type of bird which makes so little sound so it |
| 319 | +// can be said that it makes no sound. |
| 320 | +struct Hummingbird { |
| 321 | + fly(); |
| 322 | +}; |
| 323 | +``` |
| 324 | + |
| 325 | +___ |
| 326 | +<!-- .slide: style="font-size: 0.9em" --> |
| 327 | + |
| 328 | +## Dziedziczenie - zasada LSP |
| 329 | + |
| 330 | +Jeżeli spróbujemy teraz uprościć klasę poprzez dziedziczenie pojawi się problem: |
| 331 | + |
| 332 | +```cpp |
| 333 | +struct Bird { |
| 334 | + fly(); |
| 335 | + makeSound(); |
| 336 | +}; |
| 337 | + |
| 338 | +struct Penguin : public Bird { |
| 339 | + fly(); // But I can't fly! |
| 340 | + swim(); |
| 341 | + makeSound(); |
| 342 | +}; |
| 343 | + |
| 344 | +struct Hummingbird : public Bird { |
| 345 | + fly(); |
| 346 | + makeSound(); // But I don't make sound! |
| 347 | +}; |
| 348 | +``` |
| 349 | +
|
| 350 | +Jeszcze bardziej utrudnimy sytuację, gdy w przyszłości dodamy sobie kolejne klasy jak Struś. Zawsze przed implementacją musimy się zastanowić jak podzielić odpowiedzialność na poszczególne klasy, aby |
| 351 | +uniknąć podobnych problemów. |
| 352 | +
|
| 353 | +___ |
| 354 | +
|
| 355 | +### Dla ciekawskich |
| 356 | +
|
| 357 | +Poczytajcie o zasadzie Liskov Substitution Principle (LSP). Mówi ona jak powinno / nie powinno się projektować kodu obiektowego. Ta zasada została złamana w ostatnim przykładzie. |
| 358 | +
|
| 359 | +Możecie też poczytać o wszystkich zasadach SOLID. |
0 commit comments