Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Java single-case design patterns ---- lazy and hungry


May 30, 2021 Article blog


Table of contents


About the design pattern, this is actually a separate thing, it does not belong to Java, also does not belong to any language unique, but in Java used more, so today's small editor to introduce you to a single design mode of the hungry and lazy two simple hit workers. Recommended lessons: Java multithreaded instruction, in-depth analysis of Java object-oriented.

 Java single-case design patterns ---- lazy and hungry1

First, let's look at the definition of a single-case pattern: a single-case design pattern is designed to make an object of a class the only instance in the system.

Again, the definition of a single-case pattern in Java: a class has and has only one instance and instantiates itself to the entire system.

So, the difference between the so-called "lazy" and "hungry Chinese" is that the time to establish a single-case object is different.

"Lazy": is when you really use to build this single-case object;

"Hungry Chinese": Whether you use it or not, start by creating this single-case object.

First, hungry Chinese style

Hungry Chinese style: in layman's terms, this hit worker is very positive about dry rice, whether hungry or not hungry is like starving ghosts, the old early dry rice tools ready (new object), ready to dry rice.

On the code:

class Hungry_people{

private int id;

private String name;

private Hungry_people(){

} // Constructor privatization

private static Hungry_people Instance=new Hungry_people();

// Internal creation of the object

public static Hungry_people getInstance(){

return Instance;

}

/ / Provide a common static method and return to an object.

}

public class Test1 {

public static void main(String[] args) {

Hungry_people hungry1 = Hungry_people.getInstance();

Hungry_people hungry2 = Hungry_people.getInstance();

}

}

Second, lazy style

Lazy style: in layman's terms, this hit worker is not at all concerned about dry rice, dishes and chopsticks dirty also ignore, when to dry rice to wash dishes chopsticks (new object).

On the code:

class Lazy_people {

private Lazy_people(){

}

private static Lazy_people Instance = null;

// Statement the current object does not have an initialization value

public static Lazy_people getInstance(){

if (Instance == null){

Instance=new Lazy_people ();

/ / Need to renovate the object, don't never have a new object.

}

return Instance;

}

}

public class Test2 {

public static void main(String[] args) {

Lazy_people lazy1 = Lazy_people.getInstance();

}

}

Third, what's the difference between hungry and lazy?

For hungry Chinese:

Pros: Thread thief security

Cons: Objects take too long to load

For lazy:

Pros: Delay the creation of objects faster

Cons: Multithreaded security thieves are low, but can be optimized to make them available.

Four, common interview questions

1. What's the difference between lazy and hungry?

A: Lazy is characterized by lazy loading of instances.

2. Is there a problem with lazy lazy lazy loading?

A: There is a security issue when you encounter multithreaded access.

3. How can I resolve this issue?

A: You can add synchronization to resolve.

4. How do you increase synchronization?

A: It's okay to sync or sync blocks of code, but the synchronization method is a little inefficient.

5. Which lock is used when adding synchronization?

A: The bytecode file object to which the class belongs.