OOD - Object orientated design

52
OOD

Transcript of OOD - Object orientated design

Page 1: OOD - Object orientated design

OOD

Page 2: OOD - Object orientated design

We want to do our best work.

Page 3: OOD - Object orientated design

We want our work to have meaning

Page 4: OOD - Object orientated design

We want to have fun along the way

Page 5: OOD - Object orientated design

Two major themes being Designand Messages over objects

Page 6: OOD - Object orientated design

Design

Page 7: OOD - Object orientated design

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

of perspective.

Page 8: OOD - Object orientated design

Source: Pablo Picasso - Buffalo Art

Page 9: OOD - Object orientated design

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.

Page 10: OOD - Object orientated design

How does Design affect us?

Page 11: OOD - Object orientated design
Page 12: OOD - Object orientated design

Perspective.

Page 13: OOD - Object orientated design

Unfortunately, something will change.

Page 14: OOD - Object orientated design

The customers didn’t know what they wanted.

Page 15: OOD - Object orientated design

They didn’t say what they meant.

Page 16: OOD - Object orientated design

You didn’t understand their needs.

Page 17: OOD - Object orientated design

You’ve learned how to do something better.

Page 18: OOD - Object orientated design

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

Page 19: OOD - Object orientated design

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

Page 20: OOD - Object orientated design

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

Page 21: OOD - Object orientated design

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

Page 22: OOD - Object orientated design

Dependency

Page 23: OOD - Object orientated design

Object-oriented design is about managing dependencies.

Page 24: OOD - Object orientated design

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

Page 25: OOD - Object orientated design

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

Page 26: OOD - Object orientated design

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

Page 27: OOD - Object orientated design

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

Page 28: OOD - Object orientated design

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

Page 29: OOD - Object orientated design

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.

Page 30: OOD - Object orientated design

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

Page 31: OOD - Object orientated design

Dependency injection

Page 32: OOD - Object orientated design

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

Page 33: OOD - Object orientated design

Dependency aversion

Page 34: OOD - Object orientated design

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

Page 35: OOD - Object orientated design

Order Dependency

Page 36: OOD - Object orientated design

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

Page 37: OOD - Object orientated design

Is your code too coupled to your tests?

Page 38: OOD - Object orientated design

Tools for design

Page 39: OOD - Object orientated design

Single Responsibility, Open-Closed, Liskov Substitution, Interface Segregation, and Dependency Inversion. DRY (Don’t Repeat Yourself) Law of Demeter (LoD)

Principles

Page 40: OOD - Object orientated design

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

Patterns

Page 41: OOD - Object orientated design

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

Page 42: OOD - Object orientated design

Virtual World

Page 43: OOD - Object orientated design

You will never know less than you know right now.

Page 44: OOD - Object orientated design

Keep your code TRUE

Page 45: OOD - Object orientated design

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 contextsExemplary The code itself should encourage those who change it to perpetuate these qualities

Page 46: OOD - Object orientated design

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

Page 47: OOD - Object orientated design

Example

Page 48: OOD - Object orientated design
Page 49: OOD - Object orientated design
Page 50: OOD - Object orientated design

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.

Page 51: OOD - Object orientated design

Why this book?

Page 52: OOD - Object orientated design

Design

What are design principlesWhat 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 IntrospectionHow to write change tolerant codeData vs Behavior

When to design and when to replicate?

Dependencies What is coupling? is it good or bad?

How to inject DependenciesHow to isolate dependencies

Seeing hidden dependencies

Which direction should we interact with them

Interfaces Public vs Private

Responsibilities of interfacesIntention

Context independenceMessages over objects

Developing trust

LoDWhat is this? why is it important

What happens if I break the law?How to avoid violations

Duck TypingUnderstanding the quacks

Trusting ducks by choosing them wiselyFinding hidden ducks

DocumentationSharing Code

Static vs Dynamic Typing

InheritanceWhere to use it?

Drawing up relationshipsWhat happens when you misapply inheritance

Creating abstract classesTemplate Method Pattern

Superclasses and Subclasses