Java novice learning guide [day21] --- unit testing, design patterns

1. Unit testing

Testing is to check whether a piece of code or a function is correct

Note: 1. The test class is named XxxTest{}

2. The test method is TestXxx(){}

3. The test method has no parameters and no return value, and the method must be public

4. You need to guide the package for things that require a third-party Junit (not provided by the JDK)

5. Need to be annotated with @Test, there may also be @Before, @After, which will run with Test, not run separately, before or after Test

2. Packing and guiding the package

Be sure to add static when packaging the method. If there is no static, you need to instantiate the package and then create the object and call it.

Packaging: select the corresponding tool class, right-click –>Export–>select the java directory, JAR File–>Next–>select the export path–>Finish

Guide package: First copy the packaged files into the newly created folder under the new project (usually create a new folder as lib), then right-click the jar package, build path–>add to build path

A red exclamation mark appears in the project: it mostly appears in projects imported into others

Features: Once the project has a red exclamation mark, the project will not compile (no error is allowed)

Reason: The project is referencing a jar package that does not exist

3、properties

Properties is the implementation class under the map interface, directly inheriting the child Hashtable, so it also exists in the form of key-value pairs, and can only store elements of string type

For the analysis of resource files (mainly to solve hard-coded problems), there are three ways:

Traditional IO method (the file address is hard-coded, not recommended), bytecode object acquisition stream (type.cless, through Class needs to ensure that the bytecode file is in the agreed directory, not recommended), class loader acquisition stream

4. Design pattern

Singleton mode:

There is only one object globally, saving space

Involved methods: 1, the privatization of the construction method; 2, provide a static method to return the object of the current class

① Hungry man mode: create objects when the class is loaded, thread-safe, but not efficient (using static code blocks)

②Lazy man mode: it is created only when it is called for the first time. If the thread is not safe, you can use the synchronized code block synchronized + double verification to solve the thread safety problem

Factory mode:

Produce objects through the factory and construct objects based on the passed parameters

Guess you like

Origin blog.csdn.net/WLK0423/article/details/109734485