<All understand design patterns> - decorative patterns

The book, with a real mix of people playing dress decorative pattern to explain it.

from abc import ABCMeta, abstractmethod


class Person(metaclass=ABCMeta):

    def __init__(self, name):
        self._name = name

    @abstractmethod
    def wear(self):
        print("着装。。。")


class Engine(Person):

    def __init__(self, name, skill):
        super().__init__(name)
        self.__skill = skill

    def get_skill(self):
        return self.__skill

    def wear(self):
        print("我是{}工程师{}。".format(self.get_skill(), self._name))
        super().wear()


class Teacher(Person):

    def __init__(self, name, title):
        super().__init__(name)
        self.__title = title

    def get_title(self):
        return self.__title

    def wear(self):
        print("我是{}{}。".format(self._name, self.get_title() ))
        super().wear()


class ClothingDecorator(Person):

    def __init__(self, person):
        self._decorated = person

    def wear(self):
        self._decorated.wear()
        self.decorate()

    @abstractmethod
    def decorate(self):
        pass


class CasualPantDecorator(ClothingDecorator):

    def __init__(self, person):
        super().__init__(person)

    def decorate(self):
        print("一条卡其色休闲裤")


class BeltDecorator(ClothingDecorator):

    def __init__ (Self, the Person): 
        Super (). __init__ (the Person) 

    DEF the decorate (Self):
         Print ( " black belt a silver pin buckle head " ) 


class LeatherShoeDecorator (ClothingDecorator): 

    DEF  __init__ (Self, the Person): 
        Super ( ). __init__ (the Person) 

    DEF the decorate (Self):
         Print ( " one pair of casual shoes dark " ) 


class KnittedSweaterDecorator (ClothingDecorator): 

    DEF  __init__ (Self, the Person): 
        . Super () __init__ (the Person) 

    DEFthe decorate (Self):
         Print ( " a purple knit sweater " ) 


class WhiteShirtDecorator (ClothingDecorator): 

    DEF  __init__ (Self, the Person): 
        Super (). __init__ (the Person) 

    DEF the decorate (Self):
         Print ( " a white shirt " ) 


class GlassesDecorator (ClothingDecorator): 

    DEF  __init__ (Self, the Person): 
        Super (). __init__ (the Person) 

    DEF the decorate (Self):
         Print ( " a square black-rimmed glasses " ) 


DEF test_decorator():
    tony = Engine("Tony", "客户端")
    pant = CasualPantDecorator(tony)
    belt = BeltDecorator(pant)
    shoes = LeatherShoeDecorator(belt)
    shirt = WhiteShirtDecorator(shoes)
    sweater = KnittedSweaterDecorator(shirt)
    glasses = GlassesDecorator(sweater)
    glasses.wear()

    print()
    decorator_teacher = GlassesDecorator(WhiteShirtDecorator(LeatherShoeDecorator(Teacher("wells", "教授"))))
    decorator_teacher.wear()

test_decorator()
C: \ Python36 \ python.exe C: / the Users / Sahara / PycharmProjects / test1 / test.py 
my client engineer Tony. 
Dress. . . 
A khaki slacks 
a silver needle buckle black belt 
one pair of dark-colored casual shoes, 
a white shirt 
a purple knit sweater 
a square black-rimmed glasses, 

I was a professor wells. 
Dress. . . 
One pair of dark-colored casual shoes 
and a white shirt 
a square black-rimmed glasses 

Process Finished with Exit code 0

 

Guess you like

Origin www.cnblogs.com/aguncn/p/11333822.html