Pokazywanie postów oznaczonych etykietą Design patterns. Pokaż wszystkie posty
Pokazywanie postów oznaczonych etykietą Design patterns. Pokaż wszystkie posty

piątek, 23 grudnia 2011

Design patterns - Command

Second post about Design Patterns going to be devoted to Command pattern. It's one of these patterns that can be characterized with one sentence: "Move chunk of code to it's object and execute it as needed".
It might be not very informative, but that's all that is to say about Command pattern, really:)
I always considered examples to be more instructive then theoretical description, so lets consider the following example: "Your task is to design and implement a controller for autonomous race car, controller will be using preset knowledge base for different driving skills: for driving on snow and on road". Not getting into details controller might look like that:
class Driving
  def initializing(skill)
    @skill = skill
  end

  # methods implementing action based on returned value from function drive

  def drive
   @skill.use
  end

end
Now that every time we want our controller to use other knowledge base (other driving skills) all we have to do is to initialize class Driving with appropriate skill class, one might look like that:
class OnSnowSkill
  def use
    "using on snow skills"
  end
end

class OnRoadSkill
  def use
    "using on road skills"
  end
end
Now we can use our driving skills like:
Driving.new(OnSnowSkill.new).drive
or when we are driving on solid road:
Driving.new(OnRoadSkill.new).drive
That's the core of command design pattern, separate these that stay constant form these that change. Chunk of code that change should have it own class that encapsulate appropriate actions.

On github You can find full implementation as well as implementation of command design pattern using blocks (btw, migration in rails use command pattern and implement it thru block) and some other patterns too.

wtorek, 20 grudnia 2011

Design patterns - Template Method

With these post i would like to start new series on my blog, series devoted to design patterns and their implementation in Ruby.

So, for start, let's look wikipedia to find out whats design patterns really are:
"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 code. It is a description or template for how to solve a problem that can be used in many different situations..." from wikipedia
One of the most basic and commonly used design patterns (used even, implicitly, in other design patterns) is design called "Template method". Basically speaking "Template method" pattern consist of abstract class witch provide template methods and some algorithms that work on these methods, their implementation is provided by subclass.

Consider example:
Your task is to implement two classes, one for simulating driving a car and one simulating driving a motorcycle. One might notice that at some point of abstraction driving these vehicles is similar, you have to start engine, increase speed, stop engine, etc. The differences are how you are doing it. Take a look at the following code:
class DriveVehicle
 def start_engine
  raise "Trying to use abstract method"
 end

 def stop_engine
  raise "Trying to use abstract method"
 end

 def increase_velocity
  raise "Trying to use abstract method"
 end

 def decrease_velocity
  raise "Trying to use abstract method"
 end

 def drive
  [start_engine, increase_velocity, decrease_velocity, stop_engine].join ' and '
 end
end
Class DriveVehicle implements these abstract tasks (template methods) and provide method "drive" that work on them. Implementation of these method is going to be provided by subclass of DriveVehicle, so result of the "drive" method will solely depend on provided implementation.
For driving a car, class implementing it behavior might looks like that:
class DriveCar < DriveVehicle
 def start_engine
  "starting car engine"
 end

 def stop_engine
  "stoping car engine"
 end

 def increase_velocity
  "increasing car velocity"
 end

 def decrease_velocity
  "decreasing car velocity"
 end
end
and for driving a motorcycle like that:
class DriveMotorcycle < DriveVehicle
 def start_engine
  "starting motorcycle engine"
 end

 def stop_engine
  "stoping motorcycle engine"
 end

 def increase_velocity
  "increasing motorcycle velocity"
 end

 def decrease_velocity
  "decreasing motorcycle velocity"
 end
end
using of these classes is as simple as:
DriveCar.new.drive
DriveMotorcycle.new.drive
That's all, that "Template methods" design pattern! :)
Please checkout my github for source code and some tests for these classes.

PS:
I have said that these are abstract methods/classes but thats not true, ruby doesn't provide mechanism for creating abstract class, raising error is as close as it can get;P