何爲設計原則?
五大設計原則
從設計到模式
23中設計模式javascript
何爲設計?java
1) 小便是美
2) 讓每一個程序只作好好一件事
3) 快速創建原型
4) 捨棄高效率而取可移植性
5) 採用純文原本存儲數據
6) 充分利用軟件的槓桿效應(軟件複用)
7) 使用shell腳原本提升效應和可移植性
8) 避免強制性的用戶界面
9) 讓每一個程序都稱爲過濾器(shell的管道操做)
10) 容許用戶定製環境
11) 儘可能使用操做系統內核小而輕量化
12) 使用小寫字母並儘可能簡短
13) 沉默是金(在命令行的輸出有體現)
14) 各部分之和大於總體
15) 尋求90%的解決方案linux
S - 單一職責原則typescript
O - 開放封閉原則(important)shell
L - 李氏置換原則編程
I - 接口獨立原則設計模式
D - 依賴致使原則promise
在js中:dom
例如:在promise中運用了S O:測試
function loadImg(src) { var promise = new Promise(function (reslove, reject) { var img = document.creatElement('img') img.onload = function() { reslove(img) } img.src = src }) return promise } var src = '.....' var result = loadImg(src) result.then(function (img) { console.log('img.width',img.width) return img }).then(function (img) { console.log('img.height',img.height) }).catch(function (ex) { console.log(ex) })
從設計到模式
有繼承有引用
class Car { constructor(number, name) { this.number = number this.name = name } } class Kuaiche extends Car { constructor(number, name) { super(number, name) this.price = 1 } } class Zhuanche extends Car { constructor(number, name) { super(number, name) this.price = 2 } } class Trip { constructor(car) { this.car = car } start() { console.log(`行程開始,名稱: ${this.car.name}, 車牌號: ${this.car.price}`) } end() { console.log('行程結束,價格: ' + (this.car.price * 5)) } } let car = new Kuaiche(100, '桑塔納') let trip = new Trip(car) trip.start() trip.end()
都爲引用
// 車 class Car { constructor(num) { this.num = num } } // 入口攝像頭 class Camera { shot(car) { return { num: car.num, inTime: Date.now() } } } // 出口顯示器 class Screen { show(car, inTime) { console.log('車牌號', car.num) console.log('停車時間', Date.now() - inTime) } } // 停車場 class Park { constructor(floors) { this.floors = floors || [] this.camera = new Camera() this.screen = new Screen() this.carList = {} } in(car) { // 獲取攝像頭的信息:號碼 時間 const info = this.camera.shot(car) // 停到某個車位 const i = parseInt(Math.random() * 100 % 100) const place = this.floors[0].places[i] place.in() info.place = place // 記錄信息 this.carList[car.num] = info } out(car) { // 獲取信息 const info = this.carList[car.num] const place = info.place place.out() // 顯示時間 this.screen.show(car, info.inTime) // 刪除信息存儲 delete this.carList[car.num] } emptyNum() { return this.floors.map(floor => { return `${floor.index} 層還有 ${floor.emptyPlaceNum()} 個車位` }).join('\n') } } // 層 class Floor { constructor(index, places) { this.index = index this.places = places || [] } emptyPlaceNum() { let num = 0 this.places.forEach(p => { if (p.empty) { num = num + 1 } }) return num } } // 車位 class Place { constructor() { this.empty = true } in() { this.empty = false } out() { this.empty = true } } // 測試代碼------------------------------ // 初始化停車場 const floors = [] for (let i = 0; i < 3; i++) { const places = [] for (let j = 0; j < 100; j++) { places[j] = new Place() } floors[i] = new Floor(i + 1, places) } const park = new Park(floors) // 初始化車輛 const car1 = new Car('A1') const car2 = new Car('A2') const car3 = new Car('A3') console.log('第一輛車進入') console.log(park.emptyNum()) park.in(car1) console.log('第二輛車進入') console.log(park.emptyNum()) park.in(car2) console.log('第一輛車離開') park.out(car1) console.log('第二輛車離開') park.out(car2) console.log('第三輛車進入') console.log(park.emptyNum()) park.in(car3) console.log('第三輛車離開') park.out(car3)