Effective Java----1

1.Builder 模式
当类的构造函数有多个参数时,针对不同数量的构造函数,构造函数的编写显得很麻烦,可以选择使用Javabean模式编写,按照javabean的写法,私有化成员变量, 私有构造方法 ,通过setter和getter来设置和获取值 ,这种构造的缺点是传入的参数不好检测,例如有些非空的数据等,这时采用Builder模式来编写会比较方便,示例如下:

//Student .java

public class Student {  
    private int id;  
    private int classId;  
    private int schoolId;  
    private String name;  
    private String className;  
    private String schoolName;  
    private String sex;  
    private String age;  

    public static class Builder {  
        // 非空信息,限定值(必须填的)  
        private int id;  
        private int classId;  
        private int schoolId;  
        // 选择信息,可以不填的,不填默认为“未设定”  
        private String name = "未设定";  
        private String className = "未设定";  
        private String schoolName = "未设定";  
        private String sex = "未设定";  
        private String age = "未设定";  

        //builder构造方法 必须设置限定属性的值  
        public Builder(int id, int classId, int schoolId) {  
            this.id = id;  
            this.classId = classId;  
            this.schoolId = schoolId;  
        }  

        //外部提供的设置可选属性的值  
        public Builder name(String name) {  
            this.name = name;  
            return this;  
        }  

        public Builder className(String className) {  
            this.className = className;  
            return this;  
        }  

        public Builder schoolName(String schoolName) {  
            this.schoolName = schoolName;  
            return this;  
        }  

        public Builder sex(String sex) {  
            this.sex = sex;  
            return this;  
        }  

        public Builder age(String age) {  
            this.age = age;  
            return this;  
        }  

        public Student build(){  
            return new Student(this);  
        }  

    }  

    //私有化构造方法 外部不能直接new student  
    private Student(Builder builder) {  
        //通过赋值这种方法来检测传入的值得正确性 不正确会抛出异常  
        this.id = builder.id;  
        this.classId = builder.classId;  
        this.schoolId = builder.schoolId;  
        this.name = builder.name;  
        this.className = builder.className;  
        this.schoolName = builder.schoolName;  
        this.age = builder.age;  
        this.sex = builder.sex;  
    }  

    //提供访问对象各项属性数据的接口  
    public int getId() {  
        return id;  
    }  

    public int getClassId() {  
        return classId;  
    }  

    public int getSchoolId() {  
        return schoolId;  
    }  

    public String getName() {  
        return name;  
    }  

    public String getClassName() {  
        return className;  
    }  

    public String getSchoolName() {  
        return schoolName;  
    }  

    public String getSex() {  
        return sex;  
    }  

    public String getAge() {  
        return age;  
    }  

}
//Controler .java
package org.effectivejava.examples.chapter03.Builder;

public class Controler {  
    public static void main(String[] args) {  
        Student student = new Student.Builder(2012, 10086, 13800).name("罗康").age("20").sex("男")  
                .build();  
        System.out.println("名字:" + student.getName() + " - 学校名:" + student.getSchoolName()  
                + " - 性别:" + student.getSex());  
    }  
}

使用builder模式虽然增加了代码量,但是代码的可读性得到了大大增加,易于修改和维护。

参考:http://blog.csdn.net/iamkila/article/details/7824053

2.避免创建不必要的对象
在能够重用对象的场合,为了提高程序的性能,不要创建不必要的重复对象,如:

String s = new String("Effective Java");

String s = "Effective Java";

如果是对s做循环,后者在Java虚拟机中只在方法区的常量池中建立”Effective Java”,运行时直接压入Java栈中,而前者将在Java堆上创建大量String对象,对性能产生很大影响。
对于只需要使用初始值的对象来说,可以将对象的创建放到static语句块中,这样在类加载的时候static语句块就被加载到内存中了,准确地说被加载到方法区中,需要的时候可以直接使用,不用重新创建,提高了程序执行速度。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值