是否有推荐的设计模式用于与过程通信参数? [英] Is there a recommended design pattern for communicating parameters with a process?

查看:163
本文介绍了是否有推荐的设计模式用于与过程通信参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个由许多类构成的信号处理途径。每个处理类都是较小类的组合,每个类都有自己的参数。

I have a signal processing pathway made of many classes. Each processing class is a composition of smaller classes, each of which have their own parameters.

到目前为止,我已经懒惰并将所有的处理参数存储在一个单独的参数类。我使这个朋友所有的处理类,以便他们可以直接访问其数据成员。但是,这使得各个块和参数类之间的耦合非常强,使得设计完全不灵活。

Until now, I have been lazy and stored all of the processing parameters in a separate parameter class. I made this a friend of all of the processing classes so they could just access its data members directly. However, this makes for very strong coupling between the individual blocks and the parameter class, making the design completely inflexible.

我重新设计代码,以使每个小进程拥有它自己的私人数据成员,它需要用于减少耦合的功能。但现在,如果加载了一组新的参数,我需要一个复杂的方法,在每个单独的处理块中设置所有参数(使用访问器函数)。该方法中的命令将强烈耦合到该过程。

I am redesigning the code so that each small process owns it's own private data members that it needs in order to function to reduce the coupling. But now, if a new set of parameters are loaded, I need a complex method that sets all of the parameters (using accessor functions) in each of the separate processing blocks. The commands within this method would be strongly coupled to the process. How do I minimize this coupling?

推荐答案

我建议使用类似上下文模式。在构建进程时,使用上下文对象或对象(您可以为各种进程拥有各种上下文)加载它们。然后让每个进程从给定的上下文对象请求和检索必要的参数。这样,您就可以将设置过程参数的职责全部移交到这些过程。换句话说,进程知道它需要什么参数,所以进程可以从给定的上下文对象请求它们并直接设置其私有存储的成员。

I'd suggest something similar to Context Pattern. On construction of your processes, load them with the context object or objects (you can have variety of contexts for various processes). Then let each process to request and retrieve necessary parameters from given context object. This way, you move the responsibility of setting process parameters entirely to those processes. In other words, process knows what parameters it needs, so process can request them from given context object and set its privately stored members directly.

注意,有各种风格上下文模式,一般来说它是相当灵活的概念。

Note, there are various flavours of Context Pattern, generally it's fairly flexible concept.

这篇关于是否有推荐的设计模式用于与过程通信参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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