composition/

directory
v0.0.0-...-818bd76 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 2, 2024 License: Apache-2.0

README

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

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.

Directories

Path Synopsis
assertions
example1
Sample program demonstrating when implicit interface conversions are provided by the compiler.
Sample program demonstrating when implicit interface conversions are provided by the compiler.
example2
Sample program demonstrating that type assertions are a runtime and not compile time construct.
Sample program demonstrating that type assertions are a runtime and not compile time construct.
example3
Sample program to show how method sets can affect behavior.
Sample program to show how method sets can affect behavior.
decoupling
example1
Sample program demonstrating struct composition.
Sample program demonstrating struct composition.
example2
Sample program demonstrating decoupling with interfaces.
Sample program demonstrating decoupling with interfaces.
example3
Sample program demonstrating interface composition.
Sample program demonstrating interface composition.
example4
Sample program demonstrating decoupling with interface composition.
Sample program demonstrating decoupling with interface composition.
example5
Sample program demonstrating removing interface pollution.
Sample program demonstrating removing interface pollution.
example6
Sample program demonstrating being more precise with API design.
Sample program demonstrating being more precise with API design.
exercises
exercise1
Using the template, declare a set of concrete types that implement the set of predefined interface types.
Using the template, declare a set of concrete types that implement the set of predefined interface types.
grouping
example1
This is an example of using type hierarchies with a OOP pattern.
This is an example of using type hierarchies with a OOP pattern.
example2
This is an example of using composition and interfaces.
This is an example of using composition and interfaces.
mocking
example1
Sample program to show how you can personally mock concrete types when you need to for your own packages or tests.
Sample program to show how you can personally mock concrete types when you need to for your own packages or tests.
example1/pubsub
Package pubsub simulates a package that provides publication/subscription type services.
Package pubsub simulates a package that provides publication/subscription type services.
pollution
example1
This is an example that creates interface pollution by improperly using an interface when one is not needed.
This is an example that creates interface pollution by improperly using an interface when one is not needed.
example2
This is an example that removes the interface pollution by removing the interface and using the concrete type directly.
This is an example that removes the interface pollution by removing the interface and using the concrete type directly.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL