OO style advice

DomF fidtz at clara#spam#.co.uk
Thu Feb 19 07:11:30 EST 2004


"Alan Gauld" <alan.gauld at btinternet.com> wrote in message
news:m0m230lm5dv7j3rmpe1425nof55mj11ahf at 4ax.com...
> On Mon, 16 Feb 2004 14:54:31 -0000, "DomF"
> <fidtz at clara#spam#.co.uk> wrote:
> > I am passing in a clock object to the board in __init__ to achieve this.
> > However, means that the clock object can be changed via the board object
and
> > the clock object. Is this good style or should I be designing in a
totally
> > different way?
>
> In OO objects communicate via messages. Thus think about the
> messages that can be sent to the clock. Do not think about the
> data inside the clock, that should be treated as private (even
> though Python does not enforce this).
>
> Now you have a single clock object but referenced both inside and
> outside the board. Is that really what you want or do you want
> the oard to have its own clock instance internally? If it is a
> single object then its perfectly reasonable that both internal
> and external objects can send it messages. If its an internal
> object then only the board can conceptually see it.
> If you need to initialise the internal clock you may want to take
> a time parameter to the board and use that to set the clock
> initial values, rather than passing a functioning clock object as
> the parameter.
>
> Also as somebody else said you may want to use multiple clock
> objects...
>
> > I previously had them separate but that led to lots of ugly board.time1
=
> > clock.time1 statements.
>
> Rather than access the data I'd provide a method, say
> synchronise?
>
> board.synchronise(myClock)
>
> Which internally sets the board clock to the same time as the
> parameter clock, something like:
>
> def synchronise(self, aClock):
>     self.clock.setTime(aClock.time())
>
> Whether you think that looks any less ugly than the assignments
> you had is a moot point!

There isn't only one way to do this then :)

Thanks for the reply, points taken. I think the final decision will depend
on whether I need to use the clock instance outside of the board or not. At
the moment I reckon I will for match and player classes but maybe not!

>
> Alan G.
> Author of the Learn to Program website
> http://www.freenetpages.co.uk/hp/alan.gauld





More information about the Python-list mailing list