practical object-oriented design in ruby ch7

25
Sharing Role Behavior with Modules Jerry - 2015.10

Upload: jerry-chen

Post on 24-Jan-2018

247 views

Category:

Software


5 download

TRANSCRIPT

Page 1: Practical Object-Oriented Design in Ruby Ch7

Sharing Role Behavior with Modules

Jerry - 2015.10

Page 2: Practical Object-Oriented Design in Ruby Ch7

How many different ways can methods be implemented?

Page 3: Practical Object-Oriented Design in Ruby Ch7

How many different ways can methods be implemented?

• Those it implements

Page 4: Practical Object-Oriented Design in Ruby Ch7

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

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

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

Some problems require sharing behavior among otherwise

unrelated objects.

Page 8: Practical Object-Oriented Design in Ruby Ch7
Page 9: Practical Object-Oriented Design in Ruby Ch7

Modules allow objects of different classes to play a

common role.

Page 10: Practical Object-Oriented Design in Ruby Ch7

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
Page 12: Practical Object-Oriented Design in Ruby Ch7
Page 13: Practical Object-Oriented Design in Ruby Ch7
Page 14: Practical Object-Oriented Design in Ruby Ch7

Remove dependency

Page 15: Practical Object-Oriented Design in Ruby Ch7

str.empty?

or

StingUtils.empty?(str)

Page 16: Practical Object-Oriented Design in Ruby Ch7
Page 17: Practical Object-Oriented Design in Ruby Ch7

Let objects speak for themselves

Page 18: Practical Object-Oriented Design in Ruby Ch7

Extracting the Abstraction

Page 19: Practical Object-Oriented Design in Ruby Ch7

Looking up methods

Page 20: Practical Object-Oriented Design in Ruby Ch7

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

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

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

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

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

Create Shallow Hierarchies