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年出版,到现在已经将 ...
随机推荐
- [git]用pelican搞一个自己的blog(已完成)
pelican Pelican Static Site Generator, Powered by Python:Pelican是python语言写的静态网站生成器.因为我一直打算用github pa ...
- 前端引擎初步设计稿 -通过配置生成动态页面 ,LandaSugar平台 .NET-C#-MVC
公司准备开发出一款项目开发平台 LandaSugar,分为 前端引擎.工作引擎.数据引擎 三大块,开发人员只需要对三大模块进行相应的配置便能够完成一个定制项目的开发. 听起来貌似是异想天开,但是是否真 ...
- Greedy --- HNU 13320 Please, go first
Please, go first Problem's Link: http://acm.hnu.cn/online/?action=problem&type=show&id=13320 ...
- C#设计模式——观察者模式(Observer Pattern)
一.概述在软件设计工作中会存在对象之间的依赖关系,当某一对象发生变化时,所有依赖它的对象都需要得到通知.如果设计的不好,很容易造成对象之间的耦合度太高,难以应对变化.使用观察者模式可以降低对象之间的依 ...
- zTree的使用
一.节点模糊搜索功能:搜索成功后,自动高亮显示并定位.展开搜索到的节点. 二.节点异步加载:1.点击展开时加载数据:2.选中节点时加载数据. 前台代码如下: <script type=" ...
- 对一个或多个实体的验证失败。有关详细信息,请参见“EntityValidationErrors”属性。
问题原因: 1.非空列未插入值错误 2.内容长度超过列最大长度(超过数据库设置长度,或者自定义长度“[StringLength(50, MinimumLength = 6, ErrorMessage ...
- windbg学习进阶之——dump分析常用命令收集
#重要说明 (1) windbg命令分为标准命令,元命令和扩展命令. 标准命令提供最基本的调试功能,不区分大小写.如:bp g dt dv k等 元命令提供标准命令没有提供的功能,也内建在调试 ...
- 【C#进阶系列】05 基元类型、引用类型和值类型
基元类型和FCL类型 FCL类型就是指Int32这种类型,这是CLR支持的类型. 而基元类型就是指int这种类型,这是C#编译器支持的,实际上在编译后,还是会被转为Int32类型. 而且学过C的朋友 ...
- javascript: jquery.gomap-1.3.3.js
from:http://www.pittss.lv/jquery/gomap/solutions.php jquery.gomap-1.3.3.js: /** * jQuery goMap * * @ ...
- macbook 我们需要买吗
能否写出好代码与是否使用“好”的电脑是没有直接关系的.