Java Basics - Single Column

Disclaimer: The materials used in this column are written by VIP students of Kaige Academy. Students have the right to remain anonymous and have the right to final interpretation of the article. Kaige Academy aims to promote VIP students to learn from each other based on public notes.

hashCode() generates a number as an identifier for comparison. You can use this number for comparison. There are comparison schemes for numbers and comparison schemes for equals. Strings can also be compared using hashCode.
Rewrite hashCode: write Several private properties, encapsulated with get and set

image

image

Write a test class: as long as the content is the same, the hashCode that runs out is the same, which is also a means of comparison, the comparison of two objects

image

Singleton design pattern:
Design pattern:
Design pattern is a fixed idea, it is a code skill, as long as it is an object-oriented language, design patterns can be used, such as java, PHP, c#, etc... There are a total of design patterns 23, which can be divided into three parts:
1. Creation type
2. Structural type
3. Behavioral
architecture pattern:
design pattern is called tactics, architecture pattern is called strategy, design pattern is relatively small, and architecture pattern is larger than design pattern, it is specialized Used in the architecture, structure, cache architecture, distributed architecture, etc. in our system...

Singleton (singleton):
A singleton is a single instance, which means that a class has only one object. That is to say, I have to control this class to produce only one object, not multiple objects.
Singleton is divided into two modes:
1. Lazy-style
Advantage: no resource loading in advance
Disadvantage:
slow temper, that is, I use it whenever when to create an object

Apply for an attribute, but it is not assigned, and then when calling the method to get the object, judge whether the object is assigned. If there is no assignment, I will give you a new one, put it on this attribute, and then feed it back and add a synchronization keyword. It means synchronized, with this keyword you have to queue up access

image

2. Hungry style
Advantages: fast access
Disadvantages: Occupying resources in advance and
being impatient, that is to say, create your object from the
beginning First write a private constructor to prevent others from new objects, and then write a static object attribute, and then write a static method to feed back this method, which is the simplest design pattern

image

If you want to get this object, you can only get it through this static method

image

Create your own mode:

image

The usefulness of a singleton is:
1. The system maintains an object
2. The object of a class is accessed many times, but the related data value is not recorded

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326526334&siteId=291194637