-
[TIL] 7주차 2일 개인 프로젝트 주차 (Interface, 아이템 상호작용 )개발일지/스파르타 코딩클럽 부트캠프 2024. 5. 28. 20:45
오늘 배운 내용 정리
인터페이스 복습
인터페이스에 대해서 조금 더 알아보고 복습하는 시간을 가져보자
인터페이스를 통해서 클래스들은 공통적인 동작을 정의하고, 이러한 동작들을 구현하는
클래스들은 해당 인터페이스를 구현함으로써 공통 규약을 준수할 수 있다.
인터페이스를 설명하는 주요 특징은 다음과 같다.
추상화 : 인터페이스는 추상적인 개념으로, 실제로 구현된 메서드가 없고, 메서드의 시그니처만을 가진다.
따라서 인터페이스는 인스턴스화 될 수 없으며, 구현체가 필요하다.
메서드 시그니처 : 인터페이스는 구현 클래스가 반드시 구현해야하는 메서드들의 시그니처를 정의한다.
메서드의 이름, 매개변수, 변환 타입이 포함된다.
다중 상속 가능 : 클래스는 하나의 클래스만 상속받을 수 있는 반면, 인터페이스는 여러개를 동시에
구현할 수 있다. 이를 통해서 다중 상속을 흉내내는 것이 가능하다.
강제적 구현 : 클래스가 인터페이스를 구현하면, 인터페이스에서 정의한 모든 메서드를 반드시 구현해야한다.
이로 인해서 클래스는 인터페이스에 정의된 동작을 강제로 구현하게 된다.
인터페이스 간 확장 : 인터페이스는 다른 인터페이스를 확장할 수 있다. 이를 통해서 더 큰 범위의
공통 동작을 정의할 수 있다.
인터페이스를 사용해야하는 이유
코드는 결합도가 낮아야한다. <- 이 부분은 저번 주 TIL중 SOLID원칙을 다룰 때도 설명했던 부분인데
코드는 의존도가 낮고 응집성이 높아져야한다.
이 말은 코드끼리 서로 의존하는 의존도를 낮게하고 본인의 역할을 다 하게 하며 유연성을 늘리는 것이다.
그를 위해서 구체적 구현 클래스가 아닌 작은 단위의 여러 인터페이스를 사용하자.
협업의 관점
- 개발 기간을 단축 인터페이스라는 구현되지 않은 틀만 작성하면 구현 클래스에서 코드 작성 및 개발 가능
- 표준화가 가능 여러명의 개발자가 작업해도 정형화된 작업이 가능
- 독립적인 프로그래밍이 가능 선언은 인터페이스에서 구현은 클래스에서.
아래는 인터페이스 예시문이다.
public interface Payment { public void Pay(); } public class Card : Payment { public void Pay(){} } public class Cash : Payment { public void Pay(){} } public class QR : Payment { public void Pay(){} } (x) public class Store { Card card; Cash cash; QR qr; } (o) public class Store { Payment payment; payment.Pay(); }
'개발일지 > 스파르타 코딩클럽 부트캠프' 카테고리의 다른 글
[TIL] 7주차 4일 개인프로젝트주차 ( Raycast, Coroutine ) (2) 2024.05.30 [TIL] 7주차 3일 개인과제주차 (0) 2024.05.29 [TIL] 7주차 1일 개인 프로젝트 주차 ( 조명, 카메라 절두체, Coroutine) (0) 2024.05.27 [TIL] 6주차 5일 유니티 숙련주차 시작, 개인 프로젝트 ( RayCast, SkyBox, ForceMode) (0) 2024.05.24 [TIL] 6주차 4일 팀 프로젝트 끝, 객체 지향 특강 ( SOLID 원칙 ) (0) 2024.05.23