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

}

Effective Java 21 Use function objects to represent strategies的更多相关文章

  1. Effective Java 52 Refer to objects by their interfaces

    Principle If appropriate interface types exist, then parameters, return values, variables, and field ...

  2. Effective Java Index

    Hi guys, I am happy to tell you that I am moving to the open source world. And Java is the 1st langu ...

  3. 《Effective Java》读书笔记 - 4.类和接口

    Chapter 4 Classes and Interfaces Item 13: Minimize the accessibility of classes and members 一个好的模块设计 ...

  4. Effective Java Chapter4 Classes and Interface

    MInimize the accessibility of classes and members 这个叫做所谓的 information hiding ,这么做在于让程序耦合度更低,增加程序的健壮性 ...

  5. Effective Java 目录

    <Effective Java>目录摘抄. 我知道这看起来很糟糕.当下,自己缺少实际操作,只能暂时摘抄下目录.随着,实践的增多,慢慢填充更多的示例. Chapter 2 Creating ...

  6. 【Effective Java】阅读

    Java写了很多年,很惭愧,直到最近才读了这本经典之作<Effective Java>,按自己的理解总结下,有些可能还不够深刻 一.Creating and Destroying Obje ...

  7. Effective Java 第三版——21. 为后代设计接口

    Tips <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将 ...

  8. Effective Java通俗理解(持续更新)

    这篇博客是Java经典书籍<Effective Java(第二版)>的读书笔记,此书共有78条关于编写高质量Java代码的建议,我会试着逐一对其进行更为通俗易懂地讲解,故此篇博客的更新大约 ...

  9. Effective Java 第三版——10. 重写equals方法时遵守通用约定

    Tips <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将 ...

随机推荐

  1. Thrift在Windows及Linux平台下的安装和使用示例

    本文章也同时发表在个人博客Thrift在Windows及Linux平台下的安装和使用示例上. thrift介绍 Apache Thrift 是 Facebook 实现的一种高效的.支持多种编程语言的R ...

  2. HT图形组件设计之道(一)

    HT for Web简称HT提供了涵盖通用组件.2D拓扑图形组件以及3D引擎的一站式解决方案,正如Hightopo官网所表达的我们希望提供:Everything you need to create ...

  3. 【推荐】iOS集合视图的可重新排序的layout

    在实际项目中你或许会遇到在一个集合视图中移动一项到另外一个位置,那么此时我们需要对视图中的元素进行重新排序,今天推荐一个很好用的第三方类LXReorderableCollectionViewFlowL ...

  4. 【第二课】深入理解Handler

    简要讲解Handler是做什么的 我们知道,在Android中,app启动会启动一个进程一个线程——UI线程,UI线程是主线程,并且不允许这个线程阻塞超过5秒,一旦超过5秒就会ANR. 所以较为耗时的 ...

  5. java switch语句注意的事项

    1.switch语句使用的变量只能是byte.char.short.string数据类型. 2.case后面gender数据必须是一个常量. 3.switch的停止条件: switch语句一旦比配上了 ...

  6. LeetCode - 41. First Missing Positive

    41. First Missing Positive Problem's Link ---------------------------------------------------------- ...

  7. 与众不同 windows phone (46) - 8.0 通信: Socket, 其它

    [源码下载] 与众不同 windows phone (46) - 8.0 通信: Socket, 其它 作者:webabcd 介绍与众不同 windows phone 8.0 之 通信 Socket ...

  8. csharp: Domain-Driven Design(领域驱动设计)

    http://dddsample.sourceforge.net/ https://github.com/citerus/dddsample-core http://dddsamplenet.code ...

  9. js文件被浏览器缓存的思考

        我们的用户量大,修改js文件后,用户反馈登录出现问题.实际上刷新一下就没事了.就是因为用户的浏览器使用的还是本地缓存的js代码.   强制刷新一般就会重新去服务器获取新的js代码.但不能让用户 ...

  10. 【FFmpeg】ffplay播放rtsp视频流花屏问题

    问题描述:ffplay播放rtsp视频流时,播放过程中随机出现花屏现象. 基本流程学习:阅读ffplay源码,熟悉其播放rtsp视频流的基本流程. 在ffplay源码阅读和分析的基础上,画出了其播放r ...