Java代码重用之功能与上下文重用

2025-05-29 0 91

我几乎不需要讨论为什么重用代码是有利的。代码重用通常使得程序开发更加快速,并使得 bug 减少。一旦一段代码被封装和重用,那么只需要检查很少的一段代码即可确保程序的正确性。如果在整个应用程序中只需要在一个地方打开和关闭数据库连接,那么确保连接是否正常则容易的多。但我确信这些你已经都知道了。

有两种类型的重用代码,我称它们为重用类型:

第一种类型是功能重用,这是最常见的一种重用类型。这也是大多数开发人员掌握的一种。即重用一组后续指令来执行某种操作。

第二种类型是上下文重用,即不同功能或操作代码在相同上下文之间,将相同上下文封装为重用代码(这里的上下文指的是一系列相同的操作指令)。虽然它在控制反转中越来越受欢迎但它并不常见。而且,上下文重用并没有被明确的描述,因此它并没有像功能重用一样被系统的使用。我希望你看完这篇文章之后会有所改变。

功能重用

功能重用是最常见的重用类型。它是一组执行某种操作指令的重用。下面两个方法都是从数据库中读取数据:

?

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 设计模式。基本上,模板方法将算法或协议分成一系列步骤。一个模板方法通常作为一个单一的基类实现,并为算法或协议中的每一步提供一个方法。要自定义任何步骤,只需创建一个扩展模板方法基类的类,并重写要自定义的步骤的方法。

下面的示例是作为模板方法实现的 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代码重用之功能与上下文重用,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对快网idc网站的支持!

原文链接:https://www.cnblogs.com/nwgdk/p/9008548.html

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 Java代码重用之功能与上下文重用 https://www.kuaiidc.com/112049.html

相关文章

发表评论
暂无评论