简体   繁体   English

如何使此发布/子代码更具可读性?

[英]How can I make this pub/sub code more readable?

I am investigating the pub/sub pattern because I am reading a book that highly advocates event driven architecture, for the sake of loose coupling. 我正在研究pub / sub模式,因为我正在阅读一本极力主张事件驱动的体系结构的书,以实现松散耦合。 But I feel that the loose coupling is only achieved by sacrificing readability/transparency. 但是我觉得,松散耦合只能通过牺牲可读性/透明度来实现。

I'm having trouble understanding how to write easily-understood pub/sub code. 我在理解如何编写易于理解的pub / sub代码方面遇到麻烦。 The way I currently write my code results in a lot of one-to-one channels, and I feel like doing so is a bad practice. 我目前编写代码的方式会导致许多一对一的渠道,我觉得这样做是一种不好的做法。

I'm using require.js AMD modules, which means that I have many smaller-sized files, so I feel like it would be very difficult for someone to follow the flow of my publishes. 我使用的是require.js AMD模块,这意味着我有许多较小的文件,所以我觉得对于某人而言,要遵循我的发布流程非常困难。

In my example code below, there are three different modules: 在下面的示例代码中,有三个不同的模块:

  1. The UI / Controller module, handling user clicks UI /控制器模块,处理用户点击
  2. A translator module 转换器模块
  3. A data storage module 数据存储模块

The gist is that a user submits text, it gets translated to english, then stored into a database. 要点是用户提交文本,然后将其翻译成英文,然后存储到数据库中。 This flow is split into three modules in their own file. 此流程在各自的文件中分为三个模块。

// Main Controller Module
define(['pubSub'] function(pubSub) {

    submitButton.onclick(function() {
        var userText = textArea.val();
        pubSub.publish("userSubmittedText", userText);
    });

});

// Translator module
define(['pubSub'] function(pubSub) {

    function toEnglish(text) {
        // Do translation
        pubSub.publish("translatedText", translatedText);
    };

    pubSub.subscribe("userSubmittedText", toEnglish);

});


// Database module
define(['pubSub'] function(pubSub) {

    function store(text) {
        // Store to database
    };

    pubSub.subscribe("translatedText", store);

});

For a reader to see the complete flow, he has to switch between the three modules. 为了让读者看到完整的流程,他必须在这三个模块之间切换。 But how you would make clear where the reader should look, after seeing the first pubSub.publish("userSubmittedText", userText); 但是,在看到第一个pubSub.publish("userSubmittedText", userText);之后,您将如何明确读者的目光pubSub.publish("userSubmittedText", userText); ?

I feel like publishes are like a cliff hanger, where the reader wants to know what is triggered next, but he has to go and find the modules with subscribed functions. 我觉得发布就像悬崖峭壁,读者想知道下一步会触发什么,但是他必须去寻找具有订阅功能的模块。

I could comment EVERY publish, explaining what modules contain the functions that are listening, but that seems impractical. 我可以在每次发布时发表评论,解释哪些模块包含正在监听的功能,但这似乎不切实际。 And I don't think that is what other people are doing. 而且我不认为其他人正在这样做。

Furthermore, the above code uses one-to-one channels, which I think is bad style, but I'm not sure. 此外,以上代码使用一对一的渠道,我认为这是不好的风格,但我不确定。 Only the Translator module's toEnglish() function will ever subscribe to the pubSub channel "userSubmittedText" , yet I have to create the new channel for what is basically a single function call. 只有Translator模块的toEnglish()函数将订阅pubSub通道"userSubmittedText" ,但我必须为基本上是单个函数调用的通道创建一个新通道。 While this way my Controller module doesn't have to have Translator as a dependency, it just doesn't feel like true decoupling. 通过这种方式,我的Controller模块不必将Translator作为依赖项,但是就好像没有真正的解耦一样。

This lack of function flow transparency is concerning to me, as I have no idea how someone reading such source code would know how to follow along. 功能流程透明性的缺乏对我来说很重要,因为我不知道阅读此类源代码的人如何知道如何遵循。 Clearly I must be missing something important. 显然,我一定错过了一些重要的事情。 Maybe I'm not using a helpful convention, or maybe my publish event names are not descriptive enough? 也许我没有使用有用的约定,或者我的发布事件名称描述性不够?

Is the loose coupling of pub/sub only achieved by sacrificing of flow transparency? 是否只能通过牺牲流的透明度来实现pub / sub的松散耦合?

The idea of the publish subscribe pattern is that you don't make any assumptions about who has subscribed to a topic or who is publishing. 发布订阅模式的想法是,您不必对谁已订阅主题或谁正在发布进行任何假设。 From Wikipedia ( http://en.wikipedia.org/wiki/Publish%E2%80%93subscribe_pattern ): 从Wikipedia( http://en.wikipedia.org/wiki/Publish%E2%80%93subscribe_pattern ):

[...] Instead, published messages are characterized into classes, without knowledge of what, if any, subscribers there may be. [...]相反,已发布的消息被分类为类,而不知道可能有多少订户。 Similarly, subscribers express interest in one or more classes, and only receive messages that are of interest, without knowledge of what, if any, publishers there are. 同样,订户对一个或多个类别表示兴趣,并且仅接收感兴趣的消息,而不知道那里有什么发布者。

If your running code doesn't make any assumptions, your comments shouldn't do either. 如果您的运行代码没有做任何假设,则您的注释也不应做任何事情。 If you want a more readable way of module communication, you can use requirejs' dependency injection instead, which you already do with your pubsub module. 如果您想要一种更具可读性的模块通信方式,则可以改用requirejs的依赖注入,您已经在pubsub模块中进行了注入。 This way, you could make it easier to read the code (which brings other disadvantages). 这样,您可以更轻松地阅读代码(这带来了其他缺点)。 It all depends on what you want to achieve... 这完全取决于您想要实现的目标...

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

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