Policy based design in Python [closed]

2020-01-19 05:29发布

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条回答
仙女界的扛把子
2楼-- · 2020-01-19 05:52

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.

查看更多
登录 后发表回答