OOD - Object orientated design

RubertoPaulo 179 views 43 slides Feb 27, 2017
Slide 1
Slide 1 of 52
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43
Slide 44
44
Slide 45
45
Slide 46
46
Slide 47
47
Slide 48
48
Slide 49
49
Slide 50
50
Slide 51
51
Slide 52
52

About This Presentation

This is my slides that I used within the last backend developers meetup - https://www.meetup.com/Cape-Town-Backend-Developers/

This presentation covers sections from Sandi Metz's - Practical Object-Oriented Design in ruby. This presentation comes with a small workshop that focuses on refactori...


Slide Content

OO D

We want to do our best work.

We want our work to have meaning

We want to have fun along the way

Two major themes being Design and Messages over objects 

Design 

Failures of OOD might look like failures of coding technique but they are actually failures of perspective . 

 Source: Pablo Picasso - Buffalo Art

Design is not an assembly line where similarly trained workers construct identical widgets; it’s a studio where like-minded artists sculpt custom applications. Design is thus an art, the art of arranging code. 

How does Design affect us? 

 Per spec tive.

Unfortunately, something will change. 

The customers didn’t know what they wanted. 

 They didn’t say what they meant.

You didn’t understand their needs. 

You’ve learned how to do something better. 

Even applications that are perfect in every way are not stable. 

The application was a huge success, now everyone wants more. 

Change is unavoidable . It is ubiquitous , omnipresent , and inevitable . 

 Source: http://more-sky.com/

Dependency 

Object-oriented design is about managing dependencies. 

 1 class Gear 2 attr_reader :chainring , :cog , :rim , :tire 3 def initialize( chainring , cog , rim , tire ) 4 @chainring = chainring 5 @cog = cog 6 @rim = rim 7 @tire = tire 8 end 9 10 def gear_inches 11 ratio * Wheel .new( rim , tire ). diameter 12 end 13 # ... 14 end 15 16 Gear .new( 52 , 11 , 26 , 1.5 ). gear_inches

 1 class Gear 2 attr_reader :chainring , :cog , :rim , :tire 3 def initialize( chainring , cog , rim , tire ) 4 @chainring = chainring 5 @cog = cog 6 @rim = rim 7 @tire = tire 8 end 9 10 def gear_inches 11 ratio * Wheel .new( rim , tire ). diameter 12 end 13 # ... 14 end 15 16 Gear .new( 52 , 11 , 26 , 1.5 ). gear_inches

 1 class Gear 2 attr_reader :chainring , :cog , :rim , :tire 3 def initialize( chainring , cog , rim , tire ) 4 @chainring = chainring 5 @cog = cog 6 @rim = rim 7 @tire = tire 8 end 9 10 def gear_inches 11 ratio * Wheel .new( rim , tire ). diameter 12 end 13 # ... 14 end 15 16 Gear .new( 52 , 11 , 26 , 1.5 ). gear_inches

 1 class Gear 2 attr_reader :chainring , :cog , :rim , :tire 3 def initialize( chainring , cog , rim , tire ) 4 @chainring = chainring 5 @cog = cog 6 @rim = rim 7 @tire = tire 8 end 9 10 def gear_inches 11 ratio * Wheel .new( rim , tire ). diameter 12 end 13 # ... 14 end 15 16 Gear .new( 52 , 11 , 26 , 1.5 ). gear_inches

 1 class Gear 2 attr_reader :chainring , :cog , :rim , :tire 3 def initialize( chainring , cog , rim , tire ) 4 @chainring = chainring 5 @cog = cog 6 @rim = rim 7 @tire = tire 8 end 9 10 def gear_inches 11 ratio * Wheel .new( rim , tire ). diameter 12 end 13 # ... 14 end 15 16 Gear .new( 52 , 11 , 26 , 1.5 ). gear_inches

