设计模式之建造者设计模式

建造者设计模式功能上很像元素的set方法,他实现了链式编程,使得一个复杂对象的构建过程和他的表示分离,属于创建型设计模式。

public class Student {
    Student student = new Student();
    private String id;
    private String clazz;
    private String age;
    private String number;


    public Student appentId (String id){
        this.student.id = id;
        return this;
    }
    public Student appentClazz (String clazz){
        this.student.clazz = clazz;
        return this;
    }
    public Student appentAge (String age){
        this.student.age = age;
        return this;
    }
    public Student appentNumber (String number){
        this.student.clazz = number;
        return this;
    }
    public Student getStudent() {
        return student;
    }

    public void setStudent(Student student) {
        this.student = student;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getClazz() {
        return clazz;
    }

    public void setClazz(String clazz) {
        this.clazz = clazz;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public String getNumber() {
        return number;
    }

    public void setNumber(String number) {
        this.number = number;
    }

}

调用方

 public static void main(String[] args) {
        //感觉好像和实际赋值也没啥区别,就代码更简洁了。
        Student student = new Student();
        student.appentId("123")
                .appentAge("15")
                .appentClazz("高三二班")
                .appentNumber("0328");

        student.setAge("15");
        student.setClazz("高三二班");
        student.setId("123");

    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值