PiPHP / GPIO

A PHP library for accessing the GPIO pins on a Raspberry Pi.
MIT License
425 stars 43 forks source link

Flyweight pattern #8

Open AndrewCarterUK opened 8 years ago

AndrewCarterUK commented 8 years ago

Should the flyweight pattern be used (so that there is only ever one instance of any pin as they are remembered by the factory)?

For

Again - interested to hear thoughts?

colinodell commented 8 years ago

so that there is only ever one instance of any pin as they are remembered by the factory

That makes sense to me since only one instance of the physical pin exists anyway. I'm not yet convinced it's totally necessary (will have to think on this) but it certainly seems useful.

Might make it more difficult to switch a pin from an input to an output pin. Primary key would have to be number + direction rather than just number.

I honestly don't think this will be very common. If you really need a pin to read and write data at different times you should probably be using a protocol like I2C, SPI or 1 Wire which can coordinate that switching with the other component(s) you're wired up to. And I think Raspberry Pi handles those with kernel modules separate from GPIO (unconfirmed).

Because I personally wouldn't be switching pin modes in my projects, I don't have a strong preference on how it handles people creating input and output pin objects for a single physical pin - it can either allow it or throw a big exception for all I care :)

Memory and performance differences could be insignificant

I wouldn't be too worried about the memory footprint of the objects themselves, but could having so many streams cause performance issues? If so then I think a flyweight could definitely help there.