使用代理来模拟连接的对象 [英] Using Delegates to simulate connected objects

查看:93
本文介绍了使用代理来模拟连接的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个应用程序,我需要模拟如电力或天然气以任意方式相互连接对象的大型网络中的资源分配。没有物理需要进行模拟。 ,资源的单位就在流对象时要求他们

I'm writing an application where I need to simulate the distribution of a resource such as electricity or gas among a large network of objects connected to each other in arbitrary ways. No physics need to be simulated. Just the flow of the units of resources when objects ask for them.

考虑如下图:

ObjA <---> ObjB <---> PwrA <---> ObjF
            /\                                ObjG
   ObjE<---/  \---> PwrB



压水堆对象时,由<强烈要求能够提供资源的电力>的OBJ 的对象。
我需要在这里模拟的是,如果ObjA需要50电力应该将消息发送给所有连接的同龄人不关心它关系到谁,只能从同行获得资源重新它连接到直接或间接的影响。

The Pwr objects can provide resource electricity when asked by the Obj objects. What I need to simulate here is that if ObjA needs 50 electricity it should send a message to all connected peers without caring who it goes to and should get the resource back only from peers it is connected to directly or indirectly.

我不是在这里模拟延迟或任何这样的传输将瞬间。从编程的角度来看,这是所有应用程序(没有真正的网络或类似的东西)内完全本地的。

I'm not simulating latency here or anything so transmission will be instant. From a programming point of view this is all entirely local within the application (No real networking or anything like that).

我在这里的挑战是试图在一个模块化编程此和清洁的方式。我想,以避免本地连接的节点,如何应对可能的话的消息大ifelse语句巨头名单。

My challenge here is trying to program this in a modular and clean way. I'd like to avoid giant lists of locally connected nodes and large ifelse statements about how to deal with messages if possible.

我想都实现我的目标,也是学习新鲜玩意。我一直在阅读的文章和书籍,编程概念,现在几个星期,一个我认为可能是一个梦幻般的解决办法是C#的代表。

I want to both achieve my goal and also learn something new. I've been reading articles and books and programming concepts for a few weeks now and one that I think might be a fantastic solution is C# Delegates.

我已经做了一些原型设计和管理,以用MessageOut委派安装对象,其他连接的对象可以订阅和监听传出消息来处理。

I've done some prototyping and managed to setup objects with a MessageOut Delegate that other connected objects can subscribe to and listen for outgoing messages to deal with.

这工作得很好,但我有以下不足之处是由于这个概念我在经验:

This works quite well but I have the following short-comings due to my in-experience with the concept:


  • 我不知道该如何处理与之间的消息传递循环引用连接的对象:在如果ObjB广播电力ObjA,PwrA,PwrB消息上图中,OBJE将会收到通知,这将反过来所有进程,然后通知ObjB。这将反过来通知...等。联网与此交易的方法是简单地播出,除了接收它的端口的所有端口(对象听了代表)。我怎么能与代表实现这个?一台广播将不起作用。

  • 收听代表有效地创建一个双向通信。为了实现双向通信我需要有对象,倾听对方传出的消息。但是,如果没有一个以上上述的另一个循环引用肯定?

推荐答案

代表可能会笨拙而凌乱在我看来。

Delegates would be unwieldy and messy in my opinion.

你有没有看TPL数据流? http://msdn.microsoft.com/en-gb/library/hh228603.aspx

Have you looked at TPL Dataflow? http://msdn.microsoft.com/en-gb/library/hh228603.aspx

它提供了一些块具有行为,如排队的请求,发送请求等,你可以用它来模拟推拉的这些虚拟资源。

It provides a number of "blocks" that have behaviour such as queuing requests, sending requests etc, you could use it to simulate the push and pull of these virtual resources.

所有这些连接在一起,并处理循环依赖神奇的是所有照顾。

All the magic for connecting them together and dealing with circular dependencies is all taken care of.

这篇关于使用代理来模拟连接的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