Policy based design in Python [closed]

非 Y 不嫁゛ 提交于 2019-12-13 10:57:06

问题


I was very impressed by the policy based design described in Modern C++ Design by Andrei Alexandrescu and tried it successfully in some light weight programs. Now I have to write a real world system in Python and I think that approach would be very useful here. However, I can't find a single example of this approach in Python. Is it not recommended in Python or are there better alternatives? Can someone point me to an example of policy based design in Python? My aim is to develop an auctioning system and I want to be able to choose the auction strategy - English, Dutch, Silent, etc - at run time.

Since Python and Ruby are so similar, I guess an example in Ruby will also do.


回答1:


My aim is to develop an auctioning system and I want to be able to choose the auction strategy - English, Dutch, Silent, etc - at run time.

You could just use strategy pattern (also known as the policy pattern) to accomplish exactly this.

You could also use mixins to provide the strategy instead of composition.



来源:https://stackoverflow.com/questions/26533073/policy-based-design-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!