TIL
[250313 TIL] 뭔가 많이 쓸 것 같은 어댑터 패턴
yoosorang
2025. 3. 13. 22:41
1️⃣ C++ 코딩테스트 공부
💡스택은 언제 사용할까?
- 근처에 있는 것들끼리 관련 있을 때
- 순서 reverse 해야할 때
🔹괄호 짝 맞추기
더보기
더보기
#include <stack>
#include <iostream>
using namespace std;
bool solution(string s) {
bool answer;
stack<char> st;
for(char ch : s)
{
//if(ch == ')' && st.top() == '(') || )가 나왔는데 스택이 비어있을 경우 생각못함
if(ch == ')')
{
if(st.top() == '(')
{
st.pop();
continue;
}
else
{
answer = false;
break;
}
}
st.push(ch);
}
answer = st.empty() ? true:false;
return answer;
}
int main()
{
cout << solution("(())()") << endl; // 1
cout << solution("((())()") << endl; // 0
return 0;
}
닫힌 괄호가 임의 위치의 열린 괄호와 상쇄되는 것이 아니라 가장 가까운(최근) 열린 괄호와 상쇄됨
🔹10진수를 2진수로 변환하기
더보기
더보기
#include <stack>
#include <iostream>
using namespace std;
string solution(int decimal) {
string binary = "";
stack<int> st;
while(decimal > 0)
{
st.push(decimal % 2);
decimal = decimal / 2;
}
while(!st.empty())
{
binary += to_string(st.top());
st.pop();
}
return binary;
}
#include <iostream>
int main()
{
cout << solution(10) << endl; // 1010
cout << solution(27) << endl; // 11011
cout << solution(12345) << endl; // 11000000111001
return 0;
}
2️⃣ 디자인 패턴 공부_어댑터 패턴(구조 패턴)
현업에서 래핑해서 쓰자 = 어댑터 패턴 사용(개방 폐쇄 원칙, 단일 책임 원칙 준수)
💡사용 개념
래핑될 클래스는 어댑터 클래스에서 인터페이스로 상속 받고
래핑할 클래스는 참조로 가져서 래핑될 클래스의 인터페이스 메서드 구현에 래핑할 클래스의 메서드 호출
⚠️주의
너무 많은 변환로직을 어댑터에 몰아넣으면 안됨
입/출력 변환이 의도대로 이루어지는지 꼭 테스트해야
더보기
더보기
#include <iostream>
#include <memory>
using namespace std;
class IDuck
{
public:
virtual void Quack() = 0;
virtual void Fly() = 0;
};
class ITurkey
{
public:
virtual void Gobble() = 0;
virtual void FlyShortDistance() = 0;
};
class MallardDuck : public IDuck
{
public:
virtual void Quack() override
{
cout << "Duck: Quack!" << endl;
};
virtual void Fly() override
{
cout << "Duck: I'm flying far..." << endl;
}
};
class WildTurkey : public ITurkey
{
public:
virtual void Gobble() override
{
cout << "Turkey: Gobble gobble" << endl;
};
virtual void FlyShortDistance() override
{
cout << "Turkey: I'm flying a short distance..." << endl;
}
};
class TurkeyAdapter : public IDuck
{
private:
shared_ptr<ITurkey> _turkey;
public:
TurkeyAdapter(shared_ptr<ITurkey> turkey)
{
_turkey = turkey;
}
virtual void Quack() override
{
_turkey->Gobble();
}
virtual void Fly() override
{
for(int i = 0; i<5; i++)
{
_turkey->FlyShortDistance();
}
}
};
int main()
{
shared_ptr<IDuck> duck = make_shared<MallardDuck>();
duck->Quack();
duck->Fly();
shared_ptr<ITurkey> turkey = make_shared<WildTurkey>();
shared_ptr<IDuck> turkeyAdapter = make_shared<TurkeyAdapter>(turkey);
turkeyAdapter->Quack();
turkeyAdapter->Fly();
return 0;
}
💭회고
무지성 unique_ptr 사용했다가 매개변수로 전달해서 어댑터의 변수 포인터로 가리키려했더니 에러 발생
포인터 변수와 객체 간의 관계에 대한 개념이 안 서 있었던 것 같다.
make를 사용해서 힙에 만든 객체는 포인터 변수들이 가리킬 수 있는 건데
unique는 가리킬 수 있는 변수가 하나만 가능!
그래서 대입을 하던 매개변수로 넘기던 unique_ptr은 move만 가능한 것!
3️⃣ 언리얼 네트워크
Unreal5 리슨 서버와 데디케이트 서버
1️⃣ 개요언리얼이 제공하는 서버인 데디케이트 서버와 리슨 서버에 대해 알아보고자 한다.먼저 데디케이트 서버와 리슨 서버는한국의 온라인 게임이 사용하는 Persistent 서버(항상 운영되는 상
yoosorang.tistory.com
HasAuthority가 정확하게 어떤 권한인지 모르겠어서 정리를 해보았다.
이를 찾아보면서 Host와 Server가 다른 것을 알 수 있었다!
💭회고
멀티플레이 과제를 진행 중에 있는데 실제로 서버-클라이언트 작업을 해보니 헷갈리는 부분이 너무 많았다.
아무래도 지금 위젯 - 플레이어 컨트롤러 - 게임모드를 오가면서 노드를 짜고 있는데
위젯과 게임모드 간에 정보를 주고 받을 때 플레이어 컨트롤러를 꼭 거쳐야 하는 것 때문에
뭔가 더 복잡해보이는 것 같다.
그래도 오늘 안에 어느정도 틀은 잡을 수 있을 것 같고 계속해서 노드를 만지다보니
좀 익숙해져가는 것 같기도 하다!