#pragma section-numbers off = Decorator Pattern = The Wiki:DecoratorPattern is a pattern described in the Wiki:DesignPatternsBook. It is a way of apparently modifying an object's behavior, by enclosing it inside a decorating object with a similar interface. This is not to be confused with PythonDecorators, which is a language feature for dynamically modifying a function or class. == Example == This is an example of using the Decorator Pattern within Python. {{{ #!python """ Demonstrated decorators in a world of a 10x10 grid of values 0-255. """ import random def s32_to_u16( x ): if x < 0: sign = 0xf000 else: sign = 0 bottom = x & 0x00007fff return bottom | sign def seed_from_xy( x,y ): return s32_to_u16( x ) | (s32_to_u16( y ) << 16 ) class RandomSquare: def __init__( s, seed_modifier ): s.seed_modifier = seed_modifier def get( s, x,y ): seed = seed_from_xy( x,y ) ^ s.seed_modifier random.seed( seed ) return random.randint( 0,255 ) class DataSquare: def __init__( s, initial_value = None ): s.data = [initial_value]*10*10 def get( s, x,y ): return s.data[ (y*10)+x ] # yes: these are all 10x10 def set( s, x,y, u ): s.data[ (y*10)+x ] = u class CacheDecorator: def __init__( s, decorated ): s.decorated = decorated s.cache = DataSquare() def get( s, x,y ): if s.cache.get( x,y ) == None: s.cache.set( x,y, s.decorated.get( x,y ) ) return s.cache.get( x,y ) class MaxDecorator: def __init__( s, decorated, max ): s.decorated = decorated s.max = max def get( s, x,y ): if s.decorated.get( x,y ) > s.max: return s.max return s.decorated.get( x,y ) class MinDecorator: def __init__( s, decorated, min ): s.decorated = decorated s.min = min def get( s, x,y ): if s.decorated.get( x,y ) < s.min: return s.min return s.decorated.get( x,y ) class VisibilityDecorator: def __init__( s, decorated ): s.decorated = decorated def get( s,x,y ): return s.decorated.get( x,y ) def draw(s ): for y in range( 10 ): for x in range( 10 ): print "%3d" % s.get( x,y ), print # Now, build up a pipeline of decorators: random_square = RandomSquare( 635 ) random_cache = CacheDecorator( random_square ) max_filtered = MaxDecorator( random_cache, 200 ) min_filtered = MinDecorator( max_filtered, 100 ) final = VisibilityDecorator( min_filtered ) final.draw() }}} ...which outputs something like: {{{ 100 100 100 100 181 161 125 100 200 100 200 100 100 200 100 200 200 184 162 100 155 100 200 100 200 200 100 200 143 100 100 200 144 200 101 143 114 200 166 136 100 147 200 200 100 100 200 141 172 100 144 161 100 200 200 200 190 125 100 177 150 200 100 175 111 195 193 128 100 100 100 200 100 200 200 129 159 105 112 100 100 101 200 200 100 100 200 100 101 120 180 200 100 100 198 151 100 195 131 100 }}} So, what about this is the Wiki:DecoratorPattern ? It's that objects are enclosing other objects, that they share similar interfaces, and that the decorating object appears to mask or modify or annotate the enclosed object. == Discussion == Isn't there a better way to do this in Python? ''To make decorators, to solve this particular problem, or what?'' -- LionKimbro <>