Tuesday, 19 August 2014

Design Patterns in Ruby.

The life of a software developer involves dealing with complex problems during their operations. The first time a developer confronts a problem he/she improvises a solution but when the same problem occurs often then there is need to come up with a stardard operating procedure to cover it. Thats what design patterns are for.

From wikipedia's definition ;

In software engineering, a design pattern is a general reusable solution to a commonly occurring problem within a given context in software design. A design pattern is not a finished design that can be transformed directly into source or machine code.

In this post I will give a brief background on a pattern and show its implementation in ruby.I hope you will be able to judge when to use the patterns during your operations.

I will keep updating this post each day with a new pattern(GoF patterns) because posting all of them at once needs alot of time so I will do it in bits.

Singleton Pattern

This pattern is useful when you want to make sure that a class has only one instance and you need to have a global access to it.The singleton module in ruby library can be used to implement this pattern. require 'singleton'

class SingletonClass include Singleton

def initialize @file = File.open("log.txt", "a") end

def show(content) @file.puts(content) endend

Template Method Pattern

The template pattern is useful when you have a method whose algorithm varies as you will see in the example.The idea behind a template pattern is that you have a base class which contains abstract methods(also called template methods) that the concrete subclasses will implement.You can then pick the variation you want by selecting one of the concrete subclasses.class Good

You can then pick a variation by picking one of the concrete subclasses like this

tax = TaxedGood.newtax.basic_tax(400)

ortax = ExemptedGood.newtax.basic_tax(400)Strategy Pattern

The Strategy pattern is similar to the template pattern only that it works on the principle of delegation and provides for run time flexibility since we make our decision by selecting a strategy at runtime. The idea behind the strategy pattern is to define objects (also refered to as strategies by the GoF) with similar methods in our example basic_tax; and have them support exactly the same interface(called a strategy by the GoF).Then we define a class(refered to as the context by the GoF) that uses the strategy to treat the strategies like interchangeable parts(since from the outside they look the same as they support the same interface).we make our decision by selecting one of the objects(strategies) at runtime.class Good

Interator Pattern
This is a technique that allows an aggregate object to provide the outside world with a way to access its
collection of sub-objects sequentially without exposing its underlying represenation.

In ruby the iterator pattern is implemented in two flavours;
- External iterators.
- Internal iterators.