c# - How do I know what to put in my interfaces? -
i've come across recurring problem when trying design logic programs. let's have idriveable interface.
interface idriveable { public void drive(); }
then car class implements (c#) syntax:
class car : idriveable { public void drive(){ //do movement here. } }
here's problem occurs. if designing game, car doesn't drive itself, player should drive car, surely makes sense?
class player { public void drive(idriveable vehicle){ vehicle.drive(); } }
it feels 'ping-ponging' logic around doesn't seem right.
a better way structure code might this:
class player // start class names capital letter { car thisplayerscar; // initialize constructor or somewhere appropriate public void somefunction() { thisplayerscar.drive(); } }
basically, purpose of interface wherever call thisplayerscar.drive();
(or drive()
on idriveable
), you're guaranteed object have drive()
function ready go.
Comments
Post a Comment