Practical Object-Oriented Design in Ruby Ch7
-
Upload
jerry-chen -
Category
Software
-
view
249 -
download
5
Transcript of Practical Object-Oriented Design in Ruby Ch7
![Page 1: Practical Object-Oriented Design in Ruby Ch7](https://reader033.fdocument.pub/reader033/viewer/2022051503/5a685cef7f8b9a45728b5ee7/html5/thumbnails/1.jpg)
Sharing Role Behavior with Modules
Jerry - 2015.10
![Page 2: Practical Object-Oriented Design in Ruby Ch7](https://reader033.fdocument.pub/reader033/viewer/2022051503/5a685cef7f8b9a45728b5ee7/html5/thumbnails/2.jpg)
How many different ways can methods be implemented?
![Page 3: Practical Object-Oriented Design in Ruby Ch7](https://reader033.fdocument.pub/reader033/viewer/2022051503/5a685cef7f8b9a45728b5ee7/html5/thumbnails/3.jpg)
How many different ways can methods be implemented?
• Those it implements
![Page 4: Practical Object-Oriented Design in Ruby Ch7](https://reader033.fdocument.pub/reader033/viewer/2022051503/5a685cef7f8b9a45728b5ee7/html5/thumbnails/4.jpg)
How many different ways can methods be implemented?
• Those it implements
• Those implemented in all objects above it in the hierarchy
![Page 5: Practical Object-Oriented Design in Ruby Ch7](https://reader033.fdocument.pub/reader033/viewer/2022051503/5a685cef7f8b9a45728b5ee7/html5/thumbnails/5.jpg)
How many different ways can methods be implemented?
• Those it implements
• Those implemented in all objects above it in the hierarchy
• Those implemented in any module that has been added to it
![Page 6: Practical Object-Oriented Design in Ruby Ch7](https://reader033.fdocument.pub/reader033/viewer/2022051503/5a685cef7f8b9a45728b5ee7/html5/thumbnails/6.jpg)
How many different ways can methods be implemented?
• Those it implements
• Those implemented in all objects above it in the hierarchy
• Those implemented in any module that has been added to it
• Those implemented in all modules added to any object above it in the hierarchy
![Page 7: Practical Object-Oriented Design in Ruby Ch7](https://reader033.fdocument.pub/reader033/viewer/2022051503/5a685cef7f8b9a45728b5ee7/html5/thumbnails/7.jpg)
Some problems require sharing behavior among otherwise
unrelated objects.
![Page 8: Practical Object-Oriented Design in Ruby Ch7](https://reader033.fdocument.pub/reader033/viewer/2022051503/5a685cef7f8b9a45728b5ee7/html5/thumbnails/8.jpg)
![Page 9: Practical Object-Oriented Design in Ruby Ch7](https://reader033.fdocument.pub/reader033/viewer/2022051503/5a685cef7f8b9a45728b5ee7/html5/thumbnails/9.jpg)
Modules allow objects of different classes to play a
common role.
![Page 10: Practical Object-Oriented Design in Ruby Ch7](https://reader033.fdocument.pub/reader033/viewer/2022051503/5a685cef7f8b9a45728b5ee7/html5/thumbnails/10.jpg)
Trip Scheduling Problem
• Trip involve bicycles, mechanics, and motor vehicles
• Need to know whether bicycles/mechanics/vehicles are available for trip or not
• Downtime for bicycles are 1 day between trips, vehicles 3 days, and mechanics 4 days.
![Page 11: Practical Object-Oriented Design in Ruby Ch7](https://reader033.fdocument.pub/reader033/viewer/2022051503/5a685cef7f8b9a45728b5ee7/html5/thumbnails/11.jpg)
![Page 12: Practical Object-Oriented Design in Ruby Ch7](https://reader033.fdocument.pub/reader033/viewer/2022051503/5a685cef7f8b9a45728b5ee7/html5/thumbnails/12.jpg)
![Page 13: Practical Object-Oriented Design in Ruby Ch7](https://reader033.fdocument.pub/reader033/viewer/2022051503/5a685cef7f8b9a45728b5ee7/html5/thumbnails/13.jpg)
![Page 14: Practical Object-Oriented Design in Ruby Ch7](https://reader033.fdocument.pub/reader033/viewer/2022051503/5a685cef7f8b9a45728b5ee7/html5/thumbnails/14.jpg)
Remove dependency
![Page 15: Practical Object-Oriented Design in Ruby Ch7](https://reader033.fdocument.pub/reader033/viewer/2022051503/5a685cef7f8b9a45728b5ee7/html5/thumbnails/15.jpg)
str.empty?
or
StingUtils.empty?(str)
![Page 16: Practical Object-Oriented Design in Ruby Ch7](https://reader033.fdocument.pub/reader033/viewer/2022051503/5a685cef7f8b9a45728b5ee7/html5/thumbnails/16.jpg)
![Page 17: Practical Object-Oriented Design in Ruby Ch7](https://reader033.fdocument.pub/reader033/viewer/2022051503/5a685cef7f8b9a45728b5ee7/html5/thumbnails/17.jpg)
Let objects speak for themselves
![Page 18: Practical Object-Oriented Design in Ruby Ch7](https://reader033.fdocument.pub/reader033/viewer/2022051503/5a685cef7f8b9a45728b5ee7/html5/thumbnails/18.jpg)
Extracting the Abstraction
![Page 19: Practical Object-Oriented Design in Ruby Ch7](https://reader033.fdocument.pub/reader033/viewer/2022051503/5a685cef7f8b9a45728b5ee7/html5/thumbnails/19.jpg)
Looking up methods
![Page 20: Practical Object-Oriented Design in Ruby Ch7](https://reader033.fdocument.pub/reader033/viewer/2022051503/5a685cef7f8b9a45728b5ee7/html5/thumbnails/20.jpg)
Recognise the Antipatterns
• If using variables like type or category, look to classical inheritance.
• If checking the class of receiving objects to determine which message to send, duck typing.
![Page 21: Practical Object-Oriented Design in Ruby Ch7](https://reader033.fdocument.pub/reader033/viewer/2022051503/5a685cef7f8b9a45728b5ee7/html5/thumbnails/21.jpg)
Insist on the Abstraction
• Superclasses should not contain code that applies to some, but not all, subclasses.
• When subclasses override a method to declare that they do not do that thing they close to declaring that they are not that thing.
• This restriction also applies to modules
![Page 22: Practical Object-Oriented Design in Ruby Ch7](https://reader033.fdocument.pub/reader033/viewer/2022051503/5a685cef7f8b9a45728b5ee7/html5/thumbnails/22.jpg)
Honor the Contract
• subclasses agree to a contract, promising to be substitutable for their superclasses. Maintain that substitutability.
• Is a Square a Rectangle?
![Page 23: Practical Object-Oriented Design in Ruby Ch7](https://reader033.fdocument.pub/reader033/viewer/2022051503/5a685cef7f8b9a45728b5ee7/html5/thumbnails/23.jpg)
Template Method Pattern
• The abstract code defines the algorithms and
• The concrete inheritors overriding these template methods.
![Page 24: Practical Object-Oriented Design in Ruby Ch7](https://reader033.fdocument.pub/reader033/viewer/2022051503/5a685cef7f8b9a45728b5ee7/html5/thumbnails/24.jpg)
Preemptively Decouple Classes
• Avoid writing code that requires its inheritors to send super
• Use hook messages.
![Page 25: Practical Object-Oriented Design in Ruby Ch7](https://reader033.fdocument.pub/reader033/viewer/2022051503/5a685cef7f8b9a45728b5ee7/html5/thumbnails/25.jpg)
Create Shallow Hierarchies