Decoration mode , It means that you don't need to change the original class and use inheritance , Dynamically extend the function of an object . It does this by creating a wrapper object , That is to say Python To wrap the actual object .
stay Python Methods of dynamically extending object functions in , The simplest way is by copying __getattr__ function , stay getattr() Write the functions we need to expand ; Or implement a decorator .
Characteristics of decoration mode :
Decorative objects have the same interface as real objects . In this way, the client object can interact with the decoration object in the same way as the real object .
The decoration object contains a reference to a real object (reference).
The decoration object accepts all requests from the client . It forwards these requests to real objects .
Decoration objects can add some additional functions before or after forwarding these requests . This ensures that at run time , You can add additional functions outside without modifying the structure of a given object . In object-oriented design , It is usually achieved by inheritance