"일꾼이 일을 잘하려면 먼저 도구를 갈고 닦아야 한다." - 공자, 『논어』.
첫 장 > 프로그램 작성 > 인터페이스 분리 원칙

인터페이스 분리 원칙

2024-08-26에 게시됨
검색:475

Interface Segregation Principle

어떠한 클라이언트도 사용하지 않는 방법에 의존하도록 강요해서는 안 됩니다.

다양한 출력 장치를 객체로 표현한 사무실 공간의 예를 생각해 보세요.

인터페이스 분리 원칙 이전:

I다기능 인터페이스

/**
 * @ImultiFunction interface has methods related to all output devices present in office space
 * for devices like Printer, Scanner, Fax machines, etc
*/
public interface IMultiFunction {
    public void print();
    public void getPrintSpoolDetails();
    public void scan();
    public void scanPhoto();
    public void fax();
    public void internetFax();
}

이제 다양한 장치에 대해 위의 공통 인터페이스를 구현합니다.

모든 기능을 갖춘 XeroxWorkCenter 클래스

/**
 * 
 * You must have seen Xerox work station device which has all the features in one like printing, scanning, xerox,
 * fax etc
*/
public class XeroxWorkCenter implements IMultiFunction {

    @Override
    public void print() {
        // real printing code
    }

    @Override
    public void getPrintSpoolDetails() {
        // real get print spool details code
    }

    @Override
    public void scan() {
        // read scanning code
    }

    @Override
    public void scanPhoto() {
        // real scan photo code 
    }

    @Override
    public void fax() {
        // real fax code
    }

    @Override
    public void internetFax() {
        // real internet fax code
    }

}

HpPrinterNScanner 클래스에는 인쇄 및 스캔 기능이 있습니다.

public class HpPrinterNScanner implements IMultiFunction {

    @Override
    public void print() {
        // real printing code
    }

    @Override
    public void getPrintSpoolDetails() {
        // real get print spool details code
    }

    @Override
    public void scan() {
        // read scanning code
    }

    @Override
    public void scanPhoto() {
        // real scan photo code 
    }

    //Since HpPrinterNScanner has only printing and scanning abilities fax() and internetFax() will have empty body
    @Override
    public void fax() {}

    @Override
    public void internetFax() {}

}

CanonPrinter 클래스에는 인쇄 기능만 있습니다.

public class CanonPrinter implements IMultiFunction {

    @Override
    public void print() {
        // real printing code
    }

    @Override
    public void getPrintSpoolDetails() {
        // real get print spool details code
    }

    //Since the CanonPrinter has only printing ability rest of the method will have an empty body
    @Override
    public void scan() {}

    @Override
    public void scanPhoto() {}

    @Override
    public void fax() {}

    @Override
    public void internetFax() {}

}

ISP 위반 식별 기법

  • Fat 인터페이스 (두 개의 메소드 선언이 많은 인터페이스)
  • 결합력이 낮은 인터페이스(서로 관련될 가능성이 없는 메서드를 갖는 인터페이스)
  • *빈 메소드 구현 *( 클래스가 사용하지 않는 메소드를 강제로 구현해야 하는 경우 메소드 구현을 빈 본문으로 남겨둡니다.)

인터페이스 분리 후 원칙:

public interface IPrint {
    public void print();
    public void getPrintSpoolDetails();
}
public interface IScan {
    public void scan();
    public void scanPhoto();
}
public interface IFax {
    public void fax();
    public void internetFax();
}
/**
 * 
 * You must have seen the Xerox workstation device which has all the features in one like printing, scanning, xerox, fax, etc.
*/
public class XeroxWorkCenter implements IPrint,IScan,IFax {

    @Override
    public void print() {
        // real printing code
    }

    @Override
    public void getPrintSpoolDetails() {
        // real get print spool details code
    }

    @Override
    public void scan() {
        // read scanning code
    }

    @Override
    public void scanPhoto() {
        // real scan photo code ̰
    }

    @Override
    public void fax() {
        // real fax code
    }

    @Override
    public void internetFax() {
        // real internet fax code
    }

}
public class HpPrinterNScanner implements IPrint,IScan {

    @Override
    public void print() {
        // real printing code
    }

    @Override
    public void getPrintSpoolDetails() {
        // real get print spool details code
    }

    @Override
    public void scan() {
        // read scanning code
    }

    @Override
    public void scanPhoto() {
        // real scan photo code 
    }
}
public class CanonPrinter implements IPrint {

    @Override
    public void print() {
        // real printing code
    }

    @Override
    public void getPrintSpoolDetails() {
        // real get print spool details code
    } 
}

각 인터페이스에는 단일 책임이 있으며 이제 훨씬 더 깔끔해졌습니다.

ISP가 다른 SOLID 원칙과 유지하는 관계

단일 책임
인터페이스를 다른 인터페이스로 분리한 후 이제 IPrint, IScan과 같은 모든 인터페이스는 단일 책임을 갖습니다.

리스코프 대체
분리로 인해 이제 모든 클래스(인터페이스 구현)가 Liskov 대체를 따릅니다. 모든 하위 유형 또는 구현 클래스가 인터페이스 참조 변수

로 대체될 수 있기 때문입니다.
릴리스 선언문 이 기사는 https://dev.to/prashantrmishra/interface-segregation-principle-4e97 ?1에서 재현됩니다.
최신 튜토리얼 더>

부인 성명: 제공된 모든 리소스는 부분적으로 인터넷에서 가져온 것입니다. 귀하의 저작권이나 기타 권리 및 이익이 침해된 경우 자세한 이유를 설명하고 저작권 또는 권리 및 이익에 대한 증거를 제공한 후 이메일([email protected])로 보내주십시오. 최대한 빨리 처리해 드리겠습니다.

Copyright© 2022 湘ICP备2022001581号-3