What is a design pattern
In the world of software development, problems in many areas have similar characteristics. Like building a house, whether to build a shopping center, or to build a holiday hotel, there are basically similar steps between the work, all need to build beam and column, paving the roof and so on. In doing these tasks, we need to follow some special technical requirements to make the structure and affordability of the house reasonable, and these rules are the result of the accurate calculation and failure of the predecessors. For software development, too, if we want to write robust, flexible applications, it's also necessary to do elaborate design, and we can do that by following some kind of rule. These rules or techniques are called design patterns.
There is a classic book in the field of design patterns: "Design patterns:elements of reusable object-oriented Software" (That is, "designing patterns"), by Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides co-authored (addison-wesley,1995). These authors are often referred to as the "group of Four" (Gang of Four), and this book is also known as the "four-person group (or GoF)" book. This book introduces 23 design patterns, with the continuous development of technology, new design patterns are constantly found, but these 23 design patterns are the most worth learning and mastering. The design pattern in C # design mode is divided into the following 5 categories:
Interface type mode
Duty mode
Stereotype pattern
Operation mode
Extended mode
Patterns included in each category:
| Category |
Included Design patterns |
| Interface type |
Adapter mode, appearance mode, compositing mode, bridging mode |
| Duty type |
Single-piece mode, observer mode, intermediary mode, agent mode, responsibility chain mode, and element mode |
| Structural type |
Generator mode, Factory mode, abstract Factory mode, prototype mode, Memo mode |
| Operation type |
Template method mode, state mode, policy mode, command mode, interpreter mode |
| Extended Type |
Decoration mode, iterator mode, visitor mode |
First day Adapter mode
I. Real-world problems
During project development, calls between classes are often Guandi, with the following two possible scenarios:
1. We have defined a class invocation relationship, for example, a class needs to implement a specific function by invoking a class that implements an interface, and the Third-party class has already been implemented, and the method signature of a Third-party class may not be consistent with the customer's expected signature.
2. The methods provided by these classes may only accomplish most of the functions we need, and we need to perform a little extra action on this basis;
Two. The solution
Both of these problems are actually calls to the service classes that provide functionality, and there are several solutions:
1. Instantiate the service class directly in the client class, make method calls, and make additional processing after the call as needed.
2. Create a mediation that passes the method call to the mediation, where the mediation requests the service class and performs additional operations.
In comparison, the second approach is more reasonable because it reduces the coupling of customer classes and service classes through the placement of mediation classes, and does not affect the customer class when the service class changes.
Three. Conceptual definition
For this problem, you can use adapter mode. The purpose of the adapter pattern is that if a customer needs to use a service of a class that is provided by a different interface, the adapter can provide the client with an expected interface.
There are two types of adapters: interface adapters (class adapters) and object adapters.