《Core Java 2》读书笔记(二)
1,防御性编程。必要时应当考虑采取保护性拷贝的手段来保护内部的私有数据,先来看下面这个例子:
pubic final class Period
{
private final Date start;
private final Date end;
public Period(Date start, Date end)
{
if (start.compareTo(end) > 0)
throw new IllegalArgumentException(start + "after " + end);
this.start = start;
this.end = end;
}
public Date getStart()
{
return start;
}
public Date getEnd()
{
return end;
}
}
{
private final Date start;
private final Date end;
public Period(Date start, Date end)
{
if (start.compareTo(end) > 0)
throw new IllegalArgumentException(start + "after " + end);
this.start = start;
this.end = end;
}
public Date getStart()
{
return start;
}
public Date getEnd()
{
return end;
}
}
这个类存在两个不安全的地方,首先来看第一个攻击代码
Date start = new Date();
Date end = new Date();
Period p = new Period(start, end);
end.setYear(78);//改变p的内部数据!
Date end = new Date();
Period p = new Period(start, end);
end.setYear(78);//改变p的内部数据!
这是因为外部和内部引用了同样的数据,为了解决这个问题,应当修改Period的构造函数:
public Period(Date start, Date end)
{
this.start = new Date(start.getTime());
this.end = new Date(end.getTime());
if (start.compareTo(end) > 0)
throw new IllegalArgumentException(start + "after " + end);
}
{
this.start = new Date(start.getTime());
this.end = new Date(end.getTime());
if (start.compareTo(end) > 0)
throw new IllegalArgumentException(start + "after " + end);
}
这样内部的私有数据就与外部对象指向不同,则不会被外部改变
再来看第二个攻击代码:
Date start = new Date();
Date end = new Date();
Period p = new Period(start, end);
p.getEnd().setYear(78);//改变p的内部数据!
Date end = new Date();
Period p = new Period(start, end);
p.getEnd().setYear(78);//改变p的内部数据!
这很显然是由于公有方法暴露了内部私有数据,我们可以只返回内部私有数据的只读版本(即其一份拷贝)
public Date getStart()
{
return (Date)start.clone();
}
public Date getEnd()
{
return (Date)end.clone();
}
{
return (Date)start.clone();
}
public Date getEnd()
{
return (Date)end.clone();
}
2,读到上面这个例子,我想起来了下面这样的代码片段
public class Suit
{
private final String name;
private static int nextOrdinal = 0;
private final int ordinal = nextOrdinal++;
private Suit(String name)
{
this.name = name;
}
public String toString()
{
return name;
}
public int compareTo(Object o)
{
return o
}
public static final Suit CLUBS = new Suit("Clubs");
public static final Suit DIAMONDS = new Suit("diamonds");
public static final Suit HEARTS = new Suit("hearts");
public static final Suit SPADES = new Suit("spades");
private static final Suit[] PRIVATE_VALUES = {CLUBS,DIAMONDS,HEARTS,SPADES};
public static final List VALUES = Collections.unmodifiedList(Arrays.asList(PRIVATE_VALUES));
}
{
private final String name;
private static int nextOrdinal = 0;
private final int ordinal = nextOrdinal++;
private Suit(String name)
{
this.name = name;
}
public String toString()
{
return name;
}
public int compareTo(Object o)
{
return o
}
public static final Suit CLUBS = new Suit("Clubs");
public static final Suit DIAMONDS = new Suit("diamonds");
public static final Suit HEARTS = new Suit("hearts");
public static final Suit SPADES = new Suit("spades");
private static final Suit[] PRIVATE_VALUES = {CLUBS,DIAMONDS,HEARTS,SPADES};
public static final List VALUES = Collections.unmodifiedList(Arrays.asList(PRIVATE_VALUES));
}
作者:洞庭散人
出处:http://phinecos.cnblogs.com/
本博客遵从Creative Commons Attribution 3.0 License,若用于非商业目的,您可以自由转载,但请保留原作者信息和文章链接URL。
分类:
Java
posted on 2009-09-28 14:09 Phinecos(洞庭散人) 阅读(1216) 评论(0) 编辑 收藏 举报
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述
2007-09-28 基于JMF RTP的音视频传输