1 / 6

OOP 5 원칙

OOP 5 원칙. OCP(Open-Closed Principle) 확장가능하지만 변경되지 않는 구조 새로운 기능이 추가될 때 기존의 코드가 변경되어서는 안 된다 . 무조건적인 상속은 답이 아니다 . KeyPoint Open : 클래스 수직관계 (is – a) 에서는 열려 있어야 한다 . Close : 클래스 수평관계 (has – a) 에서는 허용하지 않아야 한다. 새로운 기능이 추가되도 기존 코드가 변경되지 않는다. Shape. Rectangle. Triangle. Round. Shape.

Download Presentation

OOP 5 원칙

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. OOP 5원칙

  2. OCP(Open-Closed Principle) • 확장가능하지만 변경되지 않는 구조 • 새로운 기능이 추가될 때 기존의 코드가 변경되어서는 안 된다. • 무조건적인 상속은 답이 아니다. • KeyPoint • Open : 클래스 수직관계(is – a)에서는 열려 있어야 한다. • Close : 클래스 수평관계(has – a)에서는 허용하지 않아야 한다. 새로운 기능이 추가되도 기존 코드가 변경되지 않는다. Shape Rectangle Triangle Round Shape ShapeEx : Shape Shape drawRectangle() drawRound() drawTriangle() drawTriangle() drawRectangle() drawRound() draw() draw() draw() draw() 상속 기능이 추가될때마다 상속하거나 수정해야만한다. 수정

  3. ISP(Interface Segregation Principle) • 여러 개의 특화된 인터페이스가 하나의 범용 인터페이스보다 낫다. • 비대한 범용 인터페이스를 구현한 클래스의 경우 클라이언트에게 불필요한 메소드까지 노출시키게 된다. ★ 나쁜 예 ★ 좋은 예 PartTimeWorker IWorkable StandardWorker IWorker IFeedable StandardWorker PartTimeWorker eat() work() eat() work() work() eat() work() eat() work() eat() work() 사용하지 않는 메소드를 가지게 된다. • PartTimeWorker클래스를 구현하면서 사용하지 않을 • 메소드(eat())까지 가지고 있다. • 이렇게 되면 해당 클래스를 사용하는 클라이언트에게 혼란을 야기할 수 있다.

  4. SRP(The Single Responsibility Principle) • ISP가 Interface의 단일책임을 강조하는 것이라면 SRP는 Class의 단일책임을 강조하는 것이다. • 하나의 클래스는 하나의 책임만을 가지게 한다. • KeyPoint • “하나의 클래스에 너무 많은 기능을 부여하지 않아야 한다.” • 예) 새로운 기능을 추가하게 될 때 • 하나의 클래스에 두 가지 기능을 가지고 있는 경우 • 하나의 클래스에서 한 가지 기능만을 가지고 있는 경우 • 하나의 클래스에서 한 가지 기능만을 담당하고 있을 때가 중복이 적게 발생하게 되는 장점이 있다. CRectangleDraw CTriangleDraw CPainter CTriangle CRectangle Draw() Area() : double Draw() Area() : double Draw() Area() : double Area() : double draw()함수의 중복 구현이 발생한다.

  5. LSP(The Liscov Substitution Principle) • base클래스만을 알고 있고 derived 클래스를 모르는 클라이언트가 해당 객체를 문제 없이 사용할 수 있어야 한다. • 아래와 같은 코드가 있을 때 int main(){ try{ int* n = new int } catch(CMemoryException& e){ Log.write(e.GetErrorMessage()); e.ReportError(); } catch(CException& e){ Log.write(e.GetErrorMessage()); e.ReportError(); } } catch(CMemoryException& e){…}구문이 없어도 작동에 문제가 발생하지 않는다. CException CMemoryException GetErrorMessage() ReportError() GetErrorMessage() ReportError()

  6. DIP(Dependency-Inversion Principle) • Keypoint • High Level modules should not depend upon low level modules. Both should depend upon abstractions. • Abstractions should not depend upon details. Details should depend upon abstractions. • High level Module이란? • contain the important policy decisions and business models of an application. It is these models that contain the identity of the application. • 상위레벨모듈이 하위레벨모듈에 dependency가 있을 경우 상위레벨모듈의 재사용이 매우 힘들어진다. 하위 모듈인 Utility Layer 에 dependency가 생긴다. Utility Layer는 재사용성이 매우 좋은 반면 Policy Layer의 재사용성은 극히 떨어진다. 클라이언트 쪽에서 policy layer의 abstract layers(interface)를 사용하게 되므로 policy layer의 독립성이 증가되므로 재사용성 및 수정이 용이해진다.

More Related