[TOC] ## 单一功能原则 bad ``` func(p *Person) By(name string){ switch name { case "bike": bike:=Bike{} bike.Move() case "car": car:=Car{} car.Move() } } ``` good ``` func (P *Person) RideBike(bike Bike){ bike.Move() } func (P *Person) DriveCar(car Car){ car.Move() } ``` ## 开闭原则 ## 里是替换原则 ## 接口隔离原则 ## 依赖反转原则 依赖抽象而不是具体实现 bad ``` type V6Engine struct { } func (v6 *V6Engine) Run()string{ return "V6Engine run" } type BMWCar struct { engine V6Engine } func (car *BMWCar) RunEngine() string{ return "BMWCar的"+car.engine.Run() } ``` good ``` type engine interface { Run() string } type BMWCar struct { engine engine } func (car *BMWCar) RunEngine() string{ return "BMWCar的"+car.engine.Run() } ```