Information hiding is important for many reasons, most of which stem from the fact that it decouples the modules that comprise a system, allowing them to be developed, tested, optimized, used, understood, and modified in isolation.

Advantage

  1. Speeds up system development by parallel programming.
  2. Eases the burden of maintenance.
  3. Enables effective performance tuning.
  4. Increases software reuse.
  5. Decreases the risk in building large system.

Principle

1. make each class or member as inaccessible as possible.

Possible access levels for top level classes

Package-private

Public

For members (fields, methods, nested classes, and nested interfaces), there are four possible access levels, listed here in order of increasing accessibility:

• private—The member is accessible only from the top-level class where it is declared.

• package-private—The member is accessible from any class in the package where it is declared. Technically known as default access, this is the access level you get if no access modifier is specified.

• protected—The member is accessible from subclasses of the class where it is declared (subject to a few restrictions [JLS, 6.6.2]) and from any class in the package where it is declared.

• public—The member is accessible from anywhere.

2. Instance fields should never be public.

3. Classes with public mutable fields are not thread-safe.

4. It is wrong for a class to have a public static final array field, or an accessor that returns such a field.

a. make the public array private and add a public immutable list:

private static final Thing[] PRIVATE_VALUES = { ... };

public static final List<Thing> VALUES = Collections.unmodifiableList(Arrays.asList(PRIVATE_VALUES));

b. make the array private and add a public method that returns a copy of a private array:

private static final Thing[] PRIVATE_VALUES = { ... };

public static final Thing[] values() {

return PRIVATE_VALUES.clone();

}

Effective Java 13 Minimize the accessibility of classes and members的更多相关文章

  1. Effective Java 15 Minimize mutability

    Use immutable classes as much as possible instead of mutable classes. Advantage Easy to design, impl ...

  2. Effective Java 18 Prefer interfaces to abstract classes

    Feature Interface Abstract class Defining a type that permits multiple implementations Y Y Permitted ...

  3. Effective Java 45 Minimize the scope of local variables

    Principle The most powerful technique for minimizing the scope of a local variable is to declare it ...

  4. 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 ...

  5. 《Effective Java》读书笔记 - 4.类和接口

    Chapter 4 Classes and Interfaces Item 13: Minimize the accessibility of classes and members 一个好的模块设计 ...

  6. Effective Java Chapter4 Classes and Interface

    MInimize the accessibility of classes and members 这个叫做所谓的 information hiding ,这么做在于让程序耦合度更低,增加程序的健壮性 ...

  7. Effective Java 目录

    <Effective Java>目录摘抄. 我知道这看起来很糟糕.当下,自己缺少实际操作,只能暂时摘抄下目录.随着,实践的增多,慢慢填充更多的示例. Chapter 2 Creating ...

  8. 【Effective Java】阅读

    Java写了很多年,很惭愧,直到最近才读了这本经典之作<Effective Java>,按自己的理解总结下,有些可能还不够深刻 一.Creating and Destroying Obje ...

  9. Effective Java —— 使类和成员的可访问性最小化

    本文参考 本篇文章参考自<Effective Java>第三版第十五条"Minimize the accessibility of classes and members&quo ...

随机推荐

  1. 2013/11/21工作随笔-PHP开启多进程

    今天被问到一个问题,php如何开启多进程才比较稳定. php开启多进程执行一个操作有哪些方法: 首先想到的是使用pcntl的fork 具体可以参考之前的文章:PHP的pcntl多进程 其次想到的方法是 ...

  2. Win10安装.net framework 4.0失败提示已是操作系统一部分如何解决

    有位用户因为工作需求,所以想在win10系统电脑中安装microsoft .net framework 4.0.可是在安装过程中却失败了,还遇到提示"Microsoft.net framew ...

  3. 浏览器 UserAgent 相关知识整理

    总结整理时下流行的浏览器User-Agent大全 浏览器userAgent大全 各种浏览器UserAgent一览表(桌面+移动) 使用JS判断移动设备的终端类型(浏览器UserAgent) JS通过分 ...

  4. C#一个方法返回多个值

    示例代码: static void Main(string[] args) { //声明 int value; string strOutValue; //调用函数 //函数的参数有两个返回的值 Re ...

  5. 分享一个C#的分页类

    废话不说只有代码: using System.Linq; using System.Collections.Generic; namespace CommonLibrary { public clas ...

  6. 【循序渐进学Python】7.面向对象的核心——类型(上)

    我们知道Python是一门面向对象的脚本语言.从C#的角度来看:首先Python支持多继承.Python 类型成员通常都是public的,并且所有成员函数都是virtual的(可以直接重写). 1. ...

  7. jquery实现下拉框功能

    说不清楚,直接上图 <!DOCTYPE html> <html lang="en" xmlns="http://www.w3.org/1999/xhtm ...

  8. [moka收藏]php正则表达式验证

    手机号验证规则[['mobile'], 'match','pattern' =>"/^1[34578]\\d{9}$/"], [['sendmail_limit'],'mat ...

  9. Verilog学习笔记简单功能实现(一)...............D触发器

    module D_flop(data,clk,clr,q,qb); input data,clk,clr; output q,qb; wire a,b,c,d,e,f,ndata,nclk; nand ...

  10. 【Asphyre引擎】学习笔记(一)

    先来说说一下几个最基本的对象: TGraphicsDeviceProvider:这个对象决定我们的游戏是用什么来渲染的,比如DX或者OpenGL,DX还有多个版本可以选择. TCustomSwapCh ...