在类及其成员之间共享变量
如果您有一个包含状态变量的类和两个需要访问它并异步操作的成员类。实现这一点的最佳方法是什么?
示例
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
好吧,我会将状态作为构造函数参数传递给您的两个内部类,并考虑到它是一个引用类型,它也可以修改。
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.
也许您可以在
DeliveryMan
和Supplier
类上创建事件,这些事件在需要更新状态时触发。餐厅可以订阅这些事件,并在调用事件处理程序时相应地更新自己的状态。Perhaps you can create events on the
DeliveryMan
andSupplier
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.如果
RestaurantState
是或可以制成一个保存状态的对象而不是状态本身,那么您可以按照@Davide的答案进行操作并将其传递给构造函数。但是,如果它是像
enum
这样的值类型,那么我认为event
是正确的选择。DeliveryMan
引发具有新状态的事件,Restaurant
侦听并更新其内部状态。当状态发生变化时,
Restaurant
可以调用StateChanged
方法或Supplier
上的类似方法。或者,Supplier
可以使用特殊的RestaurantStateEventArgs
或Restaurant
可以侦听并填充事件的内容来引发事件
args 与状态。不过,根据用例的不同,仅引用
Restaurant
可能并不可怕,即使它确实变得紧密耦合。编辑:实际上,如果
DeliveryMan
和Supplier
需要访问RestaurantState
,那么它们已经在某种程度上与餐厅相关联,所以除非你有一个比RestaurantState
更通用的“状态”类型,它们已经耦合了。有时最好退后一步,看看
a) 解耦在特定场景中是否确实有帮助,并且
b) 你正在做的事情是否实际上足够解耦而有用。
在这种情况下,您仍然无法为家具店重用
DeliveryMan
和Supplier
。旁注:
这些并不是枚举的最佳选择,因为它们并不都是相互排斥的。如果它是一个类可能会更好:
在这种情况下,@Davide 将 RestaurantState 传递到
DeliveryMan
和Supplier
的构造函数中的答案效果很好。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 thinkevent
s are the way to go.DeliveryMan
raises an event with the new state, thatRestaurant
listens to and updates it's internal state.Restaurant
can then call aStateChanged
method or something similar onSupplier
when the state changes. OrSupplier
can raise anevent
with a specialRestaurantStateEventArgs
or something that theRestaurant
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
andSupplier
need access toRestaurantState
then they are already somewhat tied to Restaurants, so unless you have a more generic type of "state" thanRestaurantState
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
andSupplier
for say a furniture store.As a sidenote:
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:
In that case, @Davide's answer for passing RestaurantState into the constructor of
DeliveryMan
andSupplier
works well.我将从餐厅类中取出状态,并创建一个 StateManager 类,该类是其他类的单例或工厂。很难给出更完整的答案,因为您的面向对象设计没有提供太多内容。
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.我将创建一个
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.