Introduction to design patterns

Introduction to design patterns

Design patterns represent best practices and are often used by experienced object-oriented software developers. Design patterns are solutions to the general problems that software developers face during software development. These solutions are summarized by many software developers after a long period of trial and error.

The design pattern is a collection of re-used, mostly known, cataloged, and code-designed experiences. The design pattern is used to reuse code, make the code easier to understand, and ensure code reliability. There is no doubt that the design pattern is win-win in others. The design pattern makes the code compilation truly engineering. The design pattern is the cornerstone of software engineering, just like the bricks of the building. The rational use of design patterns in the project can solve many problems perfectly. Each mode has corresponding principles in reality, and each mode describes a problem that is repeated around us and the problem. The core solution, which is why design patterns can be widely used.

What is GOF (Gang of Four)?

In 1994, Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides co-authored a book called Design Patterns - Elements of Reusable Object-Oriented Software (Chinese translation: Design Patterns - Reusable Object-Oriented Software) Elemental book, the book first mentioned the concept of design patterns in software development.

The four authors collectively call the GOF (Gang of Four) . The design patterns they propose are based primarily on the following object-oriented design principles.

  • Program the interface instead of the implementation.
  • Use object combinations first instead of inheritance.

Use of design patterns

The two main uses of design patterns in software development.

Developer's common platform

The design pattern provides a standard terminology system that is specific to a particular scenario. For example, the singleton design pattern means that a single object is used so that all developers familiar with the singleton design pattern can use a single object and can tell each other in this way that the program uses the singleton pattern.

Best practice

Design patterns have evolved over a long period of time, providing the best solution to the general problems faced in the software development process. Learning these patterns helps inexperienced developers learn software design in a quick and easy way.

Type of design pattern

According to the design pattern reference book Design Patterns - Elements of Reusable Object-Oriented Software , there are a total of 23 design patterns. These patterns can be divided into three categories: Creative Patterns, Structural Patterns, and Behavior Patterns. Of course, we will also discuss another type of design pattern: the J2EE design pattern.
Serial numberr mode description
1 Created mode These design patterns provide a way to hide the creation logic while creating the object, rather than instantiating the object directly with the new operator. This makes the program more flexible in determining which objects need to be created for a given instance. Factory Pattern Abstract Factory Pattern Singleton Pattern Builder Pattern Prototype Pattern
2 Structural mode These design patterns focus on the combination of classes and objects. The concept of inheritance is used to combine interfaces and define ways in which composite objects get new functionality. Adapter Pattern Bridge Pattern Filter mode (Filter, Criteria Pattern) Composite Pattern Decorator Pattern Facade Pattern Flyweight Pattern Proxy Pattern
3 Behavioral model These design patterns pay special attention to communication between objects. Chain of Responsibility Pattern Command pattern Interpreter Pattern Iterator Pattern Mediator Pattern Memo mode Observer pattern State pattern Null Object Pattern Strategy Pattern Template Pattern Visitor pattern
4 J2EE mode These design patterns pay special attention to the presentation layer. These patterns are certified by Sun Java Center. MVC Pattern (MVC Pattern) Business Delegate Pattern Composite Entity Pattern Data Access Object Pattern Front Controller Pattern Intercepting Filter Pattern Service Locator Pattern Transfer Object Pattern

The six principles of design patterns

1, Open Close Principle

The principle of opening and closing means: open to extension, close to modification. When the program needs to be expanded, you can't modify the original code to achieve a hot plug effect. In short, it is to make the program expandable, easy to maintain and upgrade. To achieve this, we need to use interfaces and abstract classes, which we will mention later in the specific design.

2, Liskov Substitution Principle

The principle of Lie replacement is one of the basic principles of object-oriented design. The principle of the replacement of the Richter said that where any base class can appear, subclasses must appear. LSP is the cornerstone of inheritance multiplexing. Only when a 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 based on the base class. behavior. The principle of replacement of the Richter is to supplement the principle of opening and closing. The key step in the realization of the principle of opening and closing is abstraction, and the inheritance relationship between the base class and the subclass is the concrete realization of the abstraction, so the principle of the replacement of the Richter is the specification of the concrete steps to achieve the abstraction.

3, Dependence Inversion Principle

This principle is the basis of the principle of opening and closing. The specific content: for interface programming, depends on abstraction and not on concrete.

4. Interface Segregation Principle

This principle means that using multiple isolated interfaces is better than using a single interface. It also has another meaning: to reduce the degree of coupling between classes. It can be seen that the design pattern is a software design idea that starts from a large software architecture and is easy to upgrade and maintain. It emphasizes reducing dependencies and reducing coupling.

5. Dimitt's Law, also known as the Demeter Principle

The least known principle means that an entity should interact as little as possible with other entities, making the system functional modules relatively independent.

6.Composite Reuse Principle

The principle of synthetic reuse means: try to use synthesis/aggregation instead of inheritance.

javacodegeeks is optimized for learning.© javacodegeeks .
All Right Reserved and you agree to have read and accepted our term and condition