상태 변수가 있고 해당 클래스에 액세스해야하고 비동기 적으로 작동하는 두 개의 멤버 클래스가있는 클래스가있는 경우. 이것을 구현하는 가장 좋은 방법은 무엇입니까?클래스와 해당 멤버 간의 변수 공유
이 클래스는 비동기 적으로 작동 할
public enum RestaurantState
{
BREAKFAST,
LUNCH,
DINNER
}
public class Restaurant
{
//Below need access to state
private DeliveryMan pizzaDriver ;
private Supplier butcherShop ;
internal RestaurantState state ;
}
public DeliveryMan
{
//Uses a System.Timers.Timer
//Wakes up and does work every a minute
//Needs to inform state of restaurant
}
public Supplier
{
//Waits and listens for requests to accept deliveries
//If suppliers run out we need to change the restaurant state based on our own current state
}
예. DeliveryMan 및 Supplier 클래스 모두 상태를 읽고 쓸 수 있어야합니다. DeliveryMan은 레스토랑의 상태를 푸시하고 공급 업체는 공급 업체의 상태를 수신합니다.
최소 Coupling으로이를 구현하는 더 좋은 방법은 DeliveryMan 또는 Supplier에게 owner Restaurant에 대한 참조를 제공하지 않는 것입니다.
ResturantState가 비공개로 선언되었으므로 다른 클래스가이를 업데이트 할 것으로 예상합니까? – user957902
감사합니다. 액세스 수정자를 내부 – eddiehobbes
으로 변경했습니다. 실제 상태 인 경우 내 (편집 된) 답변과 같은 2 개의 bool로 클래스를 만들 것을 권합니다. – Davy8