Effective Java 21 Use function objects to represent strategies
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
- Please give a field a discriptive name for the specific implementation.
- The anonymous class may create a new instance for each invoking.
- 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的更多相关文章
- Effective Java 52 Refer to objects by their interfaces
Principle If appropriate interface types exist, then parameters, return values, variables, and field ...
- 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 ...
- 《Effective Java》读书笔记 - 4.类和接口
Chapter 4 Classes and Interfaces Item 13: Minimize the accessibility of classes and members 一个好的模块设计 ...
- Effective Java Chapter4 Classes and Interface
MInimize the accessibility of classes and members 这个叫做所谓的 information hiding ,这么做在于让程序耦合度更低,增加程序的健壮性 ...
- Effective Java 目录
<Effective Java>目录摘抄. 我知道这看起来很糟糕.当下,自己缺少实际操作,只能暂时摘抄下目录.随着,实践的增多,慢慢填充更多的示例. Chapter 2 Creating ...
- 【Effective Java】阅读
Java写了很多年,很惭愧,直到最近才读了这本经典之作<Effective Java>,按自己的理解总结下,有些可能还不够深刻 一.Creating and Destroying Obje ...
- Effective Java 第三版——21. 为后代设计接口
Tips <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将 ...
- Effective Java通俗理解(持续更新)
这篇博客是Java经典书籍<Effective Java(第二版)>的读书笔记,此书共有78条关于编写高质量Java代码的建议,我会试着逐一对其进行更为通俗易懂地讲解,故此篇博客的更新大约 ...
- Effective Java 第三版——10. 重写equals方法时遵守通用约定
Tips <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将 ...
随机推荐
- HTML5 新特性总结
1.使用autocomplete 自动完成必须给input 加上name. 2.SVG图形代码 复制https://developer.mozilla.org/zh-CN/docs/Web/SVG/E ...
- Mysql的NULL的一个注意点
今天看到一个问题: select a.* from a where (a.id not in (NULL)); 这个sql语句会返回什么? 刚开始以为是返回所有item,但是实际运行下返回是返回emp ...
- OS X(EI Capitan)常用快捷键整理
刚上班就配了台RMBP,虽然触摸板确实好用,但是对鼠标的支持太差导致无法使用鼠标,而某些功能用触摸板还是不很方便 于是使用快捷键就势在必行了,参考了苹果官方文档和一些其他资料,整理出常用快捷键清单 准 ...
- LNK1179 无效或损坏的文件: 重复的 COMDAT“_IID_IDispatchEx”
fatal error LNK1179: invalid or corrupt file: duplicate comdat "XXX" LNK1179 无效或损坏的文件: 重复 ...
- asp.net中绘制大数据量的可交互的图表
在一个asp.net项目中要用到能绘制大数据量信息的图表,并且是可交互的(放大.缩小.导出.打印.实时数据),能够绘制多种图形. 为此进行了多方调查预研工作,预研过微软的MsChart图表组件.基于j ...
- sql server索引功能资料
无论何时对基础数据执行插入.更新或删除操作,SQL Server 数据库引擎都会自动维护索引.随着时间的推移,这些修改可能会导致索引中的信息分散在数据库中(含有碎片).当索引包含的页中的逻辑排序(基于 ...
- bootstrap - table
http://bootstrap-table.wenzhixin.net.cn/zh-cn/documentation/
- alter语法的简单的使用
用alter语法来新增列名,修改列名,删除列
- docker与虚拟机性能比较
http://blog.csdn.net/cbl709/article/details/43955687www.chenbiaolong.com 概要 Docker是近年来新兴的虚拟化工具,它可以和虚 ...
- 线段树---Atlantis
题目网址:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=110064#problem/A Description There are se ...