definition
Decorator Mode: Dynamically attaching responsibilities to objects without changing the original class file and not using inheritance, thereby enabling the ability to dynamically expand an object. It is by creating a wrapper object that is decorated to wrap the real object. Design Principles
To use the decorator pattern, the following design principles need to be met:1, multiple use combination,
Decorator decoration mode: it is mainly used to dynamically add some additional responsibilities to an object. The extended function is more flexible than the subclass generation method.First go to our example:ProgramFor example
Define abstract base class abscarHere it represents an abstract "car", which is both the base class of the decoration class and the base class of the decoration class.CodeAs follows:
Code
Code highlighting produced by A
Using the Python decorator to remove redundant code in the Django framework, pythondjango
The Python decorator is a powerful tool for eliminating redundancy. As you modularize a function into an appropriate size method, the decorator Can make it a concise function even for the most complex workflow.
For example, let's take a look at the Django web framework. This
Decorator is often translated into "decoration". I think decorator is used to paint a painter, so the object to be painted is called decoratee. these two entities are required in the Decorator mode.
Decorator definition:Dynamically add some additional responsibilities to an object, just like painting on the wall. Using
Decoration mode (Decorator pattern)——. NET Design Pattern series TenTERRYLEE,2006 year MarchOverviewIn software systems, sometimes we use inheritance to extend the functionality of an object, but because of the static traits introduced into the type, this extension lacks flexibility, and as the number of subclasses increases (the expansion function increases), the combination of each seed class (the combination of extended functions) causes more subcl
Decorator has been introduced in my previous blog about Python 2.4 features, but that was Tiger, and now I'll take a closer look at its use.
A detailed introduction to decorator in Python 2.4 is already introduced in what's new, so you can take a look.
How to call Decorator
Basically call decorator there are two forms
12. How to Use the decorator in Python, pythondecorator
Decorator is an advanced Python syntax. The decorator Can process a function, method, or class. In Python, we have multiple methods to process functions and classes. For example, in the Python closure, we can see the function object as the return result of a function. Compared with other methods, the
of child classes increases (the number of extended functions), the combination of various child classes (the combination of extended functions) will lead to the expansion of more child classes (more inheritance ).
How can we enable "extension of object functions" to be implemented dynamically (that is, runtime) as needed? At the same time, avoid the subclass expansion problem caused by "more extended functions? So that the impact caused by any "function scaling changes" is minimized?
Intent)
D
Decorator Mode descriptionDescription: A class to dynamically modify the function object of another class before or after the modification, give it some additional functions; This is the decoration of a class object function, decorated class with the decorated class, requires the same Access interface method (function), which in the dynamic object-oriented class, generally to implement the same interface (interface) to constrain the implementation; Th
The adorner was first presented in Python 2.5, and it was originally used for processing functions and methods such as callable objects (callable object, which are defined as __call__ methods). In Python 2.6 and later Python versions, adorners are further used to process classes.
This is found in many places:
@app. Route ('/')Def hello_world ():Return ' Hello world! '
Call the function before adding a @ What the hell, it turns out to be using a python d
Decoration mode: decorator is often translated into "decoration". I think decorator is used to paint a better image, the paint objects are called decoratee. these two entities are required in the decorator mode.
Decorator Definition:Dynamically add some additional responsibilities to an object, just like painting
Decorator is often converted to "painter". I think it is better to turn to "painter". decorator is used to paint, so decoratee. these two entities are required in the decorator mode.
Decorator Definition:The animation adds an external volume to an object, just like painting the paint on it. the use of the
Directory
First, the problem introducedIi. Principles of DesignThird, use the decorator mode to solve the problemFour, the characteristics of the decorator modelV. Definition of decorator patternVi. the implementation of the decorator patternSeven, java.io in the package of
Create a new text document, enter the code below, save in. bat format, and runHave a shortcut icon missing or feel the icon loading speed is slow, run this, very useful gadgetsrem Close Windows Shell Explorer Taskkill/F/im explorer.exe rem cleanup system icon cache database attrib-h-s-R"%userprofile%\appdata\local\iconcache.db"del/F"%userprofile%\appdata\local\ic
Decorator usage in Python and python
This article describes how to use the decorator in Python. Share it with you for your reference. The specific analysis is as follows:
This is caused by a problem in stackoverflow first. If the following code is used:Copy codeThe Code is as follows: @ makebold@ MakeitalicDef say ():Return "Hello"Print the following output:
What do you do? The answer is:Copy codeThe Code i
Decorator often translated into "decoration", I think translated into "painter" more image point, painter (decorator) is used to brush paint, then painted objects we call decoratee. These two entities are required in decorator mode. Decorator definition :Dynamically add some extra responsibilities to an object, just li
I've already covered the decorator in my previous blog about Python 2.4 features, but then it was suit and now I'm going to describe it in detail.
A detailed introduction to decorator is already in the What ' s new in Python 2.4, and you can take a look.
How to call Decorator
Basically there are two forms of calling decora
"Design pattern Refinement" Learning notes (11)------Decorator (decoration) mode
GoF : Dynamically adding some additional responsibilities to an object. Decorator (decorative) mode is more flexible than generating subclasses in terms of adding functionality.
In the process of system development, we want to add some functionality to an object rather than to an entire class. For example: We have to do a doo
Problem
During OO design and development, the following situations may occur frequently: we need to add new responsibilities (Operations) for a defined class ), in general, we will define a new class to inherit the custom class, but solving this problem through inheritance brings about the complexity of the system, because the depth of inheritance will become very deep.
The Decorator provides a method to add responsibilities to the class, not through
definition: dynamically adds some additional responsibilities to an object. Decorator mode is more flexible than generating subclasses in terms of adding functionality.
Type: Object-structured pattern
alias : Packing mode (wrapper)
class Diagram:
The decorator decoration pattern is a structured pattern that solves the problem of "excessive use of inheritance to extend the functionality of an object", whic
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.