Home Java javaTutorial 24 design patterns and 7 principles in Java

24 design patterns and 7 principles in Java

Dec 12, 2016 pm 01:33 PM
java design patterns

1. Creation pattern

1. Abstract factory pattern: Provides an interface for creating a family of related or dependent objects without specifying a specific class.
2. Builder pattern (Builder pattern) : Use the generator pattern to encapsulate the construction process of a product and allow step-by-step construction. Separate the construction of a complex object from its representation so that the same construction process can create different representations.
3. Factory method pattern ): Defines an interface for creating objects, but it is up to the subclass to decide which class to instantiate. The factory method allows the class to defer instantiation to the subclass.
4. Prototype pattern: When creating a given When the instance process of a class is expensive or complex, use the prototype pattern.
5. Singleton pattern: Ensure that a class has only one instance and provide a global access point.
6. Multiple instance pattern (Multition pattern) ): Combine two or more patterns in one solution to solve general or recurring problems.

2. Structural pattern

1. Adapter pattern (Adapter pattern): Convert the interface of a class into Another interface that customers expect. Adapters allow classes with incompatible interfaces to work together seamlessly. Object adapters use composition, and class adapters use multiple inheritance.
2. Bridge pattern (Bridge pattern): Use the bridge pattern by placing implementation and abstraction in Two different class hierarchies so that they can be changed independently.
3. Composite pattern: Allows you to combine objects into a tree structure to represent the "whole/part" hierarchy. Combination allows customers to create a consistent Ways to handle individual objects and combinations of objects.
4. Decorator pattern: Dynamically attach responsibilities to objects. To extend functionality, decorators provide a more flexible alternative than inheritance.
5. Appearance Pattern (facade pattern): Provides a unified interface for accessing a group of interfaces in the subsystem. The facade defines a high-level interface to make the subsystem easier to use.
6. Flyweight Pattern: If you want To allow one instance of a class to provide many "virtual instances", use the fly volume pattern.
7. Proxy pattern (Proxy pattern): Provide a substitute or placeholder for another object to control the access to this object. Visit.

3. Behavioral pattern

1. Chain of responsibility pattern: Through the chain of responsibility pattern, you can create an object chain for a request. Each object checks the request in sequence and responds to it Process it or pass it to the next object in the chain.
2. Command pattern: Enclose "request" into an object to parameterize other objects with different requests, queues or logs. Command pattern also Supports reversible operations.
3. Interpreter pattern (Interpreter pattern): Use the interpreter pattern to create an interpreter for the language.
4. Iterator pattern (iterator pattern): Provides a method to sequentially access each item in an aggregate object element without exposing its internal representation.
5. Mediator pattern: Use the Mediator pattern to centralize complex communication and control methods between related objects.
6. Memento pattern: When When you need to return an object to its previous state (for example, your user requests "undo"), you use the memo pattern.
7. Observer pattern (observer pattern): Define one-to-many dependencies between objects, so that Come, when an object changes state, objects that depend on it will be notified and automatically updated.
8. State pattern (State pattern): allows the object to change its behavior when the internal state changes, and the object will appear to have changed. Its class.
9. Strategy pattern: defines a family of algorithms and seals them separately so that they can be replaced with each other. This pattern makes the changes of the algorithm independent of the customers who use the algorithm.
10. Template method pattern (Template pattern): Define the skeleton of an algorithm in a method, and defer some steps to subclasses. The template method allows subclasses to redefine certain steps in the algorithm without changing the algorithm structure.
11. Visitor pattern: When you want to add new capabilities to a combination of objects, and encapsulation is not important, use the visitor pattern.


---------- -------------------------------------------------- ----------------------------------------

Seven design principles:

1. Single responsibility principle [SINGLE RESPONSIBILITY PRINCIPLE]: One class is responsible for one responsibility.
2. LISKOV SUBSTITUTION PRINCIPLE: The rules of inheritance and derivation.
3. Dependency inversion principle [DEPENDENCE INVERSION PRINCIPLE]: High-level modules should not depend on low-level modules, both should depend on their abstractions; abstractions should not depend on details; details should depend on abstractions. That is, program for the interface, not for the implementation.
4. Interface segregation principle [INTERFACE SEGREGATION PRINCIPLE]: Establish a single interface, do not build a huge and bloated interface, try to refine the interface, and have as few methods as possible in the interface.
5. Dimi Special rule [LOW OF DEMETER]: low coupling, high cohesion.
6. OPEN CLOSE PRINCIPLE: A software entity such as a class, module, and function should be open for extension and closed for modification.
7. Combination/ Aggregation reuse principle [Composition/Aggregation Reuse Principle (CARP)]: Try to use combination and aggregation and use less inheritance relationships to achieve the principle of reuse.


Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Apr 19, 2025 pm 04:51 PM

Troubleshooting and solutions to the company's security software that causes some applications to not function properly. Many companies will deploy security software in order to ensure internal network security. ...

How to simplify field mapping issues in system docking using MapStruct? How to simplify field mapping issues in system docking using MapStruct? Apr 19, 2025 pm 06:21 PM

Field mapping processing in system docking often encounters a difficult problem when performing system docking: how to effectively map the interface fields of system A...

How to elegantly obtain entity class variable names to build database query conditions? How to elegantly obtain entity class variable names to build database query conditions? Apr 19, 2025 pm 11:42 PM

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

How do I convert names to numbers to implement sorting and maintain consistency in groups? How do I convert names to numbers to implement sorting and maintain consistency in groups? Apr 19, 2025 pm 11:30 PM

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log? How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log? Apr 19, 2025 pm 11:45 PM

Start Spring using IntelliJIDEAUltimate version...

How to safely convert Java objects to arrays? How to safely convert Java objects to arrays? Apr 19, 2025 pm 11:33 PM

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

How to elegantly get entity class variable name building query conditions when using TKMyBatis for database query? How to elegantly get entity class variable name building query conditions when using TKMyBatis for database query? Apr 19, 2025 pm 09:51 PM

When using TKMyBatis for database queries, how to gracefully get entity class variable names to build query conditions is a common problem. This article will pin...

E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products? E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products? Apr 19, 2025 pm 11:27 PM

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

See all articles