마무리 3주차 과제 해결
배송 추적 시스템은
배송 상태가 변경될 때, 고객에게 자동으로 알림을 보내는 시스템 입니다.
옵저버 패턴을 활용해서
배송회사와 다양한 유형의 고객 간의 알림 시스템을 구현 해보세요
세부 요구사항은 아래와 같습니다.
배송 회사(Delivery Service)
1) 고객들을 관리하며 배송 상태가 변경되면 모든 고객에게 알림을 보냅니다.
2) 고객 등록, 고객 제거, 배송 상태 업데이트 기능을 제공해야 합니다.
고객(Customer)
1) 고객은 배송 상태가 변경될 때 알림을 받습니다.
2) 고객 유형은 다음 세 가지로 구분 됩니다.
+ 일반 고객(RegularCustomer)
+ VIP 고객(VIPCustomer)
+ 기업 고객(BisunessCustomer)
아래는 DeliveryService의 일부분입니다. 이를 참조해서 구현해보세요
코드의 전체적인 구조는 아래와 같습니다.

코드 실행시 출력값 예시

#include <iostream>
#include <vector>
#include <string>
using namespace std;
// 고객 인터페이스 (Observer 역할)
class Customer {
public:
virtual void update(const string& status) = 0; // 순수 가상 함수
};
// 일반 고객 클래스
class RegularCustomer : public Customer {
private:
string name;
public:
RegularCustomer(const string& name) : name(name) {}
void update(const string& status) {
cout << "Regular customer " << name << " received update: " << status << endl;
}
};
// TODO: VIP 고객 클래스 (Customer를 상속받아 구현)
// 요구 사항:
// - 고객 이름을 저장하는 멤버 변수 `name`을 추가하세요.
// - 생성자에서 이름을 초기화하세요.
// - `update` 메서드를 구현하여 "VIP customer [이름] received VIP update: [배송 상태]" 형식으로 출력되도록 하세요.
// TODO: Business 고객 클래스 (Customer를 상속받아 구현)
// 요구 사항:
// - 고객 이름을 저장하는 멤버 변수 `name`을 추가하세요.
// - 생성자에서 이름을 초기화하세요.
// - `update` 메서드를 구현하여 "Business customer [이름] received Business update: [배송 상태]" 형식으로 출력되도록 하세요.
// TODO: 배송 회사 클래스 (DeliveryService)
// 요구 사항:
// - `customers`라는 고객 리스트를 저장하는 멤버 변수를 추가하세요.
// - `currentStatus`라는 현재 배송 상태를 저장하는 멤버 변수를 추가하세요.
// - 고객을 추가하는 `addCustomer` 메서드를 구현하세요.
// - 고객을 제거하는 `removeCustomer` 메서드를 구현하세요.
// - 배송 상태를 업데이트하고 모든 고객에게 알리는 `updateStatus` 메서드를 구현하세요.
// - 등록된 모든 고객에게 상태를 전달하는 `notifyCustomers` 메서드를 구현하세요.
// Main 함수
int main() {
DeliveryService service;
// 고객 객체 생성
RegularCustomer* customer1 = new RegularCustomer("Alice");
VIPCustomer* customer2 = new VIPCustomer("Bob");
BusinessCustomer* customer3 = new BusinessCustomer("CompanyX");
// 고객 등록
service.addCustomer(customer1);
service.addCustomer(customer2);
service.addCustomer(customer3);
// 배송 상태 업데이트 및 알림
cout << "Updating status: 배송 준비 중" << endl;
service.updateStatus("배송 준비 중");
cout << "\nUpdating status: 배송 완료" << endl;
service.updateStatus("배송 완료");
// 메모리 해제
delete customer1;
delete customer2;
delete customer3;
return 0;
}
#include <iostream>
#include <vector>
#include <string>
using namespace std;
// 고객 인터페이스 (Observer 역할)
class Customer {
public:
virtual void update(const string& status) = 0; // 순수 가상 함수
};
// 일반 고객 클래스
class RegularCustomer : public Customer {
private:
string name;
public:
RegularCustomer(const string& name) : name(name) {}
void update(const string& status) {
cout << "Regular customer " << name << " received update: " << status << endl;
}
};
class VIPCustomer : public Customer {
private:
string name;
public:
VIPCustomer(const string& name) : name(name) {}
void update(const string& status) {
cout << "VIP customer " << name << " received update: " << status << endl;
}
};
class BusinessCustomer : public Customer {
private:
string name;
public:
BusinessCustomer(const string& name) : name(name) {}
void update(const string& status) {
cout << "Business customer " << name << " received Business update: " << status << endl;
}
};
class DeliveryService {
private:
vector<Customer*> customers;
string currentStatus;
public:
void addCustomer(Customer* customer) {
customers.emplace_back(customer);
}
void removeCustomer(Customer* customer) {
auto it = find(customers.begin(), customers.end(), customer);
if (it != customers.end()) {
customers.erase(it);
}
}
void updateStatus(string status) {
currentStatus = status;
notifyCustomers();
}
void notifyCustomers() {
for (Customer* c : customers) {
c->update(currentStatus);
}
}
};
// Main 함수
int main() {
DeliveryService service;
// 고객 객체 생성
RegularCustomer* customer1 = new RegularCustomer("Alice");
VIPCustomer* customer2 = new VIPCustomer("Bob");
BusinessCustomer* customer3 = new BusinessCustomer("CompanyX");
// 고객 등록
service.addCustomer(customer1);
service.addCustomer(customer2);
service.addCustomer(customer3);
// 배송 상태 업데이트 및 알림
cout << "Updating status: 배송 준비 중" << endl;
service.updateStatus("배송 준비 중");
cout << "\nUpdating status: 배송 완료" << endl;
service.updateStatus("배송 완료");
// 메모리 해제
delete customer1;
delete customer2;
delete customer3;
return 0;
}
중요한 건
updateStatus 랑 notifyCustomers 부분으로
옵저버 패턴에 대한 학습내용을 복습하면서 채웠다.
updateStatus 에서 우선 "DeliveryService" 클래스에 있는
currentStatus 변수를 업데이트 해주면서
밑에 작성할 notifyCustomers 함수를 호출하게 하면서
요구사항에 있는
"배송상태를 업데이트하고 -> 고객에게 알리는" 구현을 정직하게 작성해주고
notifyCustomers 함수에선
Customers 벡터를 순회하면서
각 고객들에게 알림을 보내는데, 알림을 보내면
`Customers c->update(currentStatus)` 를 통해
구체 옵저버 클래스에 있는 update 함수를 호출시켜
배송 상태를 업데이트 해주는 구성