05觀察,命令

2022-03-11 00:38:24 字數 2640 閱讀 9923

作用

通用實現

var event = ,

trigger: function() ,

remove: function(key, fn) else

} }};

//動態安裝發布-訂閱功能;

var installevent = function(obj)

};//執行

var publisher = {};

installevent(publisher);

publisher.listen('click', function(data) );

publisher.trigger('click', 'new data');

publisher.remove('click');

實際開發中的例子
login.succ(function(data) );
login.succ(function(data) );

var a = (function() );

return

}})

全域性的發布-訂閱物件
var event = (function() ,

listen = function(){},

triggee = function(){},

remove = function(){};

return

})

應用場景
/**

* 按鈕點選

*/var btn = document.getelementbyid('btn');

var setcommand = function (btn, command)

};//實際設定的物件接受物件和請求操作

var menu =

};var menucommand = function(receive) ;

menucommand.prototype.execute = function() ;

//使用

var addmenu = new menucommand(menu);

setcommand(btn, addmenu)

js中的命令模式
//上面例子的簡化

var menucommand = function(receive)

}};

撤銷和重做
var btn = document.getelementbyid('btn');

var btn2 = document.getelementbyid('btn2');

var btn3 = document.getelementbyid('btn3');

var setcommand = function (btn, command, fn)

}; var menu = ,

back: function() ,

restart: function()

}; var menucommand = function(receive) ,

undo: function() ,

restart: function()

}};

//使用

var refreshmenu = menucommand(menu);

setcommand(btn, refreshmenu, 'execute');

setcommand(btn2, refreshmenu, 'undo');

setcommand(btn3, refreshmenu, 'restart');

巨集命令
var macrocommand = function () ,

execute: function()

} };

乙個標題檢視的建立
var viewcommand = (function () "/>

', '

', '

' ].join(''),

title: [

'','',

'','

', '

' ].join('')

};var html = '';

function formatestring (str, obj) /g, function (match, key) )

} var action =

} else

},display: function (container, data, view)

document.getelementbyid(container).innerhtml = html;

html = '';}};

return function excute(msg)

})();

var titledata = ;

var productdata = [

,];//建立標題模組

viewcommand();

//建立乙個

viewcommand(, 'product']

})//建立多個

viewcommand();

05 觀察者模式(Observer)

定義物件間的一種一對多 變化 的依賴關係,以便當乙個物件 subject 的狀態發生改變時,所有依賴於它的物件都得到通知並自動更新。檔案切割,進度條 class filesplitter void split 主視窗顯示進度條 class mainform public form 檔案切割,進度介面...

05 命令模式

05 命令模式 define crt secure no warnings include include includeusing namespace std 協議處理類 class handleclientprotocol 處理增加鑽石 void adddiamond 處理玩家裝備 void a...

Linux命令學習 05

指在linux終端 命令列 中輸入的內容就稱之為命令 root tedu mkdir a test 在a目錄下建立test目錄 root tedu ls a 檢視a目錄下的內容 a.txt test root tedu mkdir p a b c d 在a目錄下建立b目錄,b目錄下再建立c目錄,c目...