Analysis:At Star Buzz's coffee shop, there's a variety of coffees and spices. If we choose a coffee in addition to one or several ingredients (the main master is a decorator and many decorators). So how do we charge for these coffees and ingredients (the charge here is a good example, and of course it could be some of the same things)? If you say that we can encapsulate each of the optional combinations, God, how many classes would that be? Also, wha
interface to the specification of the object to receive the additional responsibility.(2) The specific widget (concrete Component) Role: Defines a class that will receive additional responsibility.(3) decorative (decorator) Role: Holds an instance of a component (Component) object and implements an interface that is consistent with the interface of an abstract component.(4) The specific decoration (concrete decor
Adorner mode (decorator pattern) allows new functionality to be added to an existing object without altering its structure. This type of design pattern is a structural pattern and is a wrapper for an existing class.
This pattern creates a decorative class that wraps the original class and provides additional functionality while preserving the integrity of the class method signature.
We demonstrate the use of the adorner pattern through the example b
Let's talk about the decorator of py and the decorator of py.
It was very mysterious to know that there was such a decoration device.
Including c # and java,
C # is called the attribut feature, and java is calledAnnotationNote: When I secretly studied the c # tutorial, I admitted that I watched the pirated video and ta
roundFaceFeature=new RoundFaceFeature(); SmileMouth smileMouth=new SmileMouth(); roundFaceFeature.decorator(face); smileMouth.decorator(roundFaceFeature); smileMouth.show(); }
The UML diagram of the above Code is as follows:
Discuss
In the preceding example, the Face class serves as both a responsibility interface and a specific dress object. FaceFeature and Mouth are two dress classes. In java jdk, there is also a typi
The decorator mode. I have always remembered this mode. Because of Java I/O, I used to think that Java I/O is a class explosion. Since I understood the decorator mode, I think the IO System of Java is very cute. Now let's look at what the
Python decorator and python decorator
The decorator is essentially a Python function that allows other functions to add additional functions without any code changes. The return value of the decorator is also a function object.
It is often used in scenarios with specific requirements, such as log insertion, performance
= new Mocha (beverage2); // use the second Mocha to decorate it. Beverage2 = new Mocha (beverage2); // use Whip to decorate it. Beverage2 = new Whip (beverage2); System. out. println (beverage2.getDescription () + "$" + beverage2.cost (); // finally, a cup of HouseBlend coffee with soy milk, Moka, and milk. Beverage beverage3 = new HouseBlend (); beverage3 = new Soy (beverage3); beverage3 = new Mocha (beverage3); beverage3 = new Whip (beverage3); System. out. println (beverage3.getDescription (
Python decorator understanding, python decorator understanding
1. Functions of the decorator
Add new functions for the decorated object without modifying the source code and calling method of the decorated object
Principles:
1. Do not modify the source code of the decorated object2. Do not modify the call method of the decorated object
Objectives:
Add new featur
, it provides us with a warm effect.
Before talking about the decorator, you must first understand one thing. the functions in Python are not the same as those in Java and C ++, functions in Python can be passed to another function as parameters like common variables. for example:
def foo(): print("foo")def bar(func): func()bar(foo)
Officially return to our topic. The
Still from Zhili https://www.cnblogs.com/zhili/p/DecoratorPattern.htmlThank you, hehe.----------------------------------------------------Split Line--------------------------------------------------------First, IntroductionIn software development, we often want to add different functions to a class of objects, such as to add film, mobile phone pendant, mobile phone shell, etc., if the use of inheritance at this time to achieve, you need to define a myriad of classes, such as Stickerphone (film i
Python are not the same as those in Java and C ++, functions in Python can be passed to another function as parameters like common variables. For example:
def foo(): print("foo")def bar(func): func()bar(foo)
Officially return to our topic. The decorator is essentially a Python function or class that allows other functions or classes to add additional functions without any code modifications, the Return V
date (): print "2014-11-5" f = date//f is the function pointer date () f ()The function object has a property __name__, which can get the name of the function: for example: f.__name__ is date.below start to get to the point, decorator is what, Decorator is a high-order function that returns a function . For example, we have a simple log function that does the work of logging each time a function is call
Python's decorator is similar to java/c# 's annotation in its use, which is to decorate something for this method by adding a @xxx annotation to the method name. However, Java/c# 's annotation is also very daunting, too TMD complex, you want to play it, you need to know a bunch of annotation library documents, let people feel is learning another language. Python
Decorator Mode (decorator pattern): dynamically attaches responsibility to an object. To extend functionality, adorners provide a more flexible alternative than inheritance.
How to use:
1. First create the component (Component) parent class , all classes, specific components (concrete Component) and adorners (decorator) belong to this type and can be extended;
Swimcardecorator.javainherit from Cardecorator, realize the function that the car can swim in detail:
Public classSwimcardecoratorextendsCardecorator { Publicswimcardecorator (car car) {Super(car); } Public voidShow () { This. Getcar (). Show (); This. Swim (); } Public voidswim () {System.out.println ("The Swimming"); } Public voidrun () {}}
Test class:
Public classMainClass { Public Static voidMain (string[] args) {//ordinary car, only to runCar car =NewRuncar ();
Python decorator usage example summary, python decorator usage example
This document describes how to use the Python decorator. We will share this with you for your reference. The details are as follows:
I. What is the decorator?
The python decorator is essentially a Python
Example of head first: coffee shops have a variety of coffee drinks, can be added to the coffee in a variety of spices into another drink, if the use of inheritance to design a class for each beverage, the complexity of the code can easily expand, and will inherit all the attributes of the parent class, due to the inheritance of the type introduced static characteristics, The lack of flexibility in this way of scaling, while also falling into another trap, as the expansion function increases, th
variables and inheritance, you can trace the seasoning.The design is as follows:In doing so, you can actually meet the requirements temporarily, but there are some potential pitfalls, as follows:L Spice Price Changes will change the original codeNew seasoning, in addition to adding new methods, you also need to change the cost () method in the Super classL dependency inheritance, subclasses will inherit some methods that are not appropriate for themselvesL Part of the demand is not satisfied: s
I often talk about the Python advanced decorator and the python advanced decorator.
Functions are also objects
To understand the Python modifier, you must first understand that in Python, a function is also an object. Therefore, you can regard the function name when defining a function as a reference to a function object. Since it is a reference, you can assign a function to a variable, or pass or return a
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.