博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java coding style - Part one
阅读量:6540 次
发布时间:2019-06-24

本文共 1472 字,大约阅读时间需要 4 分钟。

Java coding style:

1. Factory method should be stateless.

State normally refers to the member variables of class. Stateless, more precisely, it means immutable.

Factory is just to create objects, and one call should not affect anothers, if it is mutable, then the method call mnight have the chance to change the states.

and then it would affect other calls which are not supposed to happen.
2.
Restrict the access level of all the class members as much as possible, this conforms to principle of encapsulation

3.

Assert to validate the parameters of constructor, this way to avoid the invalid data of member variables as early as in the construction stage
e.g. in Spring, if you assert to validate the values, intead of leaving the error to later, application even won't be able to start up when Spring tries to initialise these objects.

4.

Try best to use constructor to initialise the object, this way you can validate the assigned values to member variables, you can construct the object
in one place.

e.g. in Spring, using constructor to init the beans, you can make the member variables as "final", this way, the member variables get assigned and initialised

early in the construction stage, you don't have to create an empty object first and then set the fields using setters. What' more, the member variables won't be exposed to others
in this case, so you get a better access control over these member variables.

转载于:https://www.cnblogs.com/glf2046/p/4885408.html

你可能感兴趣的文章
[Gradle] 在 Eclipse 下利用 gradle 构建系统
查看>>
JAVAWEB 一一 Hibernate(框架)
查看>>
与、或、异或、取反、左移和右移
查看>>
jQuery根据元素值删除数组元素的方法
查看>>
Linux基础学习(14)--日志管理
查看>>
vue常用的指令
查看>>
matlab练习程序(随机游走图像)
查看>>
Linux命令行下运行java.class文件
查看>>
input文本框实现宽度自适应代码实例
查看>>
C#基本数据类型 <思维导图>
查看>>
POJ3321 Apple Tree (树状数组)
查看>>
protocol buffers的编码原理
查看>>
行为型设计模式之命令模式(Command)
查看>>
减少死锁的几个常用方法
查看>>
HDFS 核心原理
查看>>
正确配置jstl的maven依赖,jar包冲突的问题终于解决啦
查看>>
利用KMP算法解决串的模式匹配问题(c++) -- 数据结构
查看>>
登录内网账号后,连接不上内网网址
查看>>
安装 MariaDB
查看>>
【deep learning学习笔记】注释yusugomori的DA代码 --- dA.h
查看>>