package inital
import "fmt"
type PPerson struct{
name string
}
func (this *PPerson)SetName(){
this.name="小力"
}
func (this *PPerson)By(method string){
switch method {
case "car":
aa:=Car{}
aa.ByCar()
case "bike":
bb:=Bike{}
bb.Bike()
}
}
type Car struct{
}
func(this Car) ByCar(){
fmt.Println("坐车去........")
}
type Bike struct{
}
func (this Bike)Bike(){
fmt.Println("骑自行车去.......")
}
//TODO 上述的不符合开闭原则 修改如下 也不符合单一职责原则
func (this *PPerson)ByBikeSplit(bike Bike){
bike.Bike()
}
func (this *PPerson)ByCarSplit(car Car){
car.ByCar()
}
测试代码如下:
func TestOpenClose(t *testing.T){
person :=inital.PPerson{}
person.By("bike")
//person.SetName()
//person.GetSkin()
//person.Walk()
}
func TestAvoidOpenClose(t *testing.T){
person :=inital.PPerson{}
car:=inital.Car{}
bike :=inital.Bike{}
person.ByBikeSplit(bike)
person.ByCarSplit(car)
//person.By("bike")
//person.SetName()
//person.GetSkin()
//person.Walk()
}