Designing Objects with a Single Responsibility. Andrew P. Black

Similar documents
Finding Proper Gears and Wheels pt.ii

Exploring Bicycle Technology August 11, Front sprockets

Clicker Question: Clicker Question: Clicker Question: Phases of Matter. Phases of Matter and Fluid Mechanics

Your web browser (Safari 7) is out of date. For more security, comfort and. the best experience on this site: Update your browser Ignore

DESIGN BEHIND THE BIKE WHEELS

Answer Key. Name. Shade. of this square in two different ways. ANSWER Sample:

CAR DRIVE. Kelly went for a drive in her car. During the drive, a cat ran in front of the car. Kelly slammed on the brakes and missed the cat.

Gears Ratios and Speed / Problem Solving

we must design our trike to avoid these types of crippling

8th Grade. Data.

Question: Bicycles. Observations About Bicycles. Static Stability, Part 1. Static Stability, Part 2. Static Stability, Part 3

Concepts/Skills. Computation Ratios Problem solving. Materials

Applying Hooke s Law to Multiple Bungee Cords. Introduction

THE SYSTEM OF EFFECTIVE SELF-DEFENCE TRUE GENIUS LIES IN SIMPLICITY, BUT SIMPLE DOESN T MEAN PRIMI- TIVE, SAID EINSTEIN. Alexei Alexeevich Kadochnikov

Mechanical systems and control: investigation

LOCATION North Union Community Hall, Boag Rd, and Catering Rd. near Queensville.

The IEC61508 Inspection and QA Engineer s hymn sheet

Alabama Head Coach Nick Saban December 31, 2017

