Design patterns in Java:
Strategy pattern, proxy pattern, singleton pattern, multiple instance pattern, factory method pattern, abstract factory Pattern, facade pattern, adapter pattern, template method pattern, builder pattern, bridge pattern, command pattern, decorator pattern, iterator pattern, composition pattern, observer pattern, chain of responsibility pattern, visitor pattern, state pattern, prototype pattern, Mediator mode, interpreter mode, Hengyuan mode, memo mode.
Example:
Single case mode
The so-called singleton design means that a class only allows one instantiated object to be generated. The best understood design pattern is divided into lazy man style and hungry man style.
Hungry Chinese style: The construction method is privatized. New instantiated objects cannot be generated externally. The instantiated objects can only be obtained through static methods.
class Singleton { /** * 在类的内部可以访问私有结构,所以可以在类的内部产生实例化对象 */ private static Singleton instance = new Singleton(); /** * private 声明构造 */ private Singleton() { } /** * 返回对象实例 */ public static Singleton getInstance() { return instance; } public void print() { System.out.println("Hello Singleton..."); } }
Lazy Chinese style: When using Singleton for the first time The operation of instantiating the object will only be generated when the object is created.
class Singleton { /** * 声明变量 */ private static volatile Singleton singleton = null; /** * 私有构造方法 */ private Singleton() { } /** * 提供对外方法 * @return */ public static Singleton getInstance() { // 还未实例化 if (singleton == null) { synchronized (Singleton.class) { if (singleton == null) { singleton = new Singleton(); } } } return singleton; } public void print() { System.out.println("Hello World"); } }
Recommended tutorial: Java tutorial
The above is the detailed content of What are the design patterns in Java?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SublimeText3 Chinese version
Chinese version, very easy to use

SublimeText3 Mac version
God-level code editing software (SublimeText3)
