-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
162 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package Singleton; | ||
|
||
public class DoubleCheckedLocking { | ||
private static DoubleCheckedLocking instance; | ||
public static DoubleCheckedLocking getInstance(){ | ||
if(instance==null){ | ||
synchronized (DoubleCheckedLocking.class){ | ||
if(instance==null){ | ||
instance=new DoubleCheckedLocking(); | ||
} | ||
} | ||
} | ||
return instance; | ||
} | ||
|
||
public static void main(String[] args) { | ||
for (int i = 0; i < 100; i++) { | ||
new Thread(new Runnable() { | ||
@Override | ||
public void run() { | ||
System.out.println(Thread.currentThread().getName() + ":" + DoubleCheckedLocking.getInstance().hashCode()); | ||
} | ||
}).start(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package Singleton; | ||
|
||
public class HungrySingleton { | ||
private static HungrySingleton instance=new HungrySingleton(); | ||
|
||
//加上synchronized来保证线程安全 | ||
public static HungrySingleton getInstance(){ | ||
return instance; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package Singleton; | ||
|
||
public class SafeLazyInitialization { | ||
private static SafeLazyInitialization instance; | ||
|
||
//加上synchronized来保证线程安全 | ||
public static synchronized SafeLazyInitialization getInstance(){ | ||
if(instance==null){ | ||
instance=new SafeLazyInitialization(); | ||
} | ||
return instance; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package Singleton; | ||
|
||
public class Static_Class_Singleton { | ||
private static class SingletonHolder{ | ||
private static final Static_Class_Singleton instance=new Static_Class_Singleton(); | ||
} | ||
private static final Static_Class_Singleton getInstance(){ | ||
return SingletonHolder.instance; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package Singleton; | ||
|
||
//1.懒汉模式 | ||
public class UnsafeLazyInitialization { | ||
private static UnsafeLazyInitialization instance; | ||
|
||
//加上Synchronized来保证线程安全 | ||
public static UnsafeLazyInitialization getInstance(){ | ||
if(instance==null){ | ||
instance=new UnsafeLazyInitialization(); | ||
} | ||
return instance; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package Design_Pattern.建造者模式; | ||
|
||
public class Computer { | ||
private String cpu; | ||
private String memory; | ||
private String disk; | ||
|
||
public String getCpu() { | ||
return cpu; | ||
} | ||
|
||
public void setCpu(String cpu) { | ||
this.cpu = cpu; | ||
} | ||
|
||
public String getMemory() { | ||
return memory; | ||
} | ||
|
||
public void setMemory(String memory) { | ||
this.memory = memory; | ||
} | ||
|
||
public String getDisk() { | ||
return disk; | ||
} | ||
|
||
public void setDisk(String disk) { | ||
this.disk = disk; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package Design_Pattern.建造者模式; | ||
|
||
public interface ComputerBuilder { | ||
void builderCpu(); | ||
void builderMemory(); | ||
void builderDisk(); | ||
Computer builderComputer(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package Design_Pattern.建造者模式; | ||
|
||
public class ComputerDirector { | ||
public Computer constructComputer(ComputerBuilder computerBuilder){ | ||
computerBuilder.builderMemory(); | ||
computerBuilder.builderCpu(); | ||
computerBuilder.builderDisk(); | ||
return computerBuilder.builderComputer(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package Design_Pattern.建造者模式; | ||
|
||
public class ConcreteBuilderTest { | ||
public static void main(String[] args) { | ||
ComputerDirector computerDirector=new ComputerDirector(); | ||
ComputerBuilder computerConcreteBuilder=new CumputerConcreteBuilder(); | ||
Computer computer=computerDirector.constructComputer(computerConcreteBuilder); | ||
System.out.println(computer.getCpu()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package Design_Pattern.建造者模式; | ||
|
||
public class CumputerConcreteBuilder implements ComputerBuilder { | ||
Computer computer; | ||
|
||
public CumputerConcreteBuilder() { | ||
computer=new Computer(); | ||
} | ||
|
||
@Override | ||
public void builderCpu() { | ||
computer.setCpu("8Core"); | ||
} | ||
|
||
@Override | ||
public void builderMemory() { | ||
computer.setMemory("16G"); | ||
} | ||
|
||
@Override | ||
public void builderDisk() { | ||
computer.setDisk("1TG"); | ||
} | ||
|
||
@Override | ||
public Computer builderComputer() { | ||
return computer; | ||
} | ||
} |