常用的四種設計模式

2021-10-22 17:16:10 字數 1692 閱讀 1082

乙個建構函式一生只能有乙個例項

function

person()

//核心**

let instance=

null

function()

const p1=

singleton()

const p2=

singleton

()

//改造單例模式

const

person

(function()

person.prototype.

sayhi

=function()

return

function

singleton

(...arg)

return instance }}

)()const p1=

newperson()

const p2=

newperson

()

class

observer)}

const bzr=

newobserver

('班主任',(

)=>

)const xz=

newobserver

('校長',(

)=>

)class

subject

setstate

(val))}

addobserver

(obs)

}

class

observer

}//向佇列裡新增訊息

on(type,fn)

this

.message[type]

.push

(fn)

}//刪除訊息佇列裡的訊息

off()if

(!this

.message[

]type]

)return

this

.message[type]

=this

.message[type]

.filter

(item=>item!==fn)

}//觸發訊息佇列

trigger()

)}}const person1=

newobserver

()

乙個問題匹配多種方案,隨時可以增加

利用閉包,使策略不會被銷毀

const calcprice=

(function()

,'200_25'

:function

(price)

,'80%'

:function

(price)

}function

calcprice

(price,type)

calcprice.

add=

function()

caleprice.

del=

function()

return calcprice})

()const res =

calcprice

(320

,'100_10'

)

設計模式筆記 四種模式 四種原則

a.簡單工廠 用乙個單獨的類來做這個建立例項的過程,這就是工廠 不符合開 封閉原剛,每次擴充套件都要更改工廠類 計算器 1.cbxtype.items.addrange new object b,策略模式 定義多種演算法,並分別封裝,之間可以互相替換,演算法的變化不影響到客戶 收銀 context ...

四種單例設計模式

public class singletonlazy 私有化該類的構造器 public static singletonlazy getinstance 如果沒有例項,就建立乙個 return instance 返回例項物件 public class singletonhungary 私有化該類的構...

資料庫 四種設計模式

設計模式 design pattern 是一套被反覆使用 多數人知曉的 經過分類的 設計經驗的總結。一般應用於提取不同型別的物件的共同特徵。比如學校當中,對於上課而言分為 老師和學生,但對於食堂大媽或者門衛大爺而言,就看你是不是校內人員。這是一種包含 關係。即校內人員包括 學生 老師 其他工作人員。...