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();

}

最新文章

  1. Spring Security OAuth2 开发指南
  2. Android随笔之——闹钟制作铺垫之AlarmManager详解
  3. Caffe源码解析5:Conv_Layer
  4. oracle 字符转字符串函数
  5. Azure China (5) 管理Azure China Powershell
  6. PHP开发知识
  7. apache 访问出现403 Forbidden
  8. 硅谷新闻4--解决页签手指按下从左到右滑动的bug
  9. mysql数据库创建database(实例),和用户,并授权
  10. oracle 时间比较查询
  11. [C++程序设计]字符数组的赋值与引用
  12. OpenSUSE13.2安装MongoDB
  13. PACS发展历史
  14. Python内置函数(43)——min
  15. 【XSY2921】yja 拉格朗日乘法
  16. Shiro 系列 - 基本知识
  17. 【easy】263. Ugly Number 判断丑数
  18. webpack报错需要合适的loader
  19. jsp 自定义标签解决jsp页面中int时间戳的时间格式化问题
  20. 源码分析篇 - Android绘制流程(二)measure、layout、draw流程

热门文章

  1. 1 Servlet开篇准备
  2. Laravel 5 多个视图共享数据的方法
  3. Android View之用户界面...
  4. 参数嗅探(Parameter Sniffing)(1/2)
  5. C#语法糖之 cache操作类 asp.net
  6. LVM快照(snapshot)备份
  7. 奥特曼小分队之四(Work Breakdown Structure)
  8. Linq查询操作之Where筛选
  9. Winform开发框架之客户关系管理系统(CRM)的开发总结系列2-基于框架的开发过程
  10. onclick事件与onserverclick事件