Your goal is to model your application, using classes, such that it does what it is supposed to do right now and is also easy to change later . 

 1 class Gear 2 attr_reader :chainring , :cog , :rim , :tire 3 def initialize( chainring , cog , rim , tire ) 4 @chainring = chainring 5 @cog = cog 6 @rim = rim 7 @tire = tire 8 end 9 10 def gear_inches 11 ratio * Wheel .new( rim , tire ). diameter 12 end 13 # ... 14 end 15 16 Gear .new( 52 , 11 , 26 , 1.5 ). gear_inches

 Dependency injection

 1 class Gear 2 attr_reader :chainring , :cog , :wheel 3 def initialize( chainring , cog , wheel ) 4 @chainring = chainring 5 @cog = cog 6 @wheel = wheel 8 end 9 10 def gear_inches 11 ratio * wheel. diameter 12 end 13 # ... 14 end 15 16 Gear .new( 52 , 11 , Wheel.new( 26 , 1.5 )). gear_inches

 Dependency aversion

 1 class Gear 2 attr_reader :chainring , :cog , :wheel 3 def initialize( chainring , cog , rim , tire ) 4 @chainring = chainring 5 @cog = cog 6 @wheel = Wheel.new( rim , tire ) 8 end 9 10 def gear_inches 11 ratio * wheel. diameter 12 end 13 # ... 14 end 15 16 Gear .new( 52 , 11 , 26 , 1.5 ). gear_inches

 Order Dependency

1 class Gear 2 attr_reader :chainring , :cog , :tire 3 def initialize( args ) 4 @chainring = args[: chainring ] 5 @cog = args[: cog ] 6 @wheel = args[: wheel ] 7 end 8 9 def gear_inches 10 ratio * wheel.diameter 11 end 12 # ... 13 end 14 15 Gear .new( 16 :cog => 11 , 17 :chainring => 52 , 18 :wheel => Wheel .new( 26 , 1.5 )). gear_inches

Is your code too coupled to your tests? 

Tools for design 

S ingle Responsibility, O pen-Closed, L iskov Substitution, I nterface Segregation, and D ependency Inversion. DRY (Don’t Repeat Yourself) Law of Demeter ( LoD )  Principles

The so-called Gang of Four ( Gof ) Skinny models - Service objects  Patterns

Design is more the art of preserving changeability than it is the act of achieving perfection 

 Virtual World

You will never know less than you know right now. 

Keep your code TRUE 

Transparent The consequences of change should be obvious in the code that is changing and in distant code that relies upon it Reasonable The cost of any change should be proportional to the benefits the change achieves Usable Existing code should be usable in new and unexpected contexts Exemplary The code itself should encourage those who change it to perpetuate these qualities 

Challenge - https://gitlab.com/LegendaryRob/WizSys 

Example 

Sandi Metz “Sandi is the author of Practical Object-Oriented Design in Ruby, and most recently 99 bottles. She has thirty years of experience working on large object-oriented applications. She’s spoken about programming, object-oriented design and refactoring at numerous conferences including Agile Alliance Technical Conference, Craft Conf, Øredev, RailsConf, and RubyConf . She believes in simple code and straightforward explanations, and is the proud recipient of a Ruby Hero award for her contribution to the Ruby community. She prefers working software, practical solutions and lengthy bicycle trips (not necessarily in that order). Find out more about Sandi at sandimetz.com . 

Why this book? 

Design What are design principles What are design Patterns When should I design? How does one design ? OO vs FP Single Responsibility What belongs to a class? How do I organize my code? Why does SR matter? Code Introspection How to write change tolerant code Data vs Behavior When to design and when to replicate? Dependencies What is coupling? is it good or bad? How to inject Dependencies How to isolate dependencies Seeing hidden dependencies Which direction should we interact with them Interfaces Public vs Private Responsibilities of interfaces Intention Context independence Messages over objects Developing trust LoD What is this? why is it important What happens if I break the law? How to avoid violations Duck Typing Understanding the quacks Trusting ducks by choosing them wisely Finding hidden ducks Documentation Sharing Code Static vs Dynamic Typing Inheritance Where to use it? Drawing up relationships What happens when you misapply inheritance Creating abstract classes Template Method Pattern Superclasses and Subclasses