在类及其成员之间共享变量

发布于 2024-12-09 14:35:13 字数 850 浏览 0 评论 0原文

如果您有一个包含状态变量的类和两个需要访问它并异步操作的成员类。实现这一点的最佳方法是什么?

示例

 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 推送餐厅的状态,Supplier 监听其供应商的状态。

是否有更好的方法来设计它,或者以最小的耦合来实现它,而不需要向 DeliveryMan 或 Supplyer 提供对其所有者 Restaurant 的引用。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

巷子口的你 2024-12-16 14:35:13

好吧,我会将状态作为构造函数参数传递给您的两个内部类,并考虑到它是一个引用类型,它也可以修改。

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.

孤独患者 2024-12-16 14:35:13

也许您可以在 DeliveryManSupplier 类上创建事件,这些事件在需要更新状态时触发。餐厅可以订阅这些事件,并在调用事件处理程序时相应地更新自己的状态。

Perhaps you can create events on the DeliveryMan and Supplier classes that are fired when the state needs to be updated. The Restaurant can subscribe to these events and update its own state accordingly when the event handler(s) are invoked.

燃情 2024-12-16 14:35:13

如果 RestaurantState 是或可以制成一个保存状态的对象而不是状态本身,那么您可以按照@Davide的答案进行操作并将其传递给构造函数。

但是,如果它是像 enum 这样的值类型,那么我认为 event 是正确的选择。

DeliveryMan 引发具有新状态的事件,Restaurant 侦听并更新其内部状态。

当状态发生变化时,Restaurant 可以调用 StateChanged 方法或 Supplier 上的类似方法。或者,Supplier 可以使用特殊的 RestaurantStateEventArgsRestaurant 可以侦听并填充事件的内容来引发事件 args 与状态。

不过,根据用例的不同,仅引用 Restaurant 可能并不可怕,即使它确实变得紧密耦合。

编辑:实际上,如果DeliveryManSupplier需要访问RestaurantState,那么它们已经在某种程度上与餐厅相关联,所以除非你有一个比 RestaurantState 更通用的“状态”类型,它们已经耦合了。

有时最好退后一步,看看

a) 解耦在特定场景中是否确实有帮助,并且
b) 你正在做的事情是否实际上足够解耦而有用。

在这种情况下,您仍然无法为家具店重用 DeliveryManSupplier

旁注:

OPEN,
CLOSED,
LOW_ON_SUPPLIES

这些并不是枚举的最佳选择,因为它们并不都是相互排斥的。如果它是一个类可能会更好:

public class RestaurantState
{
  public bool IsOpen { get; set; }
  public bool IsLowOnSupplies { get; set; }
}

在这种情况下,@Davide 将 RestaurantState 传递到 DeliveryManSupplier 的构造函数中的答案效果很好。

If RestaurantState is or can be made into an object that holds the state rather than the state itself, then you can do as @Davide's answer and pass it in to the constructor.

However if it's a value type like an enum then I think events are the way to go.

DeliveryMan raises an event with the new state, that Restaurant listens to and updates it's internal state.

Restaurant can then call a StateChanged method or something similar on Supplier when the state changes. Or Supplier can raise an event with a special RestaurantStateEventArgs or something that the Restaurant can listen to and populate the event args with the state.

Depending on the use-case though, it may not be terrible to just have a reference to Restaurant even though it does become tightly coupled.

Edit: Actually if DeliveryMan and Supplier need access to RestaurantState then they are already somewhat tied to Restaurants, so unless you have a more generic type of "state" than RestaurantState they are already coupled.

Sometimes it's good to take a step back and see if

a) decoupling is actually helpful in a particular scenario and
b) whether what you're doing is actually decoupled enough to be useful.

In this scenario, you still couldn't reuse DeliveryMan and Supplier for say a furniture store.

As a sidenote:

OPEN,
CLOSED,
LOW_ON_SUPPLIES

These aren't really the best choices for an enum, since they're not all mutually exclusive. it may be better if it were a class so:

public class RestaurantState
{
  public bool IsOpen { get; set; }
  public bool IsLowOnSupplies { get; set; }
}

In that case, @Davide's answer for passing RestaurantState into the constructor of DeliveryMan and Supplier works well.

雪化雨蝶 2024-12-16 14:35:13

我将从餐厅类中取出状态,并创建一个 StateManager 类,该类是其他类的单例或工厂。很难给出更完整的答案,因为您的面向对象设计没有提供太多内容。

var restaurant = new Restaurant();
var supplier = new Supplier();
StateManager.GetState(restaurant);
StateManager.GetState(supplier);

I would take the state out of the restaurant class and make a StateManager class that was a singleton or a factory for the rest of the other classes. Its hard to give a more complete answer since your OO design doesnt give much to go on.

var restaurant = new Restaurant();
var supplier = new Supplier();
StateManager.GetState(restaurant);
StateManager.GetState(supplier);
野侃 2024-12-16 14:35:13

我将创建一个 Order 类,其中包含您在另一个类中需要的信息。还可以使用您检查计时器事件的队列。当您使订单出列时,请查看 Order.State(例如)。将队列放入具有 Enqueue 和 Dequeue 方法的公共静态类中。

当 DeliveryMan 计时器事件触发时,使订单出列。

您提到一切都是异步的,因此您可以查看 ConcurrentQueue。由于供应商等待通知,您可以使用 IObserver/IObservable 使用序列化的订单对象向供应商发送流消息......

只是一些可能有所帮助的想法。

I would create an Order class that contains the information you need in another class. Also use a Queue which you check on a Timer event. When you dequeue an Order, look at Order.State (for instance). Put the Queue in a public static class with Enqueue and Dequeue methods.

When the DeliveryMan timer event fires, dequeue the Order.

You mention that everything is async, so you might check out ConcurrentQueue. Since Supplier waits for notification, you can use IObserver/IObservable to send a stream message to Supplier with a serialized Order object ...

Just some thoughts that might help out.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文