Python implements the decorator design pattern

The python decorator is simple and the basic implementation is not complicated. Decorators mode is similar to inheritance. When you need to add additional actions and behaviors to an object, you can use decorators without changing the class. I didn't want to write this article as a hydrology article, because this column is a basic multi-language realization of the design pattern and does not involve too much content. In order to ensure the integrity of the content, it can only be inserted directly.

First, we create a new base class for a person, and assign several attributes (name, gender, hair, clothes, etc.), and there are two base classes, man and woman:

class People():
    name = ""
    sex=""
    clothes = "没穿"
    hair="光头"
    sound_color=""

class Man(People):
    def __init__(self,name):
        self.name=name
        self.sex="男"
 
class Woman(People):
    def __init__(self,name):
        self.name=name
        self.sex="女" 

Because of the class and new creation, no changes are made, and the decorator is used to add the behavior state.
Create a new decorator base class, set up the decorator methods, gethair and getclothes, and write two classes hairDecorator and dressedDecorator inherited from exteriorDecorator, make the object grow hair in the decorator hairDecorator, the attribute value of the hair in the original object is critical , Make the object wear clothes in dressedDecorator, the original attribute is no clothes. The decorator class is as follows:

class exteriorDecorator():#外形装饰基
    def gethair(self):
        pass
    def getclothes(self):
        pass
        
class hairDecorator(exteriorDecorator):#头发装饰
    def __init__(self,people):
        self.people=people
        self.sex="男"
    def gethair(self):
        return str(self.people.name)+" 长出头发"

class dressedDecorator(exteriorDecorator):#外衣装饰
    def __init__(self,people):
        self.people=people
        self.sex="男"
    def getclothes(self):
        return str(self.people.name)+" 穿上外衣"

The above decorator passed in the class object during initialization. Next, create a new object Xiaoming and decorator object:

xiaoming=Man("小明")
xiaomingHariD=hairDecorator(xiaoming)

Use the hairDecorator method to decorate Xiao Ming's hair, and use dressedDecorator to decorate Xiao Ming's top:

xiaomingHariD=hairDecorator(xiaoming)
xiaomingdressedD=dressedDecorator(xiaoming)

Finally, output:

print(xiaomingHariD.gethair())
print(xiaomingdressedD.getclothes())

The results are as follows: the
Insert picture description here
complete code is as follows:

class People():
    name = ""
    sex=""
    clothes = "没穿"
    hair="光头"
    sound_color=""

class Man(People):
    def __init__(self,name):
        self.name=name
        self.sex="男"
 
class Woman(People):
    def __init__(self,name):
        self.name=name
        self.sex="女" 


class exteriorDecorator():#外形装饰基
    def gethair(self):
        pass
    def getclothes(self):
        pass
        
class hairDecorator(exteriorDecorator):#头发装饰
    def __init__(self,people):
        self.people=people
        self.sex="男"
    def gethair(self):
        return str(self.people.name)+" 长出头发"

class dressedDecorator(exteriorDecorator):#外衣装饰
    def __init__(self,people):
        self.people=people
        self.sex="男"
    def getclothes(self):
        return str(self.people.name)+" 穿上外衣"

xiaoming=Man("小明")
print(xiaoming.name)
xiaomingHariD=hairDecorator(xiaoming)
xiaomingdressedD=dressedDecorator(xiaoming)
print(xiaomingHariD.gethair())
print(xiaomingdressedD.getclothes())

Guess you like

Origin blog.csdn.net/A757291228/article/details/107100321