A mutable object is simply an object which can change its state after
construction.
For example, StringBuilder
and Date
are
mutable objects, while String
and Integer
are immutable objects
.
A class may have a mutable object as a field.
There are two possible
cases for how the state of a mutable object field can change :
-
its state can be changed only by the native class - the native class creates
the mutable object field, and is the only class which is directly aware of its existence
-
its state can be changed both by the native class and by its callers
- the native class simply points
to a mutable object which was created
elsewhere
Both cases are valid design choices, but you must be aware of which one
is appropriate for each case.
If the mutable object field's state should be changed only by the native
class, then a defensive copy
of the mutable object must
be
made any time it is passed into (constructors and set
methods) or out of
(get
methods) the class.
If this is not
done, then it is simple for the
caller to break encapsulation, by changing the state of an object which is simultaneously visible
to both
the class and its caller.
Example
Planet
has a mutable object field fDateOfDiscovery
,
which is defensively copied in all constructors, and in getDateOfDiscovery
.
Planet
represents an immutable class, and has no set methods for its fields.
Note
that if the defensive copy of DateOfDiscovery
is not made, then
Planet
is no longer immutable!
import java.util.Date;
/**
* Planet is an immutable class, since there is no way to change
* its state after construction.
*/
public final class Planet {
public Planet (double aMass, String aName, Date aDateOfDiscovery) {
fMass = aMass;
fName = aName;
//make a private copy of aDateOfDiscovery
//this is the only way to keep the fDateOfDiscovery
//field private, and shields this class from any changes that
//the caller may make to the original aDateOfDiscovery object
fDateOfDiscovery = new Date(aDateOfDiscovery.getTime());
}
/**
* Returns a primitive value.
*
* The caller can do whatever they want with the return value, without
* affecting the internals of this class. Why? Because this is a primitive
* value. The caller sees its "own" double that simply has the
* same value as fMass.
*/
public double getMass() {
return fMass;
}
/**
* Returns an immutable object.
*
* The caller gets a direct reference to the internal field. But this is not
* dangerous, since String is immutable and cannot be changed.
*/
public String getName() {
return fName;
}
// /**
// * Returns a mutable object - likely bad style.
// *
// * The caller gets a direct reference to the internal field. This is usually dangerous,
// * since the Date object state can be changed both by this class and its caller.
// * That is, this class is no longer in complete control of fDate.
// */
// public Date getDateOfDiscovery() {
// return fDateOfDiscovery;
// }
/**
* Returns a mutable object - good style.
*
* Returns a defensive copy of the field.
* The caller of this method can do anything they want with the
* returned Date object, without affecting the internals of this
* class in any way. Why? Because they do not have a reference to
* fDate. Rather, they are playing with a second Date that initially has the
* same data as fDate.
*/
public Date getDateOfDiscovery() {
return new Date(fDateOfDiscovery.getTime());
}
// PRIVATE //
/**
* Final primitive data is always immutable.
*/
private final double fMass;
/**
* An immutable object field. (String objects never change state.)
*/
private final String fName;
/**
* A mutable object field. In this case, the state of this mutable field
* is to be changed only by this class. (In other cases, it makes perfect
* sense to allow the state of a field to be changed outside the native
* class; this is the case when a field acts as a "pointer" to an object
* created elsewhere.)
*/
private final Date fDateOfDiscovery;
}
分享到:
相关推荐
防御式编程 Defensive Programming.PPT完整版(精品课件) 大纲: 保护程序免遭非法输入数据的破坏 断言 错误处理技术 异常 隔离程序 辅助调试代码
SAS防御性编程和错误检查关键字:sas sql连接合并合并大数据分析宏oracle teradata mysql sas社区stackoverflow统计信息人工智慧AI Python R Java Javascript WPS Matlab SPSS Scala Perl CC#Excel MS Access JSON...
Defensive Security Handbook: Best Practices for Securing Infrastructure by Lee Brotherston English | 3 Apr. 2017 | ASIN: B06Y18XC5Y | 268 Pages | AZW3 | 3.88 MB Despite the increase of high-profile ...
Defensive_Database_Programming_with_SQL_Server
Despite the increase of high-profile hacks, record-breaking data leaks, and ransomware attacks, many organizations don’t have the budget to establish or outsource an information security (InfoSec) ...
詹妮弗·厄本(Jennifer Urban)和杰森·舒尔茨(Jason Schultz)提出了防御性专利许可(“原始DPL”)。 原始DPL本质上是一种标准化的,网络化的,未经协商的,范围广泛的,免版税的专利交叉许可。 原始DPL充分利用...
内容简介: Despite the increase of high-profile hacks, record-breaking data leaks, and ransomware attacks, many organizations don’t have the budget to establish or outsource an information security ...
defensive database programming
免责声明:此资源来源于网络,其著作权属于与本书相关的单位或个人。在此仅供学习和交流使用,请在下载后24小时内删除。如果因此而引起相关法律问题,本人概不负责,谢谢合作。
Defensive.Database.Programming.with.SQL.Server
使用注释来指定对方法的约束。 有一些注释可用于执行常规检查(即参数必须不为null,int值必须为正,等等),并可以通过带有suppor的java中的合同来使用完整样式的设计
Despite the increase of high-profile hacks, record-breaking data leaks, and ransomware attacks, many organizations don’t have the budget to establish or outsource an information security (InfoSec) ...
The practice of defensive school psychology THE PRACTICE OF DEFENSIVE SCHOOL PSYCHOLOGY DONALD A. LUPIANI Riverdale Country School Recent legal developments promise to bring school ...
防御性投资这是一个脚本,它将分析股票以确定它是否是一个良好的购买机会。 股票评估的标准是基于本杰明·格雷厄姆(Benjamin Graham)的《聪明的投资者》(The Intelligent Investor)一书中的建议,该书在1973年...
The practice of defensive school psychology THE PRACTICE OF DEFENSIVE SCHOOL PSYCHOLOGY DONALD A. LUPIANI Riverdale Country School Recent legal developments promise to bring school ...
某些软件对可用性,安全性或安全性的要求比通常更高。 通常在此类项目中,人们会按照一组特定的规则练习务实的偏执狂。 例如:应用程序任何级别的每个公共函数都应检查传递给它的所有参数。 显然检查它所依赖的nil...
Offensive-Defensive-Weight:进攻防守重量-熊体育分析
防御措施,在Wiki上添加 专门用于我当前正在开发的附件的Wiki。 该wiki使用laravel 5.6创建,允许访问者查看该附加组件的所有详细信息,从安装到每个新添加的物品的制作配方,甚至是即将到来的更新。 检查附加组件...
安全编码防御项目 这是Vulgar Bandidtos(Benjamin Daschel,Jacob Leque和Lander Brandt)的Java项目。 编译中 mvn clean compile assembly:single 这将在target目录中生成一个具有所有依赖项的.jar文件。 要求 您...