Effective Java 21 Use function objects to represent strategies

本文介绍如何在Java中使用策略模式实现函数指针的概念,通过具体示例展示了如何定义比较器来对字符串数组按长度进行排序,并探讨了不同实现方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Theory

In the Java the function pointers is implemented by the declaring an interface to represent strategy and a class that implements this interface for each concrete strategy. It is possible to define an object whose methods perform operations on other objects, passed explicitly to the methods. An instance of a class that exports exactly one such method is effectively a pointer to that method.

   

package com.effectivejava.classinterface;  

import java.util.Arrays;

import java.util.Comparator;

   

/**

* @author Kaibo

*

*/

public class StringLengthComparator implements Comparator<String> {  

public static final StringLengthComparator INSTANCE = new StringLengthComparator();

       private StringLengthComparator() {

}

/*

* override the super class key method.

*/

public int compare(String s1, String s2) {

return s1.length() - s2.length();

}

   

public static void main(String[] args) {

String[] a = new String[] { "I", "This", "a", "is" };

System.out.println("a origin values:");

printStringArray(a);

System.out.println("b sorted values:");

Arrays.sort(a, StringLengthComparator.INSTANCE);

for (int i = 0, len = a.length; i < len; i++)

System.out.println(a[i]);

System.out.println();

String[] b = new String[] { "He", "is", "a", "programmer" };

System.out.println("b origin values:");

printStringArray(b);

System.out.println("b sorted values:");

System.out.println();

// implement the compartor strategy by anounymous class

Arrays.sort(b, new Comparator<String>() {

public int compare(String s1, String s2) {

return s1.length() - s2.length();

}

});

}

private static void printStringArray(String[] strs) {

for (int i = 0, len = strs.length; i < len; i++)

System.out.println(strs[i]);

}

}

   

Note

  1. Please give a field a discriptive name for the specific implementation.
  2. The anonymous class may create a new instance for each invoking.
  3. When a concrete strategy is designed for repeated use, it is generally implemented as a private static member class and exported in a public static final field whose type is the strategy interface instead of expose it to the public directly.

/**

*

*/

package com.effectivejava.classinterface;

import java.io.Serializable;

import java.util.Comparator;

/**

* @author Kaibo

*

*/

// Exporting a concrete strategy

class Host {

private static class StrLenCmp implements Comparator<String>, Serializable {

private static final long serialVersionUID = 1L;

public int compare(String s1, String s2) {

return s1.length() - s2.length();

}

}

// Returned comparator is serializable

public static final Comparator<String> STRING_LENGTH_COMPARATOR = new StrLenCmp();

// Bulk of class omitted

}

   

转载于:https://www.cnblogs.com/haokaibo/p/use-function-objects-to-represent-strategies.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值