Demo entry 6776232



Submitted by anonymous on Nov 30, 2018 at 12:51
Language: Python. Code size: 1.6 kB.

class Elf:
    name = 'Rosalor'

    def le_suilon(self):
        print('Elf {name} says: Greeting'.format(

class Orc:
    name = 'Khadgar'

    def mok_rah(self):
        print('Orc {name} says: Greeting'.format(

class Human:
    name = 'Severus Smithy'

    def say_hello(self):
        print('Human {name} says: Greeting'.format(

class HeroAdapter:
    _initialised = False

    def __init__(self, hero, **adapted_methods):

        self.hero = hero

        for key, value in adapted_methods.items():
            func = getattr(self.hero, value)
            self.__setattr__(key, func)

        self._initialised = True

    def __getattr__(self, attr):
        return getattr(self.hero, attr)

    def __setattr__(self, key, value):
        if not self._initialised:
            super().__setattr__(key, value)
            setattr(self.hero, key, value)
            self.notify_observer(key=key, value=value)

class HeroFacade:
    hero_adapters = None

    def create_heroes(cls):
        print('Creating heroes ...')
        cls.hero_adapters = [
            HeroAdapter(Human(), call_me='say_hello'),
            HeroAdapter(Elf(), call_me='le_suilon'),
            HeroAdapter(Orc(), call_me='mok_rah')

    def call_heroes(cls):
        print('Call heroes ...')
        for adapter in cls.hero_adapters:

if __name__ == '__main__':


This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).