在 Dart 中实现观察者模式 [英] Implement an Observer pattern in Dart

查看:31
本文介绍了在 Dart 中实现观察者模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 Dart 中实现一个观察者模式,但我不知道如何去做.

I would like to implement an observer pattern in Dart but I'm not sure how to go about it.

假设我有一堂课:

class MyClass {

  String observed_field;

}

现在,每当我更改字段时,我都想将observed_field changed"字符串打印到控制台中.使用自定义 setter 非常简单:

Now, whenever I change the field, I'd like to print "observed_field changed" string into the console. Pretty simple to do with a custom setter:

class MyClass {

  String _observed_field;

  get observed_field    => _observed_field;
  set observed_field(v) {
    _observed_field = v;
    print("observed_field changed");
  }

}

现在,当然,如果我没有一个,而是其中许多字段,我不想创建所有这些 getter 和 setter.显而易见的理论解决方案是将它们动态添加到类中,使用类似这样的东西(不是工作代码,只是我希望它看起来如何的一个例子):

Now, of course, if I have not one, but many of those fields, I wouldn't want to create all those getters and setters. The obvious theoretical solution is to have them dynamically added to the class with something like this (not a working code, just an example of how I wish it looked):

class MyClass

  String  _observeable_field;
  String  _observeable_field_2;

  observe(#observeable_field, #observeable_field_2);

end

这可能吗?此外,如果不在 observe() 调用上方定义这些字段,而是编写如下内容,那就太棒了:>

Is it even possible? Additionally, it would be super awesome to not have those fields defined above the observe() call, but rather write something like:

observe(String: #_observeable_field, String: #_observeable_field_2);

以便自动声明这些字段.

So that those fields are declared automatically.

推荐答案

这是一种使用 观察 包.该示例取自该包中的代码注释(并适应您上面的示例).本质上,您可以使用 @observable 注释来注释您想要观察的字段,然后监听更改(您通过调用 Observable.dirtyCheck() 触发);

Here's a way to do it using the Observe package. The example is taken from code comments in that package (and adapted to your example above). Essentially, you annotate fields you want to be observable with the @observable annotation, and then listen for changes (which you trigger with the call to Observable.dirtyCheck();

首先,在您的 pubspec.yaml

dependencies:
  observe: any

然后创建一个快速测试程序...

Then create a quick test program...

import 'package:observe/observe.dart';

class MyClass extends Object with Observable {
  @observable String observedField = "Hello";

  toString() => observedField.toString(); 
}

main() {
  var obj = new MyClass();

  // anonymous function that executes when there are changes
  obj.changes.listen((records) {
    print('Changes to $obj were: $records');
  });


  obj.observedField = "Hello World";

  // No changes are delivered until we check for them
  Observable.dirtyCheck();

  print('done!');
}

这会产生以下输出:

Changes to Hello World were: [#<PropertyChangeRecord Symbol("observedField") from: Hello to: Hello World>]
done!

更新以回应评论...更新示例以省略 Observable.dirtyCheck(),您可以使用 setter 和 notifyPropertyChanged,而不是在 ChangeNotifier

Update in response to comments... Updating the example to omit the Observable.dirtyCheck() you can use a setter and notifyPropertyChanged, with the class instead mixing in ChangeNotifier

class MyClass2 extends Object with ChangeNotifier {

  String _observedField = "Hello";

  @reflectable get observedField    => _observedField;
  @reflectable set observedField(v) {
    _observedField = notifyPropertyChange(#observedField, _observedField, v);    
  }

  toString() => observedField;

}

这篇关于在 Dart 中实现观察者模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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