package interfacesegregation
// 生产中根据实际情况,将接口拆分到不同的文件中
type EatAnimalAction interface {
eat()
}
type FlyAnimalAction interface {
fly()
}
type SwimAnimalAction interface {
swim()
}
package interfacesegregation
import "fmt"
type Bird struct {
}
func (Bird) fly() {
fmt.Println("bird fly")
}
func (Bird) eat() {
fmt.Println("bird eat")
}
package interfacesegregation
import "fmt"
type Dog struct {
}
func (Dog) swim() {
fmt.Println("dog swim")
}
func (Dog) eat() {
fmt.Println("dog eat")
}
package tech.selinux.design.principle.interfacesegregation;
public interface IAnimalAction {
void eat();
void fly();
void swim();
}
package tech.selinux.design.principle.interfacesegregation;
public interface IEatAnimalAction {
void eat();
}
package tech.selinux.design.principle.interfacesegregation;
public interface IFlyAnimalAction {
void fly();
}
package tech.selinux.design.principle.interfacesegregation;
public interface ISwimAnimalAction {
void swim();
}
package tech.selinux.design.principle.interfacesegregation;
public class Dog implements ISwimAnimalAction, IEatAnimalAction {
@Override
public void eat() {}
@Override
public void swim() {}
}
package tech.selinux.design.principle.interfacesegregation;
public class Bird implements IEatAnimalAction,IFlyAnimalAction {
@Override
public void eat() {}
@Override
public void fly() {}
}
补充另一个版本的Java/Scala Demo 以及源码解析