객체가 반드시 구현해야 할 행동을 지정하는데 활용되는 타입
다형성의 구현, 의존성이 분리된 설계에 유용하게 활용
Source 폴더로 복사한 코드를 프로젝트에 포함시키려면
언리얼 에디터에서 'Tools-Referesh Visual Studio 2022 Project' 선택

인터페이스의 함수를 추상 함수로 두어 구현을 강제할 수도 있고, 미리 정의해두고 필요한 경우 재정의
Super 키워드로 인터페이스의 부모를 가져올 수 없음 (클래스 정보는 단일 상속만 지원)
대신 IUserInterface::Func(); 처럼 인터페이스의 타입을 직접 입력하여 가능
캐스팅으로 인터페이스 상속 여부를 확인할 수 있음
TArray<UPerson*> Persons = { NewObject<UStudent>(), NewObject<UTeacher>(), NewObject<UStaff>() }
for (const auto Person : Persons)
{
ILessonInterface* LessonInterface = Cast<ILessonInterface>(Person);
if (LessonInterface)
{
LessonInterface->DoLesson(); // (인터페이스 상속)
}
else
{
// 수업에 참여 불가 (인터페이스 상속 안함)
}
}
언리얼 C++ 인터페이스를 사용하면, 클래스가 수행해야 할 의무를 명시적으로 지정할 수 있어 좋은 객체 설계를 만드는데 도움을 줄 수 있다.
상속: 성질이 같은 부모-자식 관계를 의미하는 Is-A 관계
컴포지션: 성질이 다른 두 객체에서 어느 객체를 소유하는 Has-A 관계
class Card
{
public:
Card(int InId) : Id(InId) {}
int id = 0;
};
class Person
{
public:
Person(Card InCard) : IdCard(InCard) {}
protected:
Card IdCard; // 카드 소유
};

전방 선언으로 클래스 구조는 모르지만 클래스 만큼의 크기를 미리 확보 (의존성도 감소)
언리얼5부턴 선언 시 TObjectPtr로 감싸는 걸 추천 [공식 문서 링크]
언리얼 C++의 컴포지션 기법은 게임의 복잡한 객체를 설계하고 생성할 때 유용하게 사용된다.
발행 구독 디자인 패턴
Push 형태의 알림을 구현하는데 적합한 디자인 패턴

델리게이트 예시
// CourseInfo.h
DECLARE_MULTICAST_DELEGATE_TwoParams(FCourseInfoOnChangedSignature, const FString&, const FString&);
UCLASS()
class UNREALDELEGATE_API UCourseInfo : public UObject
{
...
FCourseInfoOnChangedSignature OnChanged;
};
// CourseInfo.cpp
void UCourseInfo::ChangeCourseInfo(const FString& InSchoolName, const FString& InNewContents)
{
Contents = InNewContents;
OnChanged.Broadcast(InSchoolName, Contents);
}
// Student.cpp
void UStudent::GetNotification(const FString& School, const FString& NewCourseInfo)
{
UE_LOG(LogTemp, Log, TEXT("[Student] %s님이 %s로부터 받은 메시지 : %s"), *Name, *School, *NewCourseInfo);
}
// MyGameInstance.cpp
void UMyGameInstance::Init()
{
Super::Init();
// 학사 정보는 발행자가 소유
CourseInfo = NewObject<UCourseInfo>(this);
UStudent* Student1 = NewObject<UStudent>();
UStudent* Student2 = NewObject<UStudent>();
UStudent* Student3 = NewObject<UStudent>();
// 구독
CourseInfo->OnChanged.AddUObject(Student1, &UStudent::GetNotification);
CourseInfo->OnChanged.AddUObject(Student2, &UStudent::GetNotification);
CourseInfo->OnChanged.AddUObject(Student3, &UStudent::GetNotification);
// 발행
CourseInfo->ChangeCourseInfo(SchoolName, TEXT("변경된 학사 정보"));
}
데이터 기반의 디자인 패턴을 설계할 때 유용하게 사용