选择一种类型的类来在运行时实现功能?

发布于 2024-09-19 21:42:54 字数 373 浏览 2 评论 0原文

我想将数据和功能描述发送到远程站点。在远程站点接收到数据后,我想查看描述并创建一个对象(通过工厂方法),当我在其上调用 exec 时,它完全按照我想要的方式进行。

示例:

1) 发送 [3, (add 5) ] =>接收(对象); obj->exec()->; 8

2) 发送 [3, (add -1, mul 2) ] =>接收(对象); obj->exec()->; 4

我想过以某种形式的多重继承来拥有 adder 和 multer 类,但无法弄清楚任何事情,因为这涉及从不同的功能排列创建大量类。我想我需要学习一些东西:) 模板?我主要关心的是 exec() 函数中的条件为零也可以轻松添加新功能。

谢谢

I want to send data and a capability description to a remote site. Upon receiving the data at the remote site, I want to look at the description and create an object (via a factory method ) doing exactly what I want when I invoke exec on it.

Examples:

1) send [3, (add 5) ] => receive(obj); obj->exec() -> 8

2) send [3, (add -1, mult 2) ] => receive(obj); obj->exec() -> 4

I thought of having adder and multer classes in some form of multiple inheritance but could not figure out anything as this involves creating lots of classes from different permutations of capabilities. I think I'll need to learn something :) templates? My main concern is to have zero conditionals in the exec() function also easily add new capabilities.

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

临走之时 2024-09-26 21:42:54

你说你不想为不同的能力排列构建单独的类,我同意。但是你能否将你的“能力”分成一组原子操作和另一组组合器。如果它们都派生自一个通用的“执行器”对象,并具有虚拟的“exec”方法,则可能会成功:

class executor {
  public:
  virtual double exec();
};

class constant_exec : public executor {
  public:
  constant_exec(double value) : m_value(value) {}
  double exec() {return m_value;}
  private:
  double m_value;
};

class add_op : public executor {
  public:
  add_op(executor const *lhs, executor const* rhs) : m_lhs(lhs), m_rhs(rhs){}
  double exec() {return rhs->exec + lhs->exec;}
  private:
  executor const* m_rhs, m_lhs;
};
  ... and so on

这样您就可以构建任意复杂的操作。您确实需要担心正确处置执行程序对象,因此您可能希望考虑使用共享(可能是弱)指针,尽管这会对性能产生一些轻微的影响。

-哑光

You say that you don't want to build separate classes for different permutations of abilities, to which I agree. But can you separate out your "abilities" into a set of atomic operations, and another set of combinators. If they all derive from a common 'executor' object, with a virtual 'exec' method, that might do the trick:

class executor {
  public:
  virtual double exec();
};

class constant_exec : public executor {
  public:
  constant_exec(double value) : m_value(value) {}
  double exec() {return m_value;}
  private:
  double m_value;
};

class add_op : public executor {
  public:
  add_op(executor const *lhs, executor const* rhs) : m_lhs(lhs), m_rhs(rhs){}
  double exec() {return rhs->exec + lhs->exec;}
  private:
  executor const* m_rhs, m_lhs;
};
  ... and so on

This way you can build up arbitrarily complicated operations. You do need to worry about correctly disposing of the executor objects, so you may wish to consider using shared (and possibly weak) pointers, though that will have some slight impact on performance.

-matt

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文