Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sharing a variable between a class and its member

If you have a class that contains a state variable and two member classes that need access to it and operate asynchronously. What is the best way to implement this?

An example

 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
}

These classes operate asynchronously. Both DeliveryMan and Supplier classes need to be able to read/write the state. DeliveryMan pushes out the state of the restaurant and the Supplier listens for its supplier's status.

Is there a better way to design this or a way to implement it with minimal coupling without giving DeliveryMan or Supplier a reference to its owner Restaurant.

like image 859
eddiehobbes Avatar asked Oct 09 '22 19:10

eddiehobbes


1 Answers

Well i would pass the state as constructor parameter to your two inner classes and considering that it is a reference type it can be modified as well.

like image 61
Davide Piras Avatar answered Oct 18 '22 16:10

Davide Piras