diff --git a/notes/设计模式.md b/notes/设计模式.md index b7e6c76a..93cdd0da 100644 --- a/notes/设计模式.md +++ b/notes/设计模式.md @@ -128,13 +128,13 @@ if (uniqueInstance == null) { uniqueInstance 采用 volatile 关键字修饰也是很有必要的。 -`uniqueInstance = new Singleton();` 这段代码其实是分为三步执行。 +uniqueInstance = new Singleton(); 这段代码其实是分为三步执行。 1. 分配内存空间。 2. 初始化对象。 3. 将 uniqueInstance 指向分配的内存地址。 -但是由于 JVM 具有指令重排的特性,有可能执行顺序变为了 `1>3>2`,这在单线程情况下自然是没有问题。但如果是多线程就有可能 B 线程获得是一个还没有被初始化的对象以致于程序出错。 +但是由于 JVM 具有指令重排的特性,有可能执行顺序变为了 1>3>2,这在单线程情况下自然是没有问题。但如果是多线程就有可能 B 线程获得是一个还没有被初始化的对象以致于程序出错。 所以使用 volatile 修饰的目的是禁止 JVM 的指令重排,保证在多线程环境下也能正常运行。 @@ -178,17 +178,17 @@ public interface Product { ``` ```java -public class ConcreteProduct implements Product{ +public class ConcreteProduct implements Product { } ``` ```java -public class ConcreteProduct1 implements Product{ +public class ConcreteProduct1 implements Product { } ``` ```java -public class ConcreteProduct2 implements Product{ +public class ConcreteProduct2 implements Product { } ``` @@ -249,7 +249,7 @@ public class ConcreteFactory extends Factory { ``` ```java -public class ConcreteFactory1 extends Factory{ +public class ConcreteFactory1 extends Factory { public Product factoryMethod() { return new ConcreteProduct1(); } @@ -272,7 +272,7 @@ public class ConcreteFactory2 extends Factory { ## 类图 -