JS Command mode example of the menu program _javascript skills

Source: Internet
Author: User

Application Scenarios for Command mode:

Sometimes you need to send a request to some object, but do not know who the recipient of the request is, or what the requested operation is, and you want to design the software in a loosely coupled way so that the sender of the request and the requesting recipient can eliminate the coupling between them.

HTML code:

<! DOCTYPE html>
 
 

JS Code:

Using the example in the page: SetCommand (button1, Refreshmenubarcommand); To send a command//SetCommand function to install the command on the button, reserve the interface for the installation command var SetCommand = f
 Unction (button, command) {Button.onclick = function () {Command.Execute ();
 ///write Click button after the specific behavior: Refresh menu interface, Add submenu and delete submenu var MenuBar = {refresh:function () {var cur_date = new Date ();
 document.getElementById ("textarea"). Value+=cur_date.tolocalestring () + "Refresh Menu directory \ r";
 } var submenu = {Add:function () {var cur_date = new Date ();
 document.getElementById ("textarea"). Value+=cur_date.tolocalestring () + "Add menu directory \ r";
 }, Del:function () {var cur_date = new Date ();
 document.getElementById ("textarea"). Value+=cur_date.tolocalestring () + "Delete submenu \ r";

}//encapsulation behavior in the command class var refreshmenubarcommand = function (receiver) {this.receiver = receiver; } RefreshMenuBarCommand.prototype.execute = function () {This.receiver.refresh ();} var addsubmenucommand = function (rec
Eiver) {this.receiver = receiver;} AddSubMenuCommand.prototype.execute = function () {this.receiver.add ();
var delsubmenucommand = function (receiver) {this.receiver =receiver} DelSubMenuCommand.prototype.execute = function

() {This.receiver.del ();}
The command recipient is passed into the command object var refreshmenubarcommand = new Refreshmenubarcommand (MenuBar);
var addsubmenucommand = new Addsubmenucommand (submenu);

var delsubmenucommand = new Delsubmenucommand (submenu);
 Window.onload = function () {//Install Command object to button above var button1 = document.getElementById ("button1");
 var button2 = document.getElementById ("Button2");
 var button3 = document.getElementById ("Button3");
 SetCommand (button1, Refreshmenubarcommand);
 SetCommand (Button2, Addsubmenucommand);
SetCommand (Button3, Delsubmenucommand);
 }

Summarize:

Copy code from the book to practice the process of a lot of mistakes, the most serious than "receiver" this word is wrong to write a lot of days have not seen this exercise, the process of error so that I can re-examine the content of the code, line to understand and debug. Although still not very understanding command mode, but through this part of the content and the study of MySQL, the heart of the faint left about the shadow of the command mode.

Reference:

"JavaScript design pattern and Development Practice" 9th Chapter 9.2

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.