Home  >  Article  >  Java  >  Common design patterns in Java

Common design patterns in Java

伊谢尔伦
伊谢尔伦Original
2016-12-05 13:44:021101browse

Java has a total of 23 common design patterns. They can be mainly divided into 3 categories, creation type, structural type and behavioral type.
Creative design patterns include:

1. Singleton, singleton mode: ensures that a class has only one instance and provides a unique global access point.

2. Abstract Factory: Provides an interface for creating a series of related or interdependent objects without specifying their concrete classes.

3. Factory Method: Define an interface for creating objects. Factory Method delays the instantiation of a class to a subclass.

4. Builder, construction mode: Separate the construction and representation of a complex class, so that classes with the same construction process can have different representations.

5. Prototype, prototype mode: Use prototype instances to specify the type of object to be created, and create new objects by copying the prototype.

Behavioral design patterns include:

6. Itrator, iterator pattern: Provides a method to sequentially traverse the internal elements of an aggregate object without exposing the internal representation of the object.

7. Observer, Observer pattern: Define a one-to-many relationship between objects. When an object changes, the objects that depend on it will be notified and updated accordingly.

8. Template Method: Define the skeleton of the algorithm in an operation and defer some steps to subclasses. Template Method allows subclasses to change specific steps in the algorithm without changing the skeleton of the algorithm.

9. Command, command mode: Encapsulate a request as an object, allowing you to parameterize users with different requests, queue and record request logs, and support revocable operations.

10. State, state mode: allows an object to change its behavior when its state changes. The object looks like it has changed its class.

11. Strategy, Strategy Pattern: Define a series of algorithms and encapsulate them one by one so that they can be replaced with each other. This pattern can make the algorithm independent of the customers who use it.

12. China of Responsibility, chain of responsibility model: Give multiple objects the opportunity to process requests and avoid the coupling relationship between the requester and the processor.

13. Mediator, mediation mode: Use a mediator object to encapsulate the interaction of some objects.

14. Visitor mode: Represents an operation that acts on each element in an object structure. It allows you to define new operations that act on this element without changing the class of each element.

15. Interpreter, interpreter mode: Given a language, define a grammatical representation of it, and define an interpreter that uses the representation to interpret sentences in the language.

16. Memento, memo mode: Capture the internal state of the object and save this state outside the object without destroying the object.

Structural design patterns include:

17. Composite, combination mode: combine objects into a tree structure to represent the relationship between parts and the whole, so that users can use single objects and combined objects consistently.

18. Facade, appearance mode: Provide a consistent interface for a set of structures in the subsystem. Facade provides this high-level interface, which makes the system easier to use.

19. Proxy, proxy mode: Provide a proxy for other objects to control access to this object.

20. Adapter, adapter mode: Convert a type of interface into a type of interface that the user wants. Adapter enables classes that were originally unable to work together due to incompatible interfaces to work together.

21. Decorator, decoration mode: Dynamically add some additional responsibilities to the object. In terms of added functions, Decorator is more flexible than its method of generating subclasses.

Twenty-two, Bridge, bridge mode: Separate the abstract part from its implementation part so that they can change independently.

23. Flyweight, Flyweight mode: It uses shared objects to reduce memory usage as much as possible and share information with as many similar objects as possible.


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn