You can build a fully automatic melon farm by using stick pistons and regular pistons.
The only downside is that it's quite large per-melon, but since it's fully automatic, I think the downside is easy enough to cope with.
gaming.stackexchange.com/users/12087/Mr Smooth
I place a glass block directly over the stem.
This ensures light gets through
but I don't squish my melon seeds.
gaming.stackexchange.com/users/8410/Ken
Note: This no longer works in the latest release of minecraft.
Fence posts directly beneath tilled soil (till after placing the fence, usually) prevent the farmland from being trampled.
This is likely unintended behavior, so it may be fixed in the future.
In the meantime though, this means you can walk up and down your melon farms as needed, and you'll never trample the stems while collecting melons or tilling.
gaming.stackexchange.com/users/336/Raven Dreamer
Build a melon farm according to a design that means each melon can only grow in one place: FFFFFFFFMMMMMMMMWWWWWWWWFFFFFFFFMMMMMMMM Since you have to re-hoe after harvesting a melon anyway, this design means there's no difference in the amount of hoeing that needs to be done.
gaming.stackexchange.com/users/3610/fredley
What I usually do for a melon farm (and for pumpkins farm, also) is the following: FFFFFFFFDDDDDDDDDDDDWDDDFFFF?FFFFFFFFFFFDDDDDDDDDDDDDDDDFFFFFFFFD = DirtF
=
Fertile soil (with stem)W =
Water? =
Anything (I usually put up some fences and blocks, like a lamp post)
Then, I just add sticky pistons below the dirt, wired to a button.
And, above the stems, I put up glass, so not to step in it and turn the fertile soil back to dirt.
This ensures you only have to push a button and walk through the dirt to get all the melon slices.
Nice and easy.
Of course, you can replicate this structure as many times as you want.
I personally find that 2 or 3 of those gives all the melons you'd want.
gaming.stackexchange.com/users/23712/Danilo Martins
I really like the wheat farms described in  this  answer, because it's impossible to trample farmland and harvested crops always bounce back at you.  Are there any similar designs optimized for melon farming?  I tried putting blocks directly above where melons grow, but then I can't get any slices that fall back to where the stem is.
What are some designs for "safe" melon farms?
gaming.stackexchange.com
<minecraft-java-edition>
gaming.stackexchange.com/questions/34300
gaming.stackexchange.com/users/8546/Maxpm
You can use stick pistons and regular pistons for an automatic melon farm. Try placing a glass block over the stem. You should follow the design that each melon can only grow in one place. Some designs of melon farms are described, each with its pros and cons.
Some designs of melon farms are described, each with its pros and cons.