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).driveor when we are driving on solid road:
Driving.new(OnRoadSkill.new).driveThat'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.