美文网首页
记:设计思想的学习

记:设计思想的学习

作者: 爱吃板栗的小女孩 | 来源:发表于2018-11-16 16:29 被阅读28次

以下内容为小渣渣对设计思想的小笔记,不适用大神,纯为自己记录

1.单例模式:

保证对象在整个应用程序的唯一性,避免对象重复创建

public class Singleton {
    /**
     * 单例模式:
     * (1)构造方法私有化
     * (2)对外提供共有可调用的方法
     * (3)保证线程安全
     * (4)volatile保证线程每次去读此变量时,都会去堆里读取最新的,保证instance最新的。保证可见性
     */
    private volatile static Singleton instance;

    private Singleton() {
    }

    public static Singleton getInstance() {
        if (instance == null) {
            //保证了避免不必要的同步
            synchronized (Singleton.class) {
                if (instance == null) {
                    //保证null才会创建实例
                    instance = new Singleton();
                }
            }
        }
        return instance;
    }
}

2.Builder构建者模式

适用于当对象比较复杂,包含属性很多,如果使用传统的构造方法传值(还有可能涉及到构造方法重载)或者get set传值,代码会变得很乱。

public class Student {
    private final String id;//必传
    private final String name;//必传
    private final int sex;//必传
    private final String address;//可选
    private final String like;//可选

    private Student(Builder builder) {
        this.id = builder.id;
        this.name = builder.name;
        this.sex = builder.sex;
        this.address = builder.address;
        this.like = builder.like;
    }

    public String getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public int getSex() {
        return sex;
    }

    public String getAddress() {
        return address;
    }

    public String getLike() {
        return like;
    }

    public static class Builder {
        private final String id;//必传
        private final String name;//必传
        private final int sex;//必传
        private String address;//可选
        private String like;//可选

        public Builder(String id, String name, int sex) {
            this.id = id;
            this.name = name;
            this.sex = sex;
        }

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

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

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

    @Override
    public String toString() {
        return "Student{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", sex=" + sex +
                ", address='" + address + '\'' +
                ", like='" + like + '\'' +
                '}';
    }
}

调用方法采用链式方法,可以选择需要的字段:

 Student student = new Student.Builder("1002", "波妞", 1)
                .setAddress("和平区")
                .setLike("看电视")
                .build();

3.观察者模式:

一种一对多的关系,当一个对象发生改变,其他注册的对象能收到通知并可根据情况作出改变
eg:EventBust

https://www.jianshu.com/p/3459188bc8f9
觉得这篇文章讲的很通透

4.访问者模式(不常用):

优点:适用于结构单一稳定、数据操作与数据结构分离
缺点:若后期数据结构有改动,改动量大

https://www.jianshu.com/p/80b9cd7c0da5
这篇文章例子举得相对于容易理解

//2018-12-21分享个链接https://www.jianshu.com/p/1a9f571ad7c0

相关文章

网友评论

      本文标题:记:设计思想的学习

      本文链接:https://www.haomeiwen.com/subject/vraubttx.html