20. File System in Piconion
var FileState=function(){
this.id; this.name;
this.current; this.modified;
this.set=function(){};
this.get=function(){};
};
var FileUI=function(){
this.elem;
this.create=function(){}
this.modify=function(){}
this.remove=function(){}
};
var FileData=function(){
this.img;
this.load=function(){}
this.modify=function(){}
this.dispose=function(){}
}
Glue Code
21. Modules in File System
FileState
FileUI FileData
Blue rects are modules, should meet requirements listed below:
● Clear purpose: 清楚的權責。
● Independant: 獨立運作,彼此不互相影響。
● Reusable: 可重複利用。
● Unit Testable: 可做單元測試。
22. Glue Code in File System
Glue
Green triangle is glue code, do any other dirty works:
● Set up & Initialize. 完成初期設定。
● Control application flow. 控制應用流程。
● Pass data between modules. 傳遞模組間的資料。
● Do unit test. 加入單元測試程式。
27. Why Pure Function?
● Easy to do unit test. 容易做單元測試
● Flexible and Reusable. 彈性且可重複使用
● Simple. 減少影響整體系統的函式,能有效降低系統複雜度
● Should be the interface of modules.
是建立模組介面的優良選擇。
28. Where Pure Functions Work Best?
var FileState=function(){
this.current;
this.modified;
this.set=function(){};
this.get=function(){};
};
var FileUI=function(){
this.elem;
this.create=function(){};
this.modify=function(){};
this.remove=function(){};
};
var FileData=function(){
this.id; this.name; this.img;
this.load=function(){};
this.modify=function(){};
this.dispose=function(){};
}
Glue Code
(File Object)
29. File Object
(Glue Code)
var File=function(){
// Ignore FileState, FileData, and FileUI Details
var fileState=new FileState();
var fileData=new FileData();
var fileUI=new FileUI();
this.init=function(){
fileData.load(function(){
fileState.set("current", true);
fileUI.create({
"current":fileState.get("current"),
"name":fileState.get("name"),
"img":fileState.get("img")
});
});
};
};
(new File()).init();
30. What we write
var FileUI=function(){
this.elem;
this.create=function(inputs){
this.elem=document.createElement("div");
this.elem.className=
inputs.current?"current-file":"file";
this.elem.title=inputs.name;
};
this.modify=function(){};
this.remove=function(){};
};
31. What we DON’T write
var FileUI=function(){
this.elem;
this.create=function(){
this.elem=document.createElement("div");
this.elem.className=
fileState.current?"current-file":"file";
this.elem.title=fileData.name;
};
this.modify=function(){};
this.remove=function(){};
};
33. File Management System in Piconion
Current File
(State)
File Bar
(UI)
Array of File Object
(Data)
File
Manager
34. File Management System in Piconion
var FileManager=function(){
var files=[]; // Array of File Object / Data
var fileCurrent=null; // State
var fileBar=new FileBar(); // UI
this.loadFile=function(){};
this.changeCurrentFile=function(){};
this.disposeFile=function(){};
};
FileState
FileUI FileData
Encapsulated
File Structure