A QUICK GUIDE TO FIXIE BIKES: EVERYTHING YOU NEED TO KNOW TO GET STARTED ON YOUR FIXED-GEAR BICYCLE (FIXED GEAR, SINGLE SPEED, FIXIE BIKE,

Front Derailleur. Shifters and derailleurs work together!

21-110: Problem Solving in Recreational Mathematics

GEARS. gears FORCE CYCLING. Lesson Plan 2 By Claire Hewson E: Explore Cambridge related themes

Circle Terms.notebook March 27, 2017

PSY201: Chapter 5: The Normal Curve and Standard Scores

AGA Swiss McMahon Pairing Protocol Standards

Congratulations on your purchase of a JC Series Performer trike! The Performer JC Series is designed for everything from touring to commuting and

Density and Specific Gravity

Monitoring Carbon Dioxide

Solar Bicycle Energy Management System Using HP48G Calculator

DO NOT, under any circumstances, throw this away! This packet MUST be saved for the final exam.

All Work and no Play. Is that work? Work, work, work. You might head off to your job one day, sit at a computer, and type away at the keys.

Section 2 What Is a Force?

Applications of trigonometry

SERVICE INSTRUCTIONS SI-7B40B C810

The Wheel Shop. You go to a shop that sells tricycles. There are 18 wheels in the Wheel Shop.

Lesson 1: Setting the Stage Projection Master

Vessels subject to External Pressure

Chapter 11 The Behavior of Gases

Basics, Types, Use and Applications

Chain Installation Guide

World Robot Olympiad 2018

Design Project 2 Sizing of a Bicycle Chain Ring Bolt Set ENGR 0135 Sangyeop Lee November 16, 2016 Jordan Gittleman Noah Sargent Seth Strayer Desmond

Ride for New Beginnings June 23-25, 2017

Chapters 25: Waves. f = 1 T. v =!f. Text: Chapter 25 Think and Explain: 1-10 Think and Solve: 1-4

La Plata Opinion Change

CVEN Computer Applications in Engineering and Construction. Programming Assignment #4 Analysis of Wave Data Using Root-Finding Methods

CRANKSET 1 - TECHNICAL SPECIFICATIONS. 44,5 mm 8 mm

The IEC61508 Project Manager's & Project Engineer's hymn sheet

1 - TECHNICAL SPECIFICATIONS

Terratrip 303 Plus: Codriver s Guide

University of Notre Dame Department of Finance Economics of the Firm Spring 2012

Chapter 5: Gases 5.1 Pressure Why study gases? An understanding of real world phenomena. An understanding of how science works.

Question: Bicycles. Vehicle s Static Stability, Part 1. Observations About Bicycles. Vehicle s Static Stability, Part 2

presented by: Table of Contents:

TABLE OF CONTENTS INTRODUCTION

Soccerfest Rules (Version )

Volume Formula for a Cylinder

CC Investigation 1: Graphing Proportions

Speed Reading. Forces Cause Change. The force of a bat hitting a baseball causes the baseball to change direction.

T R A F F I C S K IL L S 101

UK rated gliding competition scoring explained

Second Generation Bicycle Charging Station. Engineering Analysis

Things to know about our STEPS8000 products emax, minimax and freemax. legal notice

FRAME FEATURES TABLE OF CONTENTS INTRODUCTION

A Cost Effective and Efficient Way to Assess Trail Conditions: A New Sampling Approach

BASIC QUANTITIES OF GASES

Monday Tuesday Wednesday Thursday Friday

The Norwood Science Center 2005

2017 Sporting Chance Forum Geneva, 30 November 2017

Please note: specifications are subject to change for improvement without notice. Apr by Shimano Inc. XBC IZM Printed in Japan

Shiftable Unicycle Hub, Type KH

Have questions? Chat with us live at raleighusa.com or call us at , 8am 5pm PST

4MyCycle Mini Bike Pump. Useful Details for Owners regarding Presta & Schrader Configurations

SURFACE CASING SELECTION FOR COLLAPSE, BURST AND AXIAL DESIGN FACTOR LOADS EXERCISE

EPICYCLIC POWERED BICYCLE

RoboCupJunior Soccer Rules for SuperTeams 2016

R3 RIM MANUAL EN. Version 1 I

UNIT V 1. What are the traffic management measures? [N/D-13] 2. What is Transportation System Management (TSM)? [N/D-14]

Parking Lot HW? Joke of the Day: What do you call a leg that is perpendicular to a foot? Goals:

2.5. All games and sports have specific rules and regulations. There are rules about. Play Ball! Absolute Value Equations and Inequalities

Understanding Tempo by Tom Leoni 2014

Sherwood Drive Traffic Circle

Bicycle Safety Presented by: Name

Name Class Date. What are some properties of gases? How do changes of pressure, temperature, or volume affect a gas?

Discussion and guidance on the definition and qualification of porous loads

AIRMOUNT VIBRATION ISOLATION

Course outlineimportant: As personnel i

Bike Rack and Spare Tire Carrier 1-1/4" Receiver

2 are both ways of saying a ratio of 2 to 5

INTRODUCTION TO LESSON CLUSTER 4

Club-Wide Safety Committee Recommendations

Complete Bike Line 2017

Bikes and Energy. Pre- Lab: The Bike Speedometer. A Bit of History

Project 1 Those amazing Red Sox!

GUIDE TO A TYPICAL IPDA TOURNAMENT 1

Module 2. Topic: Mathematical Representations. Mathematical Elements. Pedagogical Elements. Activities Mathematics Teaching Institute

World Robot Olympiad 2019

(fig. 3) must be at the same temperature as the water in this chamber CALORIMETRIC STUDIES OF THE EXTREMITIES

Chapter 3. Solids, Liquids, and Gases

Michel Beal. Explanation of rope characteristics & impact force or What do the numbers mean on rope instructions? CEO Beal Ropes

Transcription:

Designing Objects with a Single Responsibility Andrew P. Black 1

Keep it Simple Beck: Is the simplest design the one with the fewest classes? This would lead to objects that were too big to be effective. Is the simplest design the one with the fewest methods? This would lead to big methods and duplication. Is the simplest design the one with the fewest lines of code? This would lead to compression for compression's sake and a loss of communication. 2

Four Constraints, in Priority Order: The simplest system (code and tests together) is one that: 1.communicates everything you want to communicate, 2.contains no duplicate code, 3.has the fewest possible classes, and 4.has the fewest possible methods. (1) and (2) together constitute the Once and Only Once rule. 3

What belongs in a Class Metz: Your goal is to model your application, using [objects], such that it does what it is supposed to do right now, and is also easy to change later. Design is more the art of preserving changeability than it is the act of achieving perfection. An object with a single responsibility: can be reused wherever that responsibility is needed solves the design problem, because it tells you what code should be in it. 4

Bicycle Example Small Gear Big Gear Little chainring, big cog. Feet go around many times, wheel goes around just once. A big chainring and little cog. Feet go around once; wheel goes around many times. Figure 2.1 Small versus big bicycle gears. 5

Ruby code: 1 chainring = 52 # number of teeth 2 cog = 11 3 ratio = chainring / cog.to_f 4 puts ratio # -> 4.72727272727273 5 6 chainring = 30 7 cog = 27 8 ratio = chainring / cog.to_f 9 puts ratio # -> 1.11111111111111 6

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 ratio 11 chainring / cog.to_f 12 end 13 14 def gear_inches 15 # tire goes around rim twice for diameter 16 ratio * (rim + (tire * 2)) 17 end 18 end 19 20 puts Gear.new(52, 11, 26, 1.5).gear_inches 21 # -> 137.090909090909 22 23 puts Gear.new(52, 11, 24, 1.25).gear_inches 24 # -> 125.272727272727 7

Grace version 8

Single Responsibility? Not really! since when does a gear have a a tire and a rim? mixed up with other bits of bicycle Does it matter? Maybe! 9

Arguments to Leave it be Code is Transparent and Reasonable Consequence of a change should be Transparent Cost of change proportional to benefits Why? Because there are no dependents How should we improve it? We don't yet know but the moment that we acquire some dependents, we will Wait until that time 10

Arguments for Change Code is neither (re)usable, nor exemplary multiple responsibilities can't be used for other gears not a pattern to be emulated 11

Improve it now vs. improve it later This tension always exists! designs are never perfect the designer has to weigh the costs and benefits of a change 12

Embracing Change Some basic rules that will help, regardless of what change happens: Depend on Behaviour, not data encapsulate instance variables Grace gives us this one for free encapsulate data e.g., don t expose an array of pairs of numbers 13

method knows all about the structure of d obscuringreferences 14

Separate Structure from Meaning If you need a table of wheel and tire sizes, make it contain objects, not lists Metz uses a Ruby Struct to create a transparent object. In Grace: factory method wheelwithrim(r) tire(t) { // this is equivalent to the Ruby `Struct.new(:rim, :tire)` method rim { r } method tire { t } method asstring { "{rim} wheel with {tire} tire" } } 15

Embracing Change Enforce Single Responsibility Everywhere Extract extra responsibilities from methods Isolate responsibilities in classes Grace lets you create local factory methods 17

The Real Wheel The customer tells you that she has need for computing wheel circumference. This tells you that your bicycle calculator app needs to model wheels. So let s move wheel out of gear. 18