简体   繁体   English

设计模式:选择哪一个?

[英]Design pattern: Which one to choose?

First of all this is all just concept, I have no actual programming done yet. 首先,这只是概念,我还没有完成实际的编程。 This is the situation: 情况就是这样:

I have a Class A which uses the Filesystemwatcher to watch for changes in a folder. 我有一个A类,它使用Filesystemwatcher来监视文件夹中的变化。 (using events) (使用活动)

Then I have a 'Collection Class' B which has a List of A's. 然后我有一个'Collection Class'B,它有一个A的列表。

Now what I want to happen is as follows, 现在我想要发生的事情如下,

A Change happens with the folder, A detects this and sends a message to B, B transfers this message to Class C. Class C then begins a method which updates the GUI. 文件夹发生更改,A检测到此消息并向B发送消息,B将此消息传送到C类。然后,C类开始更新GUI的方法。 (What changes were made etc..) (做了哪些改变等...)

Now I have searched and thought pretty long on this subject, but can't find the solution. 现在我已经在这个主题上搜索并思考了很长时间,但找不到解决方案。 But, I have found 2 design patterns: 但是,我发现了2种设计模式:

Mediator and Observer. 调解员和观察员。

As a software engineer I have to some degree once made the Observer pattern so I know some of the basics there. 作为一名软件工程师,我曾在某种程度上制作了Observer模式,因此我了解了一些基础知识。

Now to my questions: 现在问我的问题:

  • What pattern is best to use in this situation? 在这种情况下最好使用什么模式?

  • How do I make it so that B transmits the message to C? 如何使B将消息发送给C?

  • Do I need custom Events / delegates to make A transmit data to B or can I use the Built-in events? 我是否需要自定义事件/代理才能将A传输数据发送给B,或者我可以使用内置事件吗?

PS: I'm using C# as my programming language. PS:我使用C#作为我的编程语言。

edit: Thanks to everyone for helping me, votes are on the way. 编辑:感谢大家帮助我,投票正在进行中。

Observer is fine. 观察者很好。 You can either make C an observer of B (so that B transmits events from A's to C), or make C listen directly to A's (this is probably the worse choice as it creates a direct dependency from C to A). 您可以使C成为B的观察者(以便B将事件从A发送到C),或使C直接监听A(这可能是更糟糕的选择,因为它创建了从C到A的直接依赖)。

Note that this basically a variation of Model-View-Controller , where A are the models and C the view. 请注意,这基本上是模型 - 视图 - 控制器的变体,其中A是模型,C是视图。 Now whether or not B would make a proper controller depends largely on its responsibilities: if it is only a collection of A's, it is not a good idea to make it a controller. 现在B是否会成为一个合适的控制器在很大程度上取决于它的职责:如果它只是一个A的集合,那么将它作为一个控制器并不是一个好主意。 Without more details about your classes and responsibilities, it is hard to say more. 没有关于你的课程和职责的更多细节,很难说更多。

For what I make out of this, there are a bunch of 'A' objects that pass on events asynchronously to a single B, that in turn passes that info on to a single C. 对于我所做的,有一堆'A'对象将事件异步传递给单个B,然后将该信息传递给单个C.

So, let B contain and observe the A's and let C observe B. 所以,让B包含并观察A's并让C观察B.

If you got a lot of A's, you might want to have B do some gathering/caching of A's events before notifying C. Especially if C is serving a user interface. 如果你有很多A,你可能想让B在通知C之前做一些A事件的收集/缓存。特别是如果C正在为用户界面提供服务。

Side note : don't over-patternize your software. 附注 :不要过度模式化您的软件。 Try to be open-minded and always find the simplest and easiest solution. 尽量保持思想开放,始终找到最简单,最简单的解决方案。 Only use a pattern where its appropriate, and not just because it's possible. 只使用适当的模式,而不仅仅是因为它是可能的。 I have seen many people throwing in proxies, command-patterns, observers, MVC's, mediators etc, where they were unneccesary. 我看到很多人投入代理,命令模式,观察者,MVC,调解员等,他们是不必要的。

Good luck. 祝好运。

public class A
{
    public event FileSystemEventHandler FileSystemEvent;

    A()
    {
        this.fsw = new FileSystemWatcher();
        this.fsw.OnFileSystemEvent += (sender, e) => 
            { if(this.FileSystemEvent != null) 
                 this.FileSystemEvent(this,e); };
    }
}

public class B
{
    public event FileSystemEventHandler FileSystemEvent;

    B()
    {
        this.RegisterAClasses();
        foreach( A item in this.AClasses )
             item.FileSystemEvent += (sender, e) =>
                 { if(this.FileSystemEvent != null) 
                      this.FileSystemEvent(sender, e) };
    }
}

public class C
{
    C()
    {
        this.RegisterBClass();
        this.BClass.FileSystemEvent += (sender, e) => 
             { /* update gui... */ };
    }
}

(psuedo code...) (伪代码......)

I ended up using nearly the same scenario for a demo of Reactive Extensions. 我最终使用几乎相同的方案来演示Reactive Extensions。

RX is a formalism of the observer pattern - but generalized to be the inverse/dual of the iterator pattern. RX是观察者模式的形式 - 但是泛化为迭代器模式的逆/双。

Details and source code - http://code.msdn.microsoft.com/RxDemos 详细信息和源代码 - http://code.msdn.microsoft.com/RxDemos

Observer is a proper pattern here. 观察者在这里是一个合适的模式。 I don't understant why u said this: 我不明白为什么你说这个:

Then I have a 'Collection Class' B which has a List of A's. 然后我有一个'Collection Class'B,它有一个A的列表。

because, to Observer pattern, I think B should observe A, so in class A, there some Bs for listen an event fire by A(a folder be changed). 因为,对于Observer模式,我认为B应该观察A,所以在A类中,有一些B用于通过A监听事件(文件夹被更改)。 Similarly, class C should observe B, so in class B there are some objects C register for listening event fire by B. The neccesary for a custom event or build in event depend on you class. 类似地,类C应该观察B,因此在类B中有一些对象C注册用于B的侦听事件触发。自定义事件或构建事件的必要性取决于您的类。 If its .NET's class, I think there's some event for notifying a change in a dicrectory. 如果是.NET的类,我认为有一些事件可以通知更改一个dicrectory。 If not, you should write your own events / delegate. 如果没有,您应该编写自己的事件/代理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM