Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the basics of java design patterns

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "what are the basics of java design patterns". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1. Singleton mode

Singleton model: divided into hungry Chinese style and lazy Chinese style

Hungry Han style:

Public class Singleton {

Private static Singleton instance = new Singleton ()

Private Singleton () {}

Public static Singleton getInstance () {

Return instance

}

}

Lazy style:

Public class Singleton02 {

Private static Singleton02 instance

Private Singleton02 () {}

Public static Singleton02 getInstance () {

If (instance = = null) {

Instance = new Singleton02 ()

}

Return instance

}

}

/ / A thread-safe singleton that uses synchronous code blocks to lock the creation of an instance so that it is initialized only once in a thread

/ / (use double check lock to process)

Public class Singleton02 {

Private static Singleton02 instance

Private Singleton02 () {}

Public static Singleton02 getInstance () {

If (instance = = null) {

Synchronized (Singleton02.class)

If (instance = = null) {

Instance = new Singleton02 ()

}

}

}

Return instance

}

}

2 、 MVP,MVC,MVVM

   MVP mode corresponds to Model- business logic and entity model, view- corresponds to activity, responsible for drawing View and interaction with users, Presenter- is responsible for interaction between View and Model, MVP mode is based on MVC mode, complete separation of Model and View makes the project less coupled, activity in project in Mvc corresponds to C-Controllor in mvc, and logical processing in project is processed in this C At the same time, the interaction between View and Model, that is, all logical interactions and user interactions in mvc are placed in Controllor, that is, activity. View and model can communicate directly. The MVP mode is more thoroughly separated, the division of labor is more clear Model- business logic and entity model, view- is responsible for interaction with users, Presenter is responsible for completing the interaction between View and Model, the biggest difference between MVP and MVC is that Model and View are allowed to interact in MVC, while in MVP, it is obvious that the interaction between Model and View is completed by Presenter. Another point is that the interaction between Presenter and View is through the interface.

3. Factory model Wuxi Gynecology Hospital ranks http://www.0510bhyy.com/.

(1)。 Simple factory model

Definition of    simple factory: provides the ability to create an instance of an object, regardless of its implementation. The type of the instance to be created can be an interface, an abstract class, or a concrete class (factory is an interface or abstract class, and the instance is a class that inherits the interface or abstract class)

(2)。 Factory method

   means that the base class interface or abstract class in the simple factory pattern is used as the return value of the method of the factory, and then the instance inherits the factory class.

(3)。 Abstract factory

   means that the factory class is an abstract class, and then there must be a default subclass to inherit the factory, implementing its factory method to return a default data

4. Observer mode

   Observer pattern is also known as publish / subscribe (Publish/Subscribe) pattern.

The    observer design pattern involves two roles: theme (Subject) and observer (Observer).

The observer pattern in    Java requires the observer to inherit the java.util.Observable class and the observer to implement the java.util.Observer interface. There is a update method in the Observer interface, which is automatically called when some position of the observer changes.

5. Builder mode builder

The    creator pattern, also known as the builder pattern, separates the construction of a complex object from its representation, so that the same construction process can create different representations. The creator pattern hides the creation process of complex objects, abstracts the creation process of complex objects, and dynamically creates objects with compound attributes by subclass inheritance or overloading.

6. Decorator mode

The    decorator pattern, as its name implies, is to extend some functionality by decorating existing classes. Its structure is as follows:

   Component is a unified interface, and it is also the basic type of decoration class and decorated class.

   ConcreteComponent is a concrete implementation class, but also a decorated class, it is a complete class with some functions.

   Decorator is a decoration class that implements the Component interface while maintaining an instance of ConcreteComponent internally, which can be initialized through a constructor. Decorator itself, usually implemented by default, exists only as a declaration: I'm going to produce some subclasses for decoration. And its subcategory is the category of decorative products with specific decorative effects.

   ConcreteDecorator is a specific category of decorative products, each decorative product has a specific decorative effect. It can be decorated by declaring which type of ConcreteComponent to decorate through the constructor.

This is the end of the introduction to "what are the basics of java design patterns". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report