Wednesday, May 21, 2014

Session 6 (Technically 8) - Design Patterns: Structural Patterns

<Meh>

Two posts in one evening... spoiling my 4 or 5 readers (^_^)

Anyways, in this session, we continue with design patterns, namely Structural Design Patterns

Firstly, there are a lot and funny enough, if you didn't recognize any of the creational patterns, you'll definitely recognize a few here. Patterns like the Proxy Pattern (I call it the Wrapper Pattern).. the Facade should be recognizable as well (you'll notice it focusses a lot on the SRP).

In this session, we take a look at the Adapter, Composite and Decorator patterns.

Adapter: getting two classes that don't share the same interface to talk to each other
Composite: If you don't know how to build a tree hierarchy... this is how you should build it
Decorator: Wrap separate pieces of functionality into separate classes so that you can add and remove them with ease... decorating the default or original object.

The decorator for me was very exciting. I had never seen this pattern before but had taken a stab at implementing something similar a few times back in the day. It reminded me a lot of the abstract method pattern, although both serve different purposes, they're both extremely easy to use and they give your application a level of flexibility one can only dream of.

The the code sample, you'll see me build a travel package with various options. When I was coding this, I the following in mind:

You work at a travel agency or even a ticketing company and you need to build a system that will compile a product based on user input selections. In my example, I demo how you can build full package or a partial one. Ok, I do hardcode the selections (by specifying the classes myself) but you can obviously make this dynamic as well with reflection and what not.

That being said, that's just one of the many places where this pattern applies.

Enough from me, time to look at the code: It can, as always, be found here:
Presentation: http://www.slideshare.net/DeonMeyer/code-like-a-ninja-session-8-structural-design-patterns
Code: https://github.com/SheepWorx/Training/tree/master/StructuralPatterns

</Meh>

No comments:

Post a Comment