3. "We demand rigidly defined areas of doubt and uncertainty!"
- The Hitchhiker’s Guide to the Galaxy
4. What is an Interface?
An access view that separates the What from the How
The What is our defined entry point
The How is the details that we wish to hide
We hide our internal implementation (the How) by allowing access only
through a well defined entry point (the What).
5. Protected Variation
“Identify points of predicted variation and create a stable interface around
them.” - Alistair Cockburn
“We propose instead that one begins with a list of difficult design decisions or
design decisions which are likely to change.” - David Parnas
Craig Larman - Protected Variation: The Importance of Being Closed
http://codecourse.sourceforge.net/materials/The-Importance-of-Being-Closed.pdf
David Parnas - On Criteria to Be Used in Decomposing Systems into Modules
https://www.cs.umd.edu/class/spring2003/cmsc838p/Design/criteria.pdf
6. Interfaces in Go?
A custom type that specifies a group of method signatures
Checked at compile time, but dynamic when needed
Duck typing. No “implements” needed. Allows for emerging design.
type Animal interface {
Run()
}
http://play.golang.org/p/QG3uEn30EL
7. And what is this interface{} thing?
The interface{} type is a interface that specifies no methods
type interface interface {}
This type can be used for any value, because all values meet this interface.
Remember “dynamic when needed”...
http://play.golang.org/p/ERrw8A9614
8. Interface values
Runtime computed (and cached) method tables
interface table (Animal, Gopher)
type
Gopher
fn[0]
(Gopher).Run
var a Animal = Gopher{}
A function pointer to gopher Run
a.table
a.data
Gopher
data
Calling a.Run() causes Go compiler to generate code that does:
a.table -> fn[0]
Checking the type (i.e. v,
a.table -> type
ok := a.(type)
) does:
9. Embedding
Embedding interfaces
type Animal interface {
Run()
}
type Monkey interface {
Animal
Swing()
}
Embedding types, or “delegating” will meet interface specification if
embedded type meets specification.
http://play.golang.org/p/T6xXHFls8s
10. Real-ish World Example : PubCloud
https://github.com/m4tty/pubcloud
An over abstracted example, in order to highlight interface usage.