PDH 개발 공부

Interface segregation, 인터페이스 분리원칙 본문

Design Pattern/SOLID

Interface segregation, 인터페이스 분리원칙

IFBB 2021. 11. 9. 18:01
//No Interface Segregation Principle

//Large Interface
Interface ICarBoatInterface
{
    void drive();
    void turnLeft();
    void turnRight();
    void steer();
    void steerLeft();
    void steerRight();
}




//Interface Segregation Principle
//two small interfaces (Car, Boat)
Interface ICarInterface
{
    void drive();
    void turnLeft();
    void turnRight();
}

Interface IBoatInterface
{
    void steer();
    void steerLeft();
    void steerRight();
}


class Avante : ICarInterface
{
    public void drive()
    {
        //implemenetation
    }
    public void turnLeft()
    {
        //implmementation
    }
    public void turnRight()
    {
        //implementation
    }
}


class CarBoat :ICarInterface , IBoatInterface
{
    public void drive()
    {
        //implemenetation
    }
    public void turnLeft()
    {
        //implmementation
    }
    public void turnRight()
    {
        //implementation
    }
    public void steer()
    {
        //implemenetation
    }
    public void steerLeft()
    {
        //implmementation
    }
    public void steerRight()
    {
        //implementation
    }
}
  • 커져버린 인터페이스 ICarBoatInterface (자동차와 보트가 합쳐진) 에서 자동차 , 보트 인터페이스로 각각 나눠 클래스를 구현 해야한다.

  • 수륙 자동차를 만들고 싶다면 자동차 , 보트를 위한 인터페이스를 상속 받는다.

Comments