Giter VIP home page Giter VIP logo

go-poodr's Introduction

go-poodr

A Go translation of the example code from Practical Object-Oriented Design in Ruby by Sandi Metz.

Use the Go Playground or go run to try these examples, eg:

chapter2> go run gear1.go

1. Object-Oriented Design

(intro)

2. Designing Classes with a Single Responsibility

  • gear1.go defines a basic Gear with getters
  • gear2.go introduces a new feature (and responsibility)
  • gear3.go hide instance variables (behavior in one place)
  • obscure.go depending on complicated data structures is bad
  • revealing.go isolating the incoming array
  • gear4.go extracting wheel as an internal structure
  • gear5.go a real Wheel with dependency injection

3. Managing Dependencies

  • 1-dependencies.go Gear knows too much about Wheel (actually a step back from gear5.go)
  • 2-duck-type.go We don't need a Wheel specifically, just an object that responds to Diameter()
  • 3-isolate-new.go Isolate instance creation (if you can't inject the dependency for some reason)
  • 4-isolate-messages.go Isolate external messages that could be vulnerable to change
  • 5-map-init.go Remove argument order dependencies (probably not the best way to accomplish this)
  • Skipped a factory method to work with an unwieldy constructor (gear-wrapper).
  • 7-reverse-dependencies.go What if Wheel depends on Gear? (which is more stable?)

4. Creating Flexible Interfaces

(It's all UML! :-)

5. Reducing Costs With Duck Typing

(structural typing in Go)

  • trip1.go A Trip that knows it needs the bicycles prepared.
  • trip2.go Trip preparation becomes more complicated. It knows too much.
  • trip3.go A Preparer interface, more abstract but easier to extend.

6. Acquiring Behavior Through Inheritance

(which Go doesn't have)

  • bikes1.go Starting with a road bike.
  • bikes2.go We need mountain bikes too. Switching on the type.
  • Skipped misapplying inheritance.
  • bikes4.go Implicit delegation and type embedding instead of subclasses.
  • bikes5.go Specializing the Spares method.
  • bikes6.go Use a hook to push responsibilities into the embedded type.

The template method pattern would require a reference to the embedded type, after it is created. Seems like a pattern that shouldn't be attempted in Go.

7. Sharing Role Behavior With Modules

  • schedule1.go Scheduling as part of Bicycle, for later extraction.
  • schedule2.go Extract and delegate to Schedulable.

8. Combining Objects With Composition

  • Skipping first transition, which still uses template methods and inheritance.
  • parts2.go Bicycle composed of Parts, which is a slice of Part.
  • parts3.go Rather than a PartsFactory, I use array-style composite literals.

9. Designing Cost-Effective Tests

Use go test to run these, eg:

chapter9> go test gear1/gear1_check_test.go

Your GOPATH matters for these, as we are importing a separate package for black box testing.

  • gear1/gear1_test.go A basic example using Go's built in testing facilities.
  • gear1/gear1_check_test.go The same code tested with gocheck and a custom matcher.

go-poodr's People

Contributors

nathany avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.