我幾乎不需要討論為什么重用代碼是有利的。代碼重用通常使得程序開發更加快速,并使得 bug 減少。一旦一段代碼被封裝和重用,那么只需要檢查很少的一段代碼即可確保程序的正確性。如果在整個應用程序中只需要在一個地方打開和關閉數據庫連接,那么確保連接是否正常則容易的多。但我確信這些你已經都知道了。
有兩種類型的重用代碼,我稱它們為重用類型:
- 功能重用(action reuse)
- 上下文重用(context reuse)
第一種類型是功能重用,這是最常見的一種重用類型。這也是大多數開發人員掌握的一種。即重用一組后續指令來執行某種操作。
第二種類型是上下文重用,即不同功能或操作代碼在相同上下文之間,將相同上下文封裝為重用代碼(這里的上下文指的是一系列相同的操作指令)。雖然它在控制反轉中越來越受歡迎但它并不常見。而且,上下文重用并沒有被明確的描述,因此它并沒有像功能重用一樣被系統的使用。我希望你看完這篇文章之后會有所改變。
功能重用
功能重用是最常見的重用類型。它是一組執行某種操作指令的重用。下面兩個方法都是從數據庫中讀取數據:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
public list readallusers(){ connection connection = null ; string sql = "select * from users" ; list users = new arraylist(); try { connection = openconnection(); preparedstatement statement = connection.preparestatement(sql); resultset result = statement.executequery(); while (result.next()){ // 重用代碼 user user = new user(); user.setname (result.getstring( "name" )); user.setemail(result.getstring( "email" )); users.add(user); // end 重用代碼 } result.close(); statement.close(); return users; } catch (sqlexception e){ //ignore for now } finally { //ignore for now } } public list readusersofstatus(string status){ connection connection = null ; string sql = "select * from users where status = ?" ; list users = new arraylist(); try { connection = openconnection(); preparedstatement statement = connection.preparestatement(sql); statement.setstring( 1 , status); resultset result = statement.executequery(); while (result.next()){ // 重用代碼 user user = new user(); user.setname (result.getstring( "name" )); user.setemail(result.getstring( "email" )); users.add(user); // end 重用代碼 } result.close(); statement.close(); return users; } catch (sqlexception e){ //ignore for now } finally { //ignore for now } } |
對于有經驗的開發人員來說,可能很快就能發現可以重用的代碼。上面代碼中注釋“重用代碼”的地方是相同的,因此可以封裝重用。這些是將用戶記錄讀入用戶實例的操作??梢詫⑦@些行代碼封裝到他們自己的方法中,例如:
1
2
3
4
5
6
7
8
|
// 將相同操作封裝到 readuser 方法中 private user readuser(resultset result) throws sqlexception { user user = new user(); user.setname (result.getstring( "name" )); user.setemail(result.getstring( "email" )); users.add(user); return user; } |
現在,在上述兩種方法中調用readuser()方法(下面示例只顯示第一個方法):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
public list readallusers(){ connection connection = null ; string sql = "select * from users" ; list users = new arraylist(); try { connection = openconnection(); preparedstatement statement = connection.preparestatement(sql); resultset result = statement.executequery(); while (result.next()){ users.add(readuser(result)) } result.close(); statement.close(); return users; } catch (sqlexception e){ //ignore for now } finally { //ignore for now } } |
readuser()方法也可以在它自己的類中使用修飾符private隱藏。
以上就是關于功能重用的內容。功能重用是將一組執行特定操作的指令通過方法或類封裝它們來達到重用的目的。
參數化操作
有時,你希望重用一組操作,但是這些操作在使用的任何地方都不完全相同。例如readallusers()和readusersofstatus()方法都是打開一個連接,準備一條語句,執行它,并循環訪問結果集。唯一的區別是readusersofstatus()需要在preparedstatement上設置一個參數。我們可以將所有操作封裝到一個readuserlist()方法。如下所示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
private list readuserlist(string sql, string[] parameters){ connection connection = null ; list users = new arraylist(); try { connection = openconnection(); preparedstatement statement = connection.preparestatement(sql); for ( int i= 0 ; i < parameters.length; i++){ statement.setstring(i, parameters[i]); } resultset result = statement.executequery(); while (result.next()){ users.add(readuser(result)) } result.close(); statement.close(); return users; } catch (sqlexception e){ //ignore for now } finally { //ignore for now } } |
現在我們從readallusers()
和readusersofstatus()
調用readuserlist(...)
方法,并給定不同的操作參數:
1
2
3
4
5
6
|
public list readallusers(){ return readuserlist( "select * from users" , new string[]{}); } public list readuserswithstatus(string status){ return readuserlist( "select * from users" , new string[]{status}); } |
我相信你可以找出其他更好的辦法來實現重用功能,并將他們參數化使得更加好用。
上下文重用
上下文重用與功能重用略有不同。上下文重用是一系列指令的重用,各種不同的操作總是在這些指令之間進行。換句話說,重復使用各種不同行為之前和之后的語句。因此上下文重用通常會導致控制風格類的反轉。上下文重用是重用異常處理,連接和事務生命周期管理,流迭代和關閉以及許多其他常見操作上下文的非常有效的方法。
這里有兩個方法都是用 inputstream 做的:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
public void printstream(inputstream inputstream) throws ioexception { if (inputstream == null ) return ; ioexception exception = null ; try { int character = inputstream.read(); while (character != - 1 ){ system.out.print(( char ) character); // 不同 character = inputstream.read(); } } finally { try { inputstream.close(); } catch (ioexception e){ if (exception == null ) throw e; } } } public string readstream(inputstream inputstream) throws ioexception { stringbuffer buffer = new stringbuffer(); // 不同 if (inputstream == null ) return ; ioexception exception = null ; try { int character = inputstream.read(); while (character != - 1 ){ buffer.append(( char ) character); // 不同 character = inputstream.read(); } return buffer.tostring(); // 不同 } finally { try { inputstream.close(); } catch (ioexception e){ if (exception == null ) throw e; } } } |
兩種方法與流的操作是不同的。但圍繞這些操作的上下文是相同的。上下文代碼迭代并關閉 inputstream。上述代碼中除了使用注釋標記的不同之處外都是其上下文代碼。
如上所示,上下文涉及到異常處理,并保證在迭代后正確關閉流。一次又一次的編寫這樣的錯誤處理和資源釋放代碼是很繁瑣且容易出錯的。錯誤處理和正確的連接處理在 jdbc 事務中更加復雜。編寫一次代碼并在任何地方重復使用顯然會比較容易。
幸運的是,封裝上下文的方法很簡單。 創建一個上下文類,并將公共上下文放入其中。 在上下文的使用中,將不同的操作指令抽象到操作接口之中,然后將每個操作封裝在實現該操作接口的類中(這里稱之為操作類),只需要將該操作類的實例插入到上下文中即可??梢酝ㄟ^將操作類的實例作為參數傳遞給上下文對象的構造函數,或者通過將操作類的實例作為參數傳遞給上下文的具體執行方法來完成。
下面展示了如何將上述示例分隔為上下文和操作接口。streamprocessor(操作接口)作為參數傳遞給streamprocessorcontext的processstream()方法。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
// 流處理插件接口 public interface streamprocessor { public void process( int input); } // 流處理上下文類 public class streamprocessorcontext{ // 將 streamprocessor 操作接口實例化并作為參數 public void processstream(inputstream inputstream, streamprocessor processor) throws ioexception { if (inputstream == null ) return ; ioexception exception = null ; try { int character = inputstream.read(); while (character != - 1 ){ processor.process(character); character = inputstream.read(); } } finally { try { inputstream.close(); } catch (ioexception e){ if (exception == null ) throw e; throw exception; } } } } |
現在可以像下面示例一樣使用streamprocessorcontext類打印出流內容:
1
2
3
4
5
6
7
8
|
fileinputstream inputstream = new fileinputstream( "myfile" ); // 通過實現 streamprocessor 接口的匿名子類傳遞操作實例 new streamprocessorcontext() .processstream(inputstream, new streamprocessor(){ public void process( int input){ system.out.print(( char ) input); } }); |
或者像下面這樣讀取輸入流內容并添加到一個字符序列中:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
public class streamtostringreader implements streamprocessor{ private stringbuffer buffer = new stringbuffer(); public stringbuffer getbuffer(){ return this .buffer; } public void process( int input){ this .buffer.append(( char ) input); } } fileinputstream inputstream = new fileinputstream( "myfile" ); streamtostringreader reader = new streamtostringreader(); new streamprocessorcontext().processstream(inputstream, reader); // do something with input from stream. reader.getbuffer(); |
正如你所看到的,通過插入不同的streamprocessor接口實現來對流做任何操作。一旦streamprocessorcontext被完全實現,你將永遠不會有關于未關閉流的困擾。
上下文重用非常強大,可以在流處理之外的許多其他環境中使用。一個明顯的用例是正確處理數據庫連接和事務(open - process - commit()/rollback() - close()
)。其他用例是 nio 通道處理和臨界區中的線程同步(lock() - access shared resource - unlock()
)。它也能將api的已檢查異常轉換為未檢查異常。
當你在自己的項目中查找適合上下文重用的代碼時,請查找以下操作模式:
- 常規操作之前(general action before)
- 特殊操作(special action)
- 常規操作之后(general action after)
當你找到這樣的模式時,前后的常規操作就可能實現上下文重用。
上下文作為模板方法
有時候你會希望在上下文中有多個插件點。如果上下文由許多較小的步驟組成,并且你希望上下文的每個步驟都可以自定義,則可以將上下文實現為模板方法。模板方法是一種 gof 設計模式?;旧希0宸椒▽⑺惴ɑ騾f議分成一系列步驟。一個模板方法通常作為一個單一的基類實現,并為算法或協議中的每一步提供一個方法。要自定義任何步驟,只需創建一個擴展模板方法基類的類,并重寫要自定義的步驟的方法。
下面的示例是作為模板方法實現的 jdbccontext。子類可以重寫連接的打開和關閉, 以提供自定義行為。必須始終重寫processrecord(resultset result)方法, 因為它是抽象的。此方法提供不屬于上下文的操作,在使用jdbccontext的不同情況下的操作都不相同。這個例子不是一個完美的jdbccontext。它僅用于演示在實現上下文時如何使用模板方法。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
public abstract class jdbccontext { datasource datasource = null ; // 無參數的構造函數可以用于子類不需要 datasource 來獲取連接 public jdbccontext() { } public jdbccontext(datasource datasource){ this .datasource = datasource; } protected connection openconnection() throws sqlexception{ return datasource.getconnection(); } protected void closeconnection(connection connection) throws sqlexception{ connection.close(); } // 必須始終重寫 processrecord(resultset result) 方法 protected abstract processrecord(resultset result) throws sqlexception ; public void execute(string sql, object[] parameters) throws sqlexception { connection connection = null ; preparedstatement statement = null ; resultset result = null ; try { connection = openconnection(); statement = connection.preparestatement(sql); for ( int i= 0 ; i < parameters.length; i++){ statement.setobject(i, parameters[i]); } result = statement.executequery(); while (result.next()){ processrecord(result); } } finally { if (result != null ){ try { result.close(); } catch (sqlexception e) { /* ignore */ } } if(statement != null){ try{ statement.close(); } catch(sqlexception e) { /* ignore */ } } if (connection != null ){ closeconnection(connection); } } } } |
這是擴展 jdbccontext 以讀取用戶列表的子類:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
public class readusers extends jdbccontext{ list users = new arraylist(); public readusers(datasource datasource){ super (datasource); } public list getusers() { return this .users; } protected void processrecord(resultset result){ user user = new user(); user.setname (result.getstring( "name" )); user.setemail(result.getstring( "email" )); users.add(user); } } |
下面是如何使用 readusers 類:
1
2
3
|
readusers readusers = new readusers(datasource); readusers.execute( "select * from users" , new object[ 0 ]); list users = readusers.getusers(); |
如果readusers類需要從連接池獲取連接并在使用后將其釋放回該連接池,則可以通過重寫openconnection()
和closeconnection(connection connection)
方法來插入該連接。
注意如何通過方法重寫插入操作代碼。jdbccontext的子類重寫processrecord方法以提供特殊的記錄處理。 在streamcontext示例中,操作代碼封裝在單獨的對象中,并作為方法參數提供。實現操作接口streamprocessor的對象作為參數傳遞給streamcontext類的processstream(...)
方法。
實施上下文時,你可以使用這兩種技術。jdbccontext類可以將實現操作接口的connectionopener和connectioncloser對象作為參數傳遞給execute方法,或作為構造函數的參數。就我個人而言,我更喜歡使用單獨的操作對象和操作接口,原因有兩個。首先,它使得操作代碼可以更容易單獨進行單元測試;其次,它使得操作代碼在多個上下文中可重用。當然,操作代碼也可以在代碼中的多個位置使用,但這只是一個優勢。畢竟,在這里我們只是試圖重用上下文,而不是重用操作。
結束語
現在你已經看到了兩種不同的重用代碼的方法。經典的功能重用和不太常見的上下文重用。希望上下文的重用會像功能重用一樣普遍。上下文重用是一種非常有用的方法,可以從 api 的底層細節(例如jdbc,io 或 nio api等)中抽象出代碼。特別是如果 api 包含需要管理的資源(打開和關閉,獲得并返回等)。
persistence/orm api、mr.persister 利用上下文重用來實現自動連接和事務生命周期管理。 這樣用戶將永遠不必擔心正確打開或關閉連接,或提交或回滾事務。mr.persister 提供了用戶可以將他們的操作插入的上下文。 這些上下文負責打開,關閉,提交和回滾。
流行的 spring 框架包含大量的上下文重用。 例如 springs jdbc 抽象。 spring 開發人員將其使用上下文重用作為“控制反轉”。 這不是 spring 框架使用的唯一一種控制反轉類型。 spring 的核心特性是依賴注入 bean 工廠或“應用程序上下文”。 依賴注入是另一種控制反轉。
以上所述是小編給大家介紹的java代碼重用之功能與上下文重用,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!
原文鏈接:https://www.cnblogs.com/nwgdk/p/9008548.html