pythonpolicy-based-design

Policy based design in Python


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.


Solution

  • 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.