Skip to content

Commit

Permalink
add code
Browse files Browse the repository at this point in the history
  • Loading branch information
VentureQ committed Jul 8, 2021
1 parent 695fff2 commit e561fa8
Show file tree
Hide file tree
Showing 10 changed files with 162 additions and 0 deletions.
26 changes: 26 additions & 0 deletions code/Design_Pattern/单例模式/DoubleCheckedLocking.java
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();
}
}
}
10 changes: 10 additions & 0 deletions code/Design_Pattern/单例模式/HungrySingleton.java
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;
}
}
13 changes: 13 additions & 0 deletions code/Design_Pattern/单例模式/SafeLazyInitialization.java
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;
}
}
10 changes: 10 additions & 0 deletions code/Design_Pattern/单例模式/Static_Class_Singleton.java
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;
}
}
15 changes: 15 additions & 0 deletions code/Design_Pattern/单例模式/UnsafeLazyInitialization.java
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;
}

}
31 changes: 31 additions & 0 deletions code/Design_Pattern/建造者模式/Computer.java
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;
}
}
8 changes: 8 additions & 0 deletions code/Design_Pattern/建造者模式/ComputerBuilder.java
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();
}
10 changes: 10 additions & 0 deletions code/Design_Pattern/建造者模式/ComputerDirector.java
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();
}
}
10 changes: 10 additions & 0 deletions code/Design_Pattern/建造者模式/ConcreteBuilderTest.java
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());
}
}
29 changes: 29 additions & 0 deletions code/Design_Pattern/建造者模式/CumputerConcreteBuilder.java
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;
}
}

0 comments on commit e561fa8

Please sign in to comment.