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.
- 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.
"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
- Learn about the design guidelines for composition.
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
Grouping By State (Go Playground)
Grouping By Behavior (Go Playground)
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)
Interface Conversions (Go Playground)
Runtime Type Assertions (Go Playground)
Behavior Changes (Go Playground)
Create Interface Pollution (Go Playground)
Remove Interface Pollution (Go Playground)
Package To Mock (Go Playground)
Client (Go Playground)
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.