一句話總結設計模式 《大化設計模式》讀書筆記

2021-09-17 23:17:32 字數 4243 閱讀 8415

設計模式並不是一種演算法,而是一種思想,一種軟體開發思想;這種思想便於開發的維護、擴充套件。

基於結構型

基於行為:

/**

* @description: 1.1多執行緒單列-懶漢式-用時再建立

* @author: zoutai

* @create: 2019/4/11

**/public

class

singletoncon

// 例項化函式類函式

public

static singletoncon getinstance()

}}return instance;}}

/** *

* 補充說明volatile禁止重排序:對於構造語句 instance = new singletoncon();

* 在構造時,cpu會對進行語句優化,進行重排序,先對instance賦值,然後才例項化物件;這樣就instance不為null,但是卻沒有例項化物件。

內部重排序指令如下:

inst = allocat(); // 分配記憶體

ssingleton = inst; // 賦值

constructor(inst); // 真正執行建構函式

* **/

/** * @description: 1.2 單例模式-餓漢式

* @author: zoutai

* @create: 2019/4/11

**/public

class

singletonstatic

public

static singletonstatic getinstance()

}// 2 工廠模式:

// 執行緒池介面

public

inte***ce

mythreadpool

// 第一種執行緒池

public

class

singlethreadpool

implements

mythreadpool

}// 第二種執行緒池

public

class

fixedthreadpool

implements

mythreadpool

}// 工廠建立

public

class

threadfactory

else}}

public

class

testfactory

}// 3 抽象工廠模式

// 建立使用者工廠

public

inte***ce

user

public

class

studentuser

implements

user

}public

class

teacheruser

implements

user

}// 建立執行緒池工廠同上(省略)

// 建立抽象工廠,集合所有的工廠

public

inte***ce

abstractfactory

public

class

singlefactory

implements

abstractfactory

@override

public user newuser()

}public

class

fixedfactory

implements

abstractfactory

@override

public user newuser()

}public

class

testabstractfactory

}// 4.裝飾器

// 定義輸入流介面

public

inte***ce

inputstream

// 檔案型別輸入流

public

class

fileinputstream

implements

inputstream

}// 裝飾器:為所有的輸入流 增添新的功能

public abstract class

filterinputstream

implements

inputstream

@override

public

void

read()

}// 緩衝裝飾器:新增具有緩衝功能的裝飾器

public

class

bufferedinputstream

extends

filterinputstream

@override

public

void

read()

private

void

readbuffered()

}public

class

testdecorator

}// 5 介面卡

// 適配者

public

inte***ce

afteradvice

// 介面after的***

public

class

methodafteradvice

implements

afteradvice

}// 主介面

public

inte***ce

beforeadvice

// 具體的***

public

class

mathodbeforeadvice

implements

beforeadvice

}// 介面卡:適配介面通過例項物件呼叫

public

class

methodbeforeadviceadapter

implements

beforeadvice

} @override

public

void

before()

else}}

// 6 **模式

public

inte***ce

task

public

class

posttask

implements

task

}// **類,實現被**者的內部邏輯

public

class

proxytask

implements

task

system.out.

println

("**間接呼叫");

task.

dealtask

(taskname);}

}public

class

testproxy

}// 7 模板類

public abstract class

jdbctemplate

}public

class

myjdbctemplate

extends

jdbctemplate

@override

void

dotask()

@override

void

releaseconnection()

}// 8 策略模式

public

inte***ce

strategy

// 各種策略實現

public

class

addstrategy

implements

strategy

}public

class

substrategy

implements

strategy

}// 策略類

public

class

context

public int execute

(int num1, int num2)

}public

class

teststrategy

}

一句話設計模式

提供乙個建立一系列或相關依賴物件的介面,而無需指定它們具體的類 將乙個複雜物件的構建與它的表示分離,使得同樣的構建過程可以建立不同的表示 3.工廠方法模式 factorymethod 定義乙個用於建立物件的介面,讓子類決定例項化哪乙個類,使得乙個類的例項化延遲到其子類 4.原型模式 prototyp...

一句話 道出設計模式

近段時間漸覺自己愚弱,感覺各方面都有些透支。索性去腦補一下設計模式。講真,之前對此也是一知半解,未曾有太多研究,此番也是做簡單了解,參考園裡大神做些紀要。於是乎,我又一次發覺其實在日常擼碼中已經隱隱約約用到了各種設計模式 你覺得夢想很遠,其實你已經在路上了 比如,資料庫連線池用到了單例模式 比如,流...

一句話歸納設計模式

通過 23種設計模式全面解析 教程,我們已經學習完了經典的 23 種設計模式。下面總結一下這 23 種設計模式,以方便小夥伴們日後複習和查閱。分類設計模式 簡述一句話歸納 目的生活案例 建立型設計模式 簡單來說就是用來建立物件的 工廠模式 factory pattern 不同條件下建立不同例項 產品...