Using the new Observer blocks in Minecraft 1.11, it’s really easy to make an extremely cheap and compact automatic farm for melons and pumpkins.
This farm was designed by Srdd900 on Reddit. How it works is pretty clever.
When a melon or pumpkin grows, the redstone torch & repeater power the melon/pumpkin, which then opens the gate. The observe block detects the gate opening, and powers the piston below. All the melon pieces or pumpkin is then collected in a row of hoppers.
Each module only uses 2 hoppers, 1 piston, 1 observer, and a couple of other basic blocks. It’s modularity is a huge plus, and it’s extremely cheap. An observer block is just a couple of redstone pieces, some quartz, and a bunch of cobblestone.