Design Pattern Overview
The design pattern, as its name implies, is a pattern. In object orientation, it uses classes and their methods to achieve the goal. It facilitates code duplication and readability and facilitates system maintenance. In Design pattern, 23 Design patterns are proposed for the first time. If you want to become a programming guru, you must learn Design patterns.
The 23 Design patterns described in Design pattern are listed below:
1. Adapter Mode
2. Appearance Mode
3. merging mode
4. Bridging Mode
5. Singleton Mode
6. Observer Mode
7. Redeployment Mode
8. Proxy Mode
9. Responsibility Chain Model
10. metadata sharing
11. Builder Mode
12. Factory method mode
13. Abstract Factory Model
14. prototype mode
15. Memorandum Mode
16. template method mode
17. Status Mode
18. Policy Mode
19. Command mode
20. interpreter Mode
21. decorator Mode
22. iterator Mode
23. Visitor Mode
The 23 design modes are described as follows:
I. Adapter Mode
The so-called adapter mode is to adapt a class to what the user expects. It is generally divided into two categories: Class adapter and Object Adapter.
Class Adapter
In this adapter mode, the adapter inherits its own implemented classes (generally multiple inheritance ).
Specifically, we need to implement the Target Interface and inherit from the existing class to achieve the desired effect. The specific implementation is as follows:
[Java]
Package cn.com. pattern;
// Target abstract role. You need a circular usb interface.
Public interface Target {
Public void provideCircleUsb ();
}
Package cn.com. pattern;
// Source role, which provides a square usb Interface
Public class Adaptee {
Public void provideSquareUsb (){
System. out. println ("I provide a square USB interface ");
}
}
Package cn.com. pattern;
// Adapt to the role. Here is the converter that the boss bought for us.
Public class Adapter extends Adaptee implements Target {
@ Override
// The converter converts the circular interface to a square interface.
Public void provideCircleUsb (){
This. provideSquareUsb ();
}
}
Package cn.com. pattern;
// Client, that is, the keyboard in question
Public class Client {
Public static void main (String [] args ){
Target target = new Adapter ();
// The keyboard can only use a circular USB Interface
Target. provideCircleUsb ();
}
}
Object Adapter
In this adapter mode, the adapter holds an instance of the class it wraps. In this case, the adapter calls the physical entity of the wrapped object. When you need to adapt to a set of methods that are not defined in the interface, you can create an Object Adapter and implement it using the object method:
[Java]
Package cn.com. pattern;
Public class Adapter implements Target {
Private Adaptee adptee;
Public Adapter (Adaptee adptee ){
This. adptee = adptee;
}
@ Override
// The converter converts the circular interface to a square interface.
Public void provideCircleUsb (){
Adptee. provideSquareUsb ();
}