Skip to content

Latest commit

 

History

History
79 lines (52 loc) · 4.26 KB

File metadata and controls

79 lines (52 loc) · 4.26 KB

Interface and Composition Design

Composition goes beyond the mechanics of type embedding and is more than just a paradigm. It is the key for maintaining stability in your software by having the ability to adapt to the data and transformation changes that are coming.

Notes

  • This is much more than the mechanics of type embedding.
  • Declare types and implement workflows with composition in mind.
  • Understand the problem you are trying to solve first. This means understanding the data.
  • The goal is to reduce and minimize cascading changes across your software.
  • Interfaces provide the highest form of composition.
  • Don't group types by a common DNA but by a common behavior.
  • Everyone can work together when we focus on what we do and not what we are.

Quotes

"A good API is not just easy to use but also hard to misuse." - JBD

"You can always embed, but you cannot decompose big interfaces once they are out there. Keep interfaces small." - JBD

"Don't design with interfaces, discover them." - Rob Pike

"Duplication is far cheaper than the wrong abstraction. - Sandi Metz

Design Guidelines

Links

Repeat yourself, do more than one thing, and rewrite everything - tef
Embedding
Methods, Interfaces and Embedding - William Kennedy
Composition In Go - William Kennedy
Reducing Type Hierarchies - William Kennedy
Avoid Interface Pollution - William Kennedy

Code Review

Grouping Types

Grouping By State (Go Playground)
Grouping By Behavior (Go Playground)

Decoupling

Struct Composition (Go Playground)
Decoupling With Interface (Go Playground)
Interface Composition (Go Playground)
Decoupling With Interface Composition (Go Playground)
Remove Interface Pollution (Go Playground)
More Precise API (Go Playground)

Conversion and Assertions

Interface Conversions (Go Playground)
Runtime Type Assertions (Go Playground)
Behavior Changes (Go Playground)

Interface Pollution

Create Interface Pollution (Go Playground)
Remove Interface Pollution (Go Playground)

Mocking

Package To Mock (Go Playground)
Client (Go Playground)

Exercises

Exercise 1

Using the template, declare a set of concrete types that implement the set of predefined interface types. Then create values of these types and use them to complete a set of predefined tasks.

Template (Go Playground) | Answer (Go Playground)


All material is licensed under the Apache License Version 2.0, January 2004.