c# - Do I understand this Strategy Pattern correctly? -
c# - Do I understand this Strategy Pattern correctly? -
in previous question, i've been taught below code illustration of strategy pattern. line _player.draw(spritebatch, _context);
in particular.
i don't see difference between line , 1 below it, aside former beingness method call.
could explain me why wouldn't utilize sec phone call right _drawhugecontext
, (gasp) delete draw()
player
class? illustration simple , there situation former much better?
public class arena { player _player; iplayercontext _drawhugecontext; public void draw(spritebatch spritebatch) { _player.draw(spritebatch, _drawhugecontext); _drawhugecontext.draw(spritebatch, _player); } } public class player { public int percentage { get; private set; } [...] //a few more fields public void draw(spritebatch spritebatch, iplayercontext context) { context.draw(spritebatch, this); } } public class iplayercontext { public void draw(spritebatch spritebatch, player player) { spritebatch.draw(player.percentage); [...] //a few more fields drawn player } }
you using strategy patterns, not yet understand why , how utilize them. allow me give simple example.
let's have set of objects , want sort them. problem is: how specify sort order? in .net typically done passing lambda or class knows how compare 2 of objects "sort" method, e.g.
var sorted = myobjects.sort((a,b) => a.id > b.id);
this allows decouple logic of how generically sort list logic of how order 2 elements of specific set.
in case spritebatch
strategy injected in calls, object construction not need know, exactly, how draw stuff.
i think can utilize illustration above restructure code effectively.
c# xna-4.0 strategy-pattern
Comments
Post a Comment