In my program, I draw some quads. I want to add the functionality for them to scale up, then down, then go back to being static (to draw attention). In the quads I have:
self.scale = 10
Making scale change according to sin would be nice. But adding frequency, amplitude and logic to my already bloated quad class is something I take as a challenge to avoid.
Something like this:
class mysin:
def __init__(self):
self.tick = 0.0
self.freq = 1.0
self.ampl = 1.0
def update(self, amount):
self.tick += amount
def value(self):
return math.sin(self.tick)
That class would also add itself to the logic system (getting update calls every frame). I would then do:
quad.scale = 10 # for static quad
quad.scale = mysin() # for cool scaling quad
The problem is that some calculations expect scale to hold a value. I could of course add another class where value() returns a (previously saved) constant value and adapt all the calculations.
What I want to know now is… does this have a name, is it a valid technique? I read the wiki article on functional programming and this idea sprung to mind as a wacky implementation (although Im not sure it qualifies as FP). I could very well have been driven mad by that article. Put me back in line fellow coders.
The distinction between
and
Is minor. Within the
Quadclass definition the “scale” attribute can be a property with proper getter and setter functions.Alternate version with the explicit
propertyfunction (which I prefer).Any other class should NOT know or care what type of value
scalehas. If some client does thisThen you have design issues. You haven’t properly encapsulated your design and Quad’s client classes are too friendly with Quad.
If you absolutely must do this — because you can’t write a method function of Quad to encapsulate this — then you have to make
MySina proper numeric class so it can respond toquad.scale * 2requests properly.