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. C++ async task

    最近在搞Android 开发,里面多线程的使用比较频繁,java多线程接口很方便. Thread, AysncTask, Handler 这些接口比起posix提供的pthread_create()等 ...

  2. Laravel 5 多个视图共享数据的方法

    我们都知道模板一般会用到继承,导航栏就是一个很好的例子,但是导航栏的数据如何共享,比如有个导航的文件叫在view/navigation.blade.php 为了简单一点,文件里只有设置了一个变量 {{ ...

  3. Mysql中的各种timeout

    在使用MySQL的过程中,你是否遇到了众多让人百思不得其解的Timeout?那么这些Timeout之后,到底是代码问题,还是不为人知的匠心独具?本期Out-man,讲述咱们MySQL DBA自己的Ti ...

  4. Travis-CI与Latex构建开源中文PDF

    博主有一本开源书籍,用 latex 排版,托管在Github上.但用 latex 不像是 Markdown,当tex文本更新时,用于最终浏览的PDF文件很难得到及时的更新, 所以博主一直想找到一套工具 ...

  5. Linq专题之创建Linq查询表达式

    本节我们主要介绍一下如何创建查询集合类型,关系数据库类型,DataSet对象类型和XML类型的数据源的Linq查询表达式. 下面在实例代码ReadyCollectionData()函数创建了准备的数据 ...

  6. HTML5实现音频播放

    Web 上的音频 直到现在,仍然不存在一项旨在网页上播放音频的标准. 今天,大多数音频是通过插件(比如 Flash)来播放的.然而,并非所有浏览器都拥有同样的插件. HTML5 规定了一种通过 aud ...

  7. Python基础:函数式编程

    一.概述 Python是一门多范式的编程语言,它同时支持过程式.面向对象和函数式的编程范式.因此,在Python中提供了很多符合 函数式编程 风格的特性和工具. 以下是对 Python中的函数式编程 ...

  8. WebApi传参总动员(填坑)

    本以为系列文章已经Over,突然记起来前面留了个大坑还没填,真是自己给自己挖坑. 这个坑就是: (body 只能被读取一次)Only one thing can read the body MVC和W ...

  9. 重新想象 Windows 8 Store Apps (50) - 输入: 边缘手势, 手势操作, 手势识别

    [源码下载] 重新想象 Windows 8 Store Apps (50) - 输入: 边缘手势, 手势操作, 手势识别 作者:webabcd 介绍重新想象 Windows 8 Store Apps ...

  10. csharp: Speech

    Speech SDK 5.1https://www.microsoft.com/en-us/download/details.aspx?id=10121 detects mobile devices ...