最近在看《 Effective Java 》这本书,里面提到了编码时需要注意【 保护性拷贝(Rule39) 】,这里备份下实现的注意点,截图版权属于原作者。
github:
【 https://github.com/cstriker1407/think_in_java 】
首先列出基本类:
public Rule39Bean( int value) |
public Rule39Bean(Rule39Bean bean) |
public Period(Rule39Bean small, Rule39Bean big) |
protected Rule39Bean small; |
protected Rule39Bean big; |
if (small.value < big.value) |
System.out.println( "Right!! small < big" ); |
System.out.println( "Wrong!! small >= big" ); |
public Rule39Bean getBig() |
使用方式A,有缺陷,未加任何保护:
class PeriodA extends Period |
public PeriodA(Rule39Bean small, Rule39Bean big) |
if (small.value >= big.value) |
throw new IllegalArgumentException( "small >= big" ); |
测试代码:
Rule39Bean small = new Rule39Bean( 10 ); |
Rule39Bean big = new Rule39Bean( 20 ); |
PeriodA periodA = new PeriodA(small, big); |
使用方式B,有缺陷,保护了构造函数,但是没有保护获取方法:
class PeriodB extends Period |
public PeriodB(Rule39Bean small, Rule39Bean big) |
this .small = new Rule39Bean(small); |
this .big = new Rule39Bean(big); |
if (small.value >= big.value) |
throw new IllegalArgumentException( "small >= big" ); |
测试代码:
Rule39Bean small = new Rule39Bean( 10 ); |
Rule39Bean big = new Rule39Bean( 20 ); |
PeriodB periodB = new PeriodB(small, big); |
periodB.getBig().value = 5 ; |
使用方式C,在B的基础上增加了对获取方式的保护:
class PeriodC extends PeriodB |
public PeriodC(Rule39Bean small, Rule39Bean big) |
public Rule39Bean getBig() |
return new Rule39Bean(big); |
测试代码:
Rule39Bean small = new Rule39Bean( 10 ); |
Rule39Bean big = new Rule39Bean( 20 ); |
PeriodC periodC = new PeriodC(small, big); |
periodC.getBig().value = 5 ; |
原书介绍:

发表评论