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 design patterns of java

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article introduces the knowledge of "what are the design patterns of java". In the operation of actual cases, many people will encounter such a dilemma. Then 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 pattern is a commonly used software design pattern.

There is only one special class called singleton class in its core structure. Through the singleton mode, we can ensure that there is only one instance of a class in the system and the instance is easy to be accessed by the outside world, so as to facilitate the control of the number of instances and save system resources.

Application scenario: if you want only one object of a class to exist in the system, singleton mode is the best solution.

2) factory mode.

The factory pattern mainly provides an interface for creating objects.

The application scenarios are as follows:

It is impossible to foresee what kind of instance needs to be created when coding.

B. the system should not rely on the details of how product class instances are created, combined, and expressed.

3) Strategy mode.

Strategy pattern: algorithm families are defined and encapsulated so that they can be replaced with each other. This mode makes the change of the algorithm independent of the customer who uses the algorithm.

The application scenario is as follows.

A, one thing, there are many solutions can be achieved.

B. I can decide which implementation to use at any time.

C. more plans may be added in the future.

D, the strategy mode makes sure that the change of the scheme will not affect the customers who use the scheme.

An example business scenario is as follows.

The operation of the system must be logged, and the log is usually recorded in the database to facilitate subsequent management, but errors may occur when recording the log to the database, such as temporarily unable to connect to the database. Then record it in the file first. There are two algorithms for log writing to the database and the file, but the caller does not care and is only responsible for writing.

4) Observer mode.

The Observer pattern, also known as the publish / subscribe pattern, defines one-to-many dependencies between objects. When an object changes its state, all its dependents are notified and updated automatically.

The application scenarios are as follows:

A. updating the state of one object requires synchronous updates of other objects, and the number of other objects is dynamically variable.

B. objects only need to notify other objects of their updates and do not need to know the details of other objects.

5) iterator mode.

The iterator pattern provides a way to access elements in an aggregated object sequentially without exposing the internal representation of the object.

The application scenarios are as follows:

When you need to access an aggregate object and need to traverse no matter what those objects are, you should consider using the iterator pattern. The stl container is actually a good example of the iterator pattern.

6) template method mode.

The template method pattern defines the skeleton of an algorithm in operation and delays some steps to the subclass. the template method enables the subclass to redefine some of the steps of the algorithm without changing the structure of the algorithm.

The application scenarios are as follows:

For some functions, different functions are shown on different objects, but the framework of the function is the same.

This is the end of the content of "what are the design patterns of java"? 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

Internet Technology

Wechat

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

12
Report