ChapterOne
learning ...
#ChapterOne
RuleOne
package chapter01.rule01;
import lombok.Data;
/**
* 静态工厂
* @author 82341
*/
@Data
public class staticFactoryMethod {
private String name;
private Integer age;
private staticFactoryMethod(){}
public static staticFactoryMethod factoryMethod(){
return new staticFactoryMethod();
}
public staticFactoryMethod name(String name){
this.name = name;
return this;
}
public staticFactoryMethod age(Integer age){
this.age = age;
return this;
}
public static void main(String[] args) {
//流式API
new staticFactoryMethod().name("chz").age(23);
}
}
RuleTwo
package chapter01.rule02;
import lombok.Data;
@Data
public class Person {
private String name;
private Integer age;
private Boolean gender;
private Double salary;
private Person(Builder builder) {
name = builder.name;
age = builder.age;
gender = builder.gender;
salary = builder.salary;
}
@Data
public static class Builder {
private String name;
private Integer age;
private Boolean gender;
private Double salary;
public Builder(String name, Integer age) {
//这里的this指的时静态内部类
this.name = name;
this.age = age;
}
public Builder gender(Boolean gender) {
this.gender = gender;
return this;
}
public Builder salary(Double salary) {
this.salary = salary;
return this;
}
/**
* 关键的一步, 将Builder获取到的值传给Person
*/
public Person build() {
return new Person(this);
}
}
public static void main(String[] args) {
Person person = new Builder("chz", 23)
.gender(Boolean.TRUE).salary(200D)
.build();
System.out.println(person);
}
}
RuleThree
package chapter01.rule03;
/**
* 单例模式一
*/
class Elvis {
//使用公开的类变量
public static final Elvis elvis = new Elvis();
private Elvis() {
System.out.println("Elvis is unique");
}
public void sing() {
System.out.println("sing out of tune");
}
}
/**
* 单例模式二
*/
class Bowie {
private static final Bowie bowie = new Bowie();
private Bowie() {
System.out.println("rain man");
}
public static Bowie getInstance() {
return bowie;
}
public void sing() {
System.out.println("sing out of tune");
}
}
/**
* 单例模式三, 推荐
* enum无偿提供序列化
* 反编译的Enum对象自动实现Serializable
*/
public enum Teacher {
INSTANCE("zs", 22);
private final String name;
private final Integer age;
Teacher(String name, Integer age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public Integer getAge() {
return age;
}
public void say() {
System.out.println("hello world");
}
public static void main(String[] args) {
Elvis.elvis.sing();
Bowie.getInstance().sing();
Teacher.INSTANCE.say();
}
}
#RuleFour
对于工具类, 因该要私有化构造器, 避免工具类实例化, 同时工具类将不能被实例化
#RuleFive
当创建一个新的实例时, 就将该资源传入到构造器中. 这就是依赖注入(dependency injection)
As follows:
class SpellChecker{
private final Lexicon dictionary;
public SpellChecker(Lexicon dictionary){
this.dictionary = Objects.requireNonNull(dictionary);
}
}
#RuleSix
-
对于创建成本高的对象, 要将对象缓存下来
As follows:
public class RomanNumberals {
private static final Pattern ROMAN = Pattern.compile("");
static boolean isRoman(String s){
//创建matcher并判断是否匹配对应的正则
return ROMAN.matcher(s).matches();
}
}
这里的final
作用是将对象存入常量池中, 以免被gabage collector
回收, 造成不必要的性能浪费
-
要优先使用基本数据类型, 而不是装箱基本类型, 当心无意识的自动装箱.
创建多余的对象会降低性能
As follows:
String s = new String("jetbrain");
Integer i = new Integer(23);
但是对于domain object
应该使用装箱的基本类型, 防止基本类型的初始化赋值