转自:https://www.jianshu.com/p/b333c5f34ef6
type Animal interface {
Sleep()
Age() int
Type() string
}
必然我们需要真正去实现这些的子类:
type Cat struct {
MaxAge int
}
func (this *Cat) Sleep() {
fmt.Println("Cat need sleep")
}
func (this *Cat) Age() int {
return this.MaxAge
}
func (this *Cat) Type() string {
return "Cat"
}
type Dog struct {
MaxAge int
}
func (this *Dog) Sleep() {
fmt.Println("Dog need sleep")
}
func (this *Dog) Age() int {
return this.MaxAge
}
func (this *Dog) Type() string {
return "Dog"
}
我们有两个具体实现类 Cat, Dog, 但是Animal如何知道Cat, Dog已经实现了它呢? 原因在于: Cat, Dog实现了Animal中的全部方法, 那么它就认为这就是我的子类。
那么如何去使用这种关系呢?
func Factory(name string) Animal {
switch name {
case "dog":
return &Dog{MaxAge: 20}
case "cat":
return &Cat{MaxAge: 10}
default:
panic("No such animal")
}
}
我们使用具体工厂类来构造具体的实现类, 在调用时你知道有这些方法, 但是并不清楚具体的实现, 每一种类型的改变都不会影响到其它的类型。
package main
import (
"animals"
"fmt"
)
func main() {
animal := animals.Factory("dog")
animal.Sleep()
fmt.Printf("%s max age is: %d", animal.Type(), animal.Age())
}
来看看我们的输出会是什么吧
> Output:
animals
command-line-arguments
Dog need sleep
Dog max age is: 20
> Elapsed: 0.366s
> Result: Success