Categories
Design Patterns Lua Programming

New release of lua_objects for Lua OOP

For those Lua OOP aficionados, I recently released a new version of lua_objects which, among other things, now includes support for:
* class mixins
* multiple-inheritance

Check out lua_objects @ github for more info.

These changes were inspired from continuing work on my Lua port of the WAMP Protocol library for Corona SDK. That project contains a lot of Python code and it has reminded me how much I like the Python language. 🙂 Some particular things I re-discovered this time around were 1. method / class decorators and 2. multiple inheritance (including mixins).

That got me working on how to bring some of these aspects to Lua when doing OOP.

After the update, I was able to move the existing Event code (add/remove listener, etc) from the lua_objects core into its own module lua_events_mix.lua. Now that Event code can be easily re-used by other projects, either as a mixin or as a (monkey) patch.

Have fun !

References

Categories
Design Patterns Programming

Design Patterns – The Decorator

Tonight was the second night of my design patterns meeting I hold with some friends from work. Most of us already had the book Head First Design Patterns by O’Reilly Publishing, but never made the time to go through it. Jennifer was the one who came up with the idea to start having a meeting every two weeks and we all agreed that it was a good idea.

We’re currently working on our format, but so far have settled on one person being the ‘teacher’ for the evening. This person is responsible for presenting the highlights of the chapter to everyone even though we have all read it. After they are finished presenting, we then go around the table and share our ideas on how the pattern could be used at work or on personal projects.

Tonight we discussed the Decorator Pattern. Here were my solutions:

Recipe card

In this design, the recipe card is the base object and the decorators are ingredients. As per the design, ingredients would be stacked up to create the ingredient list. Here’s a rough cut at the class diagram.

decorator_class.png

click to view a larger image

Some of the interesting characteristics of recipes is that they often have to be scaled (eg, from 4 to 12 persons) or translated to another type of measurement (eg, metric/english). This functionality can easily be added down at the ingredient level. The different ingredients could also have superclasses based on their type – liquid, solid, etc. This makes it easy to separate scaling and measurement translation as necessary.

Invoice discounts

In this example the base object is an invoice and the decorators would be different types of discounts or rebates. This would allow a sales team to be creative with the types of discounts given to a particular client.

Discount objects could be applied to different products, plus you have them expire at certain times (free for 3 months), also be percentages or flat dollar amounts.

Next time I’m presenting the Command Pattern.