Appearance
组合模式
树形结构常用。会为多个对象创建统一的访问。
javascript
const Folder = function(folder){
this.folder = folder
this.list = [] // 保存 子文件夹 或者文件
}
Folder.prototype.add = function(res){
this.list.push(res)
}
Folder.prototype.scan = function(){
console.log("扫描文件夹", this.folder)
for(let i = 0; i< this.list.length;i++){
this.list[i].scan()
}
}
const File = function(file){
this.file = file
}
File.prototype.scan = function(){
console.log("开始扫描文件", this.file)
}
// 根
let rootFolder = new Folder("root")
// 子文件夹
let htmlFolder = new Folder("html")
let cssFolder = new Folder("css")
let jsFolder = new Folder("js")
rootFolder.add(htmlFolder)
rootFolder.add(cssFolder)
rootFolder.add(jsFolder)
// 文件
let html1 = new File("html1")
let html2 = new File("html2")
let html3 = new File("html3")
let css = new File("css")
let js1 = new File("js1")
let js2 = new File("js2")
htmlFolder.add(html1)
htmlFolder.add(html2)
htmlFolder.add(html3)
cssFolder.add(css)
jsFolder.add(js1)
jsFolder.add(js2)
rootFolder.scan()