选择一种类型的类来在运行时实现功能?
我想将数据和功能描述发送到远程站点。在远程站点接收到数据后,我想查看描述并创建一个对象(通过工厂方法),当我在其上调用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你说你不想为不同的能力排列构建单独的类,我同意。但是你能否将你的“能力”分成一组原子操作和另一组组合器。如果它们都派生自一个通用的“执行器”对象,并具有虚拟的“exec”方法,则可能会成功:
这样您就可以构建任意复杂的操作。您确实需要担心正确处置执行程序对象,因此您可能希望考虑使用共享(可能是弱)指针,尽管这会对性能产生一些轻微的影响。
-哑光
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:
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