auto commit
This commit is contained in:
parent
f8ca395296
commit
8edbeacff7
|
@ -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 {
|
|||
|
||||
## 类图
|
||||
|
||||
<div align="center"> <img src="../pics//920c034c-c212-4f79-9ddb-84e4bb6cd088.png"/> </div><br>
|
||||
<div align="center"> <img src="../pics//14cfe8d4-e31b-49e0-ac6a-6f4f7aa06ab6.png"/> </div><br>
|
||||
|
||||
抽象工厂模式创建的是对象家族,也就是很多对象而不是一个对象,并且这些对象是相关的,也就是说必须一起创建出来。而工厂模式只是用于创建一个对象,这和抽象工厂模式有很大不同。
|
||||
|
||||
|
@ -305,12 +305,12 @@ public class ProductA2 extends AbstractProductA {
|
|||
```
|
||||
|
||||
```java
|
||||
public class ProductB1 extends AbstractProductB{
|
||||
public class ProductB1 extends AbstractProductB {
|
||||
}
|
||||
```
|
||||
|
||||
```java
|
||||
public class ProductB2 extends AbstractProductB{
|
||||
public class ProductB2 extends AbstractProductB {
|
||||
}
|
||||
```
|
||||
|
||||
|
@ -322,7 +322,7 @@ public abstract class AbstractFactory {
|
|||
```
|
||||
|
||||
```java
|
||||
public class ConcreteFactory1 extends AbstractFactory{
|
||||
public class ConcreteFactory1 extends AbstractFactory {
|
||||
AbstractProductA createProductA() {
|
||||
return new ProductA1();
|
||||
}
|
||||
|
|
BIN
pics/14cfe8d4-e31b-49e0-ac6a-6f4f7aa06ab6.png
Normal file
BIN
pics/14cfe8d4-e31b-49e0-ac6a-6f4f7aa06ab6.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 50 KiB |
Loading…
Reference in New Issue
Block a user