In-depth analysis of the use of the command mode in Ruby Design Mode Programming, ruby Design Mode

Source: Internet
Author: User

In-depth analysis of the use of the command mode in Ruby Design Mode Programming, ruby Design Mode

The command mode is the design mode with a high usage of the object behavior type, alias: Action, Transaction)

Intent: encapsulate a request as an object so that you can parameterize different requests, queue requests or record request logs, and support cancelable operations
The so-called "different requests" also means that the request may change and is a feature that may be extended.

Motivation: convenient scalability

Structure:


Collaboration description:
Participating roles:
Command declares an interface to implement an operation.
ConcreteCommand binds the action to the Reciver and calls the corresponding method of the Reciver object to implement the Command method.
The Client creates a ConcreteCommand object and sets its Reciver object.
Invoker requires the Command to implement the request.
The Reciver knows how to implement the specific request class.
The client creates a specific Command object and specifies its receiver.
The caller object stores this specific Command object.
The caller object implements the current request by executing the Execute method of the Command object.
If the command can be undone, the specific object will store the corresponding status before calling the execution method to command this request.
The specific Command object calls the method of its receiver to implement the corresponding request.


Applicability:
Similar to MenuItem, abstract the action to be executed to parameterize an object
Specify, sort, and execute requests at different times
Undo
Logs can be modified.
High-level operations built on primitive operations construct a system (actually a transaction)


Dynamic: block is the command mode in ruby.

Effect:
The command mode decouples the caller object from the receiver object (call and implementation are decoupled ). When implementing the function, the caller only needs to call the Execute method of the Command interface.
The specific Commands objects are the first-level objects that can be extended or operated like other objects.
You can aggregate multiple Commands objects into a combined command. Combined commands are also an instance of the combined object mode. queuing commands is also a special case.
You can easily add new commands because you do not need to modify the existing code. This is also in line with the open and closed principle, and is open to modifications and extensions.


When implementing the command, consider the intelligent program to which the command object should be implemented and the two questions that support undo and redo.


Misuse:
Don't be fascinated by what is simple?
The command mode does not mean "do this", "remember how to do this", and later "follow the methods I just want you to remember to do this"
Careful undo. Many operations are destructive, such as deleting files.


Class diagram:



class Button
 
 attr_accessor :name, :command
 
 def initialize name, command
  @name = name
  @command = command
 end
 
 def do_something
  @command.execute
 end
 
end

class Command
 
 def execute
  "root execute"
 end
 
end

class PaintCommand < Command
 
 def execute
  "draw something"
 end
 
end

class VocalCommand < Command
 
 def execute
  "talk something"
 end
 
end

paintCommand = PaintCommand.new
vocalCommand = VocalCommand.new
button = Button.new("button", paintCommand)
p button.do_something
button.command = vocalCommand
p button.do_something

The main class Button is defined. The Button aggregates a Command object Command, declares Command, PaintCommand, and VocalCommand. There may be multiple buttons in the system, each Button has different things to do, that is, this part is changed, that is, the code in the do_something method is also uncertain, this part of the code is separated into a separate object for management, and this object is called a command object. The command object is only responsible for the tasks or commands that need to be completed, the subject object can call the required commands at any time according to its own needs. In the code of the call, it is also clear that it is very convenient and flexible to switch the current Button command. You only need to simply call the set method. If the relationship inherited by the Button is used, the first subject object will cause a class explosion, and the second method will be more difficult to compare when switching the command implementation.
 
Use ruby proc to complete the command mode:
 

class Button
 
 attr_accessor :name
 
 def initialize name, &command
  @name = name
 end
 
 def do_something &command
  command.call
 end
 
end

paint_command = lambda do
 p "paint something"
end

vocal_command = lambda do
  p "talk something"
end

button = Button.new ("name")
button.do_something &vocal_command
button.do_something &paint_command

It can be seen that using block to replace the command class is simpler and easier to understand. You can choose to use proc and command in the actual project environment. If the command object is very complex, if you need to have your own status and method, you can use the command class to complete the process. If you are simply dealing with some small things, you can use proc
 
If you need to execute too many commands, you can define a command queue, that is, to manage multiple commands in a command. When you call it, you can call each command one by one to execute it, from this point, it is very similar to the combination mode.
 
In a certain sense, the observer mode and the command mode are somewhat similar. They aggregate some objects with common features into their own classes and then call them as needed. However, there is an obvious difference between the two modes, that is, the purpose. The observer mode is used by the observer to notify different observers of changes. The command mode does not care whether it notifies other commands. The command object is only responsible for executing its own tasks or commands, in addition, the command mode can remember the previous operation, so many text editors usually use the command mode for Undo/Redo operations.


Articles you may be interested in:
  • Example Analysis of Observer Pattern in Ruby programming in Design Pattern
  • Instance parsing Implementation of observer mode in Ruby design mode development
  • Application instance analysis of appearance mode in Ruby Design Mode Programming
  • Detailed description of the structure of the Combination Mode and Its Application in Ruby Design Mode Programming
  • The template method in the design mode applies two examples in Ruby.
  • Example parsing: Use of Strategy Mode in Ruby Design Mode Programming
  • The example explains how Ruby uses the decorator mode in the design mode.
  • Example of using Builder mode in Ruby Design Mode Programming
  • Detailed description of the Application of Singleton mode in Ruby Design Mode Programming
  • Programming in Ruby design mode-Introduction to adapter Mode
  • Ruby uses code instances in the proxy mode and decoration mode in the Design Mode
  • Ruby uses the simple factory mode and factory method mode in the Design Mode
  • Application of parsing proxy mode in Ruby design mode development


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.