Adorner mode
Adorner mode (Decorator pattern) allows you to add new functionality to an existing object without changing its structure. This type of design pattern belongs to the structural pattern, which is a wrapper as an existing class.
This pattern creates an adornment class that wraps the original class and provides additional functionality, while preserving the integrity of the class method signature.
Import TimedefLog_calls (func):defWrapper (*args,**Kwargs): now=time.time ()Print("calling{0} with {1} and {2}". Format (func.__name__, Args,kwargs)) Return_value=func (*args,**Kwargs)Print("executrd{0} in {1}ms". Format (func.__name__, Time.time ()-Now )) returnReturn_valuereturnwrapperdefTest1 (A, b):Print("Test1 called") Time.sleep (1) Test1=log_calls (test1) test1 ()
The function passes in the adorner and returns a new function that assigns the function name of the original function to the new function, effectively replacing the original function with the decorated function.
The adorner mode of Python design mode