File tree Expand file tree Collapse file tree 16 files changed +251
-0
lines changed Expand file tree Collapse file tree 16 files changed +251
-0
lines changed Original file line number Diff line number Diff line change
1
+ <?xml version =" 1.0" encoding =" UTF-8" ?>
2
+ <classpath >
3
+ <classpathentry kind =" src" path =" src" />
4
+ <classpathentry kind =" con" path =" org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7" />
5
+ <classpathentry kind =" output" path =" bin" />
6
+ </classpath >
Original file line number Diff line number Diff line change
1
+ /bin /
Original file line number Diff line number Diff line change
1
+ <?xml version =" 1.0" encoding =" UTF-8" ?>
2
+ <projectDescription >
3
+ <name >offer-java</name >
4
+ <comment ></comment >
5
+ <projects >
6
+ </projects >
7
+ <buildSpec >
8
+ <buildCommand >
9
+ <name >org.eclipse.jdt.core.javabuilder</name >
10
+ <arguments >
11
+ </arguments >
12
+ </buildCommand >
13
+ </buildSpec >
14
+ <natures >
15
+ <nature >org.eclipse.jdt.core.javanature</nature >
16
+ </natures >
17
+ </projectDescription >
Original file line number Diff line number Diff line change
1
+ eclipse.preferences.version =1
2
+ org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode =enabled
3
+ org.eclipse.jdt.core.compiler.codegen.targetPlatform =1.7
4
+ org.eclipse.jdt.core.compiler.codegen.unusedLocal =preserve
5
+ org.eclipse.jdt.core.compiler.compliance =1.7
6
+ org.eclipse.jdt.core.compiler.debug.lineNumber =generate
7
+ org.eclipse.jdt.core.compiler.debug.localVariable =generate
8
+ org.eclipse.jdt.core.compiler.debug.sourceFile =generate
9
+ org.eclipse.jdt.core.compiler.problem.assertIdentifier =error
10
+ org.eclipse.jdt.core.compiler.problem.enumIdentifier =error
11
+ org.eclipse.jdt.core.compiler.source =1.7
Original file line number Diff line number Diff line change
1
+ # 数据结构与算法 面试题(Java版)
2
+
3
+ - [ 单例模式的六种实现] ( /review02 )
4
+ - [ 二位数组中的查找] ( /review03 )
5
+ - [ 字符串替换空格] ( /review04 )
Original file line number Diff line number Diff line change
1
+ ## 为什么使用单例模式
2
+ 需要确保某个类只要一个对象,或创建一个类需要消耗的资源过多,如访问IO和数据库操作等,这时就需要考虑使用单例模式了。
3
+
4
+ ## 使用单例模式需要注意的关键点
5
+ 将构造函数访问修饰符设置为private
6
+ 通过一个静态方法或者枚举返回单例类对象
7
+ 确保单例类的对象有且只有一个,特别是在多线程环境下
8
+ 确保单例类对象在反序列化时不会重新构建对象
9
+ ## 单例模式的几种写法
10
+
11
+ ### 1. 饿汉式
12
+ [ Singleton1.java] ( /Singleton1.java )
13
+ ### 2. 懒汉式
14
+ getInstance()方法中添加了synchronized关键字,使其变成一个同步方法,目的是为了在多线程环境下保证单例对象唯一。
15
+
16
+ 优点: 只有在使用时才会实例化单例,一定程度上节约了资源。
17
+ 缺点: 第一次加载时要立即实例化,反应稍慢。每次调用getInstance()方法都会进行同步,这样会消耗不必要的资源。这种模式一般不建议使用。
18
+
19
+ [ Singleton2.java] ( /Singleton2.java )
20
+ ### 3. DCL(Double CheckLock)实现单例
21
+ 优点: 资源利用率高,既能够在需要的时候才初始化实例,又能保证线程安全,同时调用getInstance()方法不进行同步锁,效率高。
22
+ 缺点: 第一次加载时稍慢,由于Java内存模型的原因偶尔会失败。在高并发环境下也有一定的缺陷,虽然发生概率很小。
23
+ DCL模式是使用最多的单例模式实现方式,除非代码在并发场景比较复杂或者JDK 6以下版本使用,否则,这种方式基本都能满足需求。
24
+
25
+ [ Singleton3.java] ( /Singleton3.java )
26
+ ### 4. 静态内部类
27
+ 第一次加载Singleton类时不会初始化instance,只有在第一次调用getInstance()方法时,虚拟机会加载SingletonHolder类,初始化instance。
28
+
29
+ 这种方式既保证线程安全,单例对象的唯一,也延迟了单例的初始化,推荐使用这种方式来实现单例模式。
30
+
31
+ [ Singleton4.java] ( /Singleton4.java )
32
+ ### 5. 枚举单例
33
+ 默认枚举实例的创建是线程安全的,即使反序列化也不会生成新的实例,任何情况下都是一个单例。
34
+
35
+ 优点: 简单!
36
+
37
+ [ Singleton5.java] ( /Singleton5.java )
38
+ ### 6. 容器实现单例
39
+ SingletonManager可以管理多个单例类型,使用时根据key获取对象对应类型的对象。这种方式可以通过统一的接口获取操作,隐藏了具体实现,降低了耦合度。
40
+
41
+ [ Singleton6.java] ( /Singleton6.java )
Original file line number Diff line number Diff line change
1
+ package review02 ;
2
+ /*
3
+ * 饿汉式实现单例模式
4
+ */
5
+ public class Singleton1 {
6
+ private static Singleton1 instance = new Singleton1 ();
7
+
8
+ private Singleton1 () {
9
+ }
10
+
11
+ public static Singleton1 getInstance () {
12
+ return instance ;
13
+ }
14
+ }
Original file line number Diff line number Diff line change
1
+ package review02 ;
2
+ /**
3
+ * 懒汉式实现单例模式
4
+ */
5
+ public class Singleton2 {
6
+ private static Singleton2 instance ;
7
+
8
+ private Singleton2 () {
9
+ }
10
+
11
+ // synchronized方法,多线程情况下保证单例对象唯一
12
+ public static synchronized Singleton2 getInstance () {
13
+ if (instance == null ) {
14
+ instance = new Singleton2 ();
15
+ }
16
+ return instance ;
17
+ }
18
+ }
Original file line number Diff line number Diff line change
1
+ package review02 ;
2
+
3
+ /**
4
+ * DCL实现单例模式
5
+ */
6
+ public class Singleton3 {
7
+ private static Singleton3 instance = null ;
8
+
9
+ private Singleton3 () {
10
+ }
11
+
12
+ public static Singleton3 getInstance () {
13
+ // 两层判空,第一层是为了避免不必要的同步
14
+ // 第二层是为了在null的情况下创建实例
15
+ if (instance == null ) {
16
+ synchronized (Singleton3 .class ) {
17
+ if (instance == null ) {
18
+ instance = new Singleton3 ();
19
+ }
20
+ }
21
+
22
+ }
23
+ return instance ;
24
+ }
25
+ }
Original file line number Diff line number Diff line change
1
+ package review02 ;
2
+
3
+ /**
4
+ * 静态内部类实现单例模式
5
+ */
6
+ public class Singleton4 {
7
+ private Singleton4 () {
8
+ }
9
+
10
+ public static Singleton4 getInstance () {
11
+ return SingletonHolder .instance ;
12
+ }
13
+
14
+ /**
15
+ * 静态内部类
16
+ */
17
+ private static class SingletonHolder {
18
+ private static Singleton4 instance = new Singleton4 ();
19
+ }
20
+ }
You can’t perform that action at this time.
0 commit comments