Home Java JavaBase Briefly describe the concept of design patterns

Briefly describe the concept of design patterns

Apr 22, 2020 am 10:08 AM
Design Patterns

Briefly describe the concept of design patterns

What is a design pattern

A design pattern is a set of code design experiences that are used repeatedly, known to most people, and cataloged. In summary, it has nothing to do with specific language, it is a kind of thought.

Only by mastering object-oriented thinking can we better understand design patterns, and vice versa.

Design patterns are good programming methods summarized by programmers during the practice of software engineering.

There are 23 design patterns in total.

The essence of these 23 design patterns is the practical application of object-oriented design principles, and a full understanding of the encapsulation, inheritance and polymorphism of classes, as well as the association and combination relationships of classes.

(Recommended related video tutorials: java video)

Classification of design patterns

1. Creational pattern

Creative patterns (5 types): singleton pattern, factory method pattern, abstract factory pattern, builder pattern, prototype pattern.

2. Structural mode

Structural mode (7 types): adapter mode, decorator mode, proxy mode, appearance mode, bridge mode, combination mode, flyweight mode.

3. Behavioral patterns

Behavioral patterns (11 types): strategy pattern, template method pattern, observer pattern, iterative sub-pattern, chain of responsibility pattern, command pattern, memo pattern, State pattern, visitor pattern, mediator pattern, interpreter pattern.

Six principles of design patterns

General principles: Opening and closing principle

Open to extensions, closed to modifications. When the program needs to be expanded, the original code cannot be modified, but the original code must be expanded to achieve a hot-swappable effect. So the summary in one sentence is: in order to make the program scalable and easy to maintain and upgrade.

To achieve this effect, we need to use interfaces and abstract classes, etc. We will mention this in the specific design later.

1. Single Responsibility Principle

Do not have more than one reason for class changes, that is to say, each class should implement a single responsibility, otherwise the class should be split.

2. Liskov Substitution Principle

Anywhere a base class can appear, a subclass can definitely appear. The Liskov substitution principle is the cornerstone of inheritance reuse. Only when the derived class can replace the base class and the function of the software unit is not affected, the base class can be truly reused, and the derived class can also add new ones on the basis of the base class. the behavior of.

The Richter substitution principle is a supplement to the "open-closed" principle. The key step in realizing the "open-closed" principle is abstraction. The inheritance relationship between base classes and subclasses is the specific implementation of abstraction, so the Liskov substitution principle is a specification for the specific steps to achieve abstraction. In the Liskov substitution principle, subclasses should try not to overwrite or overload methods of the parent class. Because the parent class represents a defined structure and interacts with the outside world through this standardized interface, subclasses should not destroy it casually.

3. Dependence Inversion Principle

Interface-oriented programming relies on abstraction rather than concreteness. When a concrete class is used when writing code, it does not interact with the concrete class, but with the upper interface of the concrete class.

4. Interface Segregation Principle

There are no methods in each interface that are not used by subclasses but must be implemented. If not, the interface must be split. It is better to use multiple isolated interfaces than to use a single interface (multiple interface methods combined into one interface).

5. Demeter Principle (Demeter Principle)

The less a class knows about the classes it depends on, the better. No matter how complex the dependent class is, the logic should be encapsulated inside the method and provided to the outside through the public method. In this way, when the dependent class changes, the impact on the class will be minimal.

Another expression of the least-known principle is: only communicate with direct friends. As long as there is a coupling relationship between classes, it is called a friend relationship. Coupling is divided into dependence, association, aggregation, combination, etc. We call classes that appear in member variables, method parameters, and method return values ​​direct friends. Local variables and temporary variables are not direct friends. We require that unfamiliar classes do not appear as local variables in the class.

6. Composite Reuse Principle

Try to use synthesis/aggregation first instead of inheritance.

Recommended tutorial: Getting started with java

The above is the detailed content of Briefly describe the concept of design patterns. For more information, please follow other related articles on the PHP Chinese website!

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)

The difference between design patterns and architectural patterns in Java framework The difference between design patterns and architectural patterns in Java framework Jun 02, 2024 pm 12:59 PM

In the Java framework, the difference between design patterns and architectural patterns is that design patterns define abstract solutions to common problems in software design, focusing on the interaction between classes and objects, such as factory patterns. Architectural patterns define the relationship between system structures and modules, focusing on the organization and interaction of system components, such as layered architecture.

Analysis of the Decorator Pattern in Java Design Patterns Analysis of the Decorator Pattern in Java Design Patterns May 09, 2024 pm 03:12 PM

The decorator pattern is a structural design pattern that allows dynamic addition of object functionality without modifying the original class. It is implemented through the collaboration of abstract components, concrete components, abstract decorators and concrete decorators, and can flexibly expand class functions to meet changing needs. In this example, milk and mocha decorators are added to Espresso for a total price of $2.29, demonstrating the power of the decorator pattern in dynamically modifying the behavior of objects.

PHP design pattern practical case analysis PHP design pattern practical case analysis May 08, 2024 am 08:09 AM

1. Factory pattern: Separate object creation and business logic, and create objects of specified types through factory classes. 2. Observer pattern: allows subject objects to notify observer objects of their state changes, achieving loose coupling and observer pattern.

How design patterns deal with code maintenance challenges How design patterns deal with code maintenance challenges May 09, 2024 pm 12:45 PM

Design patterns solve code maintenance challenges by providing reusable and extensible solutions: Observer Pattern: Allows objects to subscribe to events and receive notifications when they occur. Factory Pattern: Provides a centralized way to create objects without relying on concrete classes. Singleton pattern: ensures that a class has only one instance, which is used to create globally accessible objects.

The wonderful use of the adapter pattern in Java design patterns The wonderful use of the adapter pattern in Java design patterns May 09, 2024 pm 12:54 PM

The Adapter pattern is a structural design pattern that allows incompatible objects to work together. It converts one interface into another so that the objects can interact smoothly. The object adapter implements the adapter pattern by creating an adapter object containing the adapted object and implementing the target interface. In a practical case, through the adapter mode, the client (such as MediaPlayer) can play advanced format media (such as VLC), although it itself only supports ordinary media formats (such as MP3).

PHP Design Patterns: Test Driven Development in Practice PHP Design Patterns: Test Driven Development in Practice Jun 03, 2024 pm 02:14 PM

TDD is used to write high-quality PHP code. The steps include: writing test cases, describing the expected functionality and making them fail. Write code so that only the test cases pass without excessive optimization or detailed design. After the test cases pass, optimize and refactor the code to improve readability, maintainability, and scalability.

Application of design patterns in Guice framework Application of design patterns in Guice framework Jun 02, 2024 pm 10:49 PM

The Guice framework applies a number of design patterns, including: Singleton pattern: ensuring that a class has only one instance through the @Singleton annotation. Factory method pattern: Create a factory method through the @Provides annotation and obtain the object instance during dependency injection. Strategy mode: Encapsulate the algorithm into different strategy classes and specify the specific strategy through the @Named annotation.

What are the advantages and disadvantages of using design patterns in java framework? What are the advantages and disadvantages of using design patterns in java framework? Jun 01, 2024 pm 02:13 PM

The advantages of using design patterns in Java frameworks include: enhanced code readability, maintainability, and scalability. Disadvantages include complexity, performance overhead, and steep learning curve due to overuse. Practical case: Proxy mode is used to lazy load objects. Use design patterns wisely to take advantage of their advantages and minimize their disadvantages.

See all articles