订阅活动的这种模式可以吗?

时间:2015-11-22 19:05:53

标签: c# design-patterns system.reactive

我希望有一个记录器可以使用#34;我的申请。我的应用程序记录有趣的东西,订阅者订阅这些消息。例如,订户可能将消息放入数据库,Windows事件日志或消息总线等

我所写的内容的简化复制如下:

//Link up any subscribers
new ConsoleLogListener(logger.LogMessagesObservable);
new WindowsEventListener(logger.LogMessagesObservable);
//... more listeners go here. 

但我对代码看起来并不满意:

Itemid      Username            Rate        Opinion     
0944947     pooh_sweety_70      3.2         Negative
0468569     pooh_sweety_70                  Positive
0411008     liamvdheuvel        2.1         Negative
0468569     liamvdheuvel        3.4         Negative
1375666     liamvdheuvel        1.5         Negative
0903747     liamvdheuvel        5.5         Negative
0412142     rzajac              4.8         Negative
2267998     rzajac              9.2         Positive
1074638     rzajac              7.4         Positive
1182345     rzajac              6.1         Negative
0468569     rzajac              3.3         Negative
1856010     michaelsmith01      9.9         Positive
1486217     michaelsmith01                  Positive
1300854     michaelsmith01      2.4         Negative
2267998     pooh_sweety_70      8.4         Positive
2911666     aswilliams40        8.7         Positive
1074638     aswilliams40        9.4         Positive
0468569     aswilliams40        8.2         Positive

由于某种原因让我感到烦恼,但我不能指责它。我想新建一个类似的监听器看起来很奇怪,而不是保留对它的引用。

我尝试做的事情是否有更好/更受欢迎的模式?

1 个答案:

答案 0 :(得分:1)

我可能希望听众能够实施logger.LogMessagesObservable.Subscribe(new ConsoleLogListener()); ,然后你就这样连接两者:

import java.util.Scanner;
import java.io.*;
public class Student_Test {

public static void main(String[] args) throws IOException {
    Scanner in = new Scanner(System.in); // sets up scanner
    System.out.print("Enter file name: "); //user provides file name and location
    String userFile = in.nextLine(); // accepts input from user
    File file = new File(userFile); //uses the file method to import the data
    Scanner inputFile = new Scanner(file); // uses scanner to read the data
    System.out.print(inputFile.Out());



}

}