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年出版,到现在已经将 ...
随机推荐
- Android 学习笔记之Volley开源框架解析(二)
PS:Volley已经学完,可以安心的写一下博客总结一下了... 学习内容: 1.Request的完整封装... 2.RetryPolicy,DefaultRetryPolicy(请求重试策略源码解析 ...
- Ioc原理及常用框架
1 IoC理论的背景 我们都知道,在采用面向对象方法设计的软件系统中,它的底层实现都是由N个对象组成的,所有的对象通过彼此的合作,最终实现系统的业务逻辑. 图1:软件系统中耦合的对象 如果我们 ...
- C#方法的六种参数,值参数、引用参数、输出参数、参数数组、命名参数、可选参数
方法的参数有六种,分别是值参数.引用参数.输出参数.参数数组.命名参数.可选参数. 值参数 值参数是方法的默认类型,通过复制实参的值到形参的方式把数据传递到方法,方法被调用时,系统作两步操作: 在栈中 ...
- 遇上了artTemplate做的东西
js现在最牛的地方是 有了Node.js后,前后端的界限几乎都消失了,围绕着它,出现了一整套生态体系. 在生态方面,比php好太多了.
- [PE结构分析] 9.导出表 IMAGE_EXPORT_DIRECTORY
typedef struct _IMAGE_EXPORT_DIRECTORY { DWORD Characteristics; // 未使用,总为0 DWORD TimeDateStamp; // 文 ...
- 【Java Saves!】Session 5:计算机器之三--二指禅
人有十指.人类掰着手指头,发明出了0.1.2-9这十个数字.后来手指头不够用了,便发明出数位(个.十.百.千-)和满十进一的规则,称为十进制. 而计算机靠两个手指头工作.在计算机内部,只有0和1两个数 ...
- php 7 windows redis 扩展
搜了一圈也没找到redis 对于 php 7 windows 扩展,最后还是在apache lounge论坛找到了php7全扩展包 本人把里面的扩展全进行了上传(下载见本文底部) 在这里主要讲讲 ph ...
- poolboy的坑
poolboy是Erlang中运用非常广泛的进程池库,它有很多优点,使用简单,在很多项目中都能看到它的身影.不过,它也有一些坑,使用时候需要注意.(本文对poolboy的分析基于1.5.1版本) wo ...
- RAID选项
RAID:Redundant Array Independent Disk(独立磁盘构成的具有冗余能力的阵列) 最常见的为RAID类型为:0,1,5和10:3和6很少见,但在某些环境中仍然有用. RA ...
- [ASP.NET MVC] 使用CLK.AspNet.Identity提供以角色为基础的访问控制(RBAC)
[ASP.NET MVC] 使用CLK.AspNet.Identity提供以角色为基础的访问控制(RBAC) 程序代码下载 程序代码下载:点此下载 前言 ASP.NET Identity是微软所贡献的 ...