I want to make a cattle farm in my dwarf fortress

Solution 1:

If you want to accomplish anything you'll probably want to build a cage (or multiple) and have it in one room. Assign all the animals to the cage(s) and keep them in there. You'll probably want to make a zoo from that cage.

What this means is that you don't need to feed/water them while they're in the cage, they can't move around so they won't eat your framerate, and by making it a zoo you even give your dwarves happy thoughts for watching animals, and even have another meeting hall.

If you put them all in an enclosed space they will still try to sneak out whenever a dwarf opens the door, and they will keep walking around which will hurt your framerate.

Solution 2:

If you want to have a traditional "farm" where the cows will reproduce, you'll have to use ropes or chains. Assign one animal to each chain/rope. Unlike a cage, your animals will still be able to reproduce.

For bonus points, you can build "watch towers" that have your cows on it. This is so you can spot invaders the moment they arrive. All you have to do is build a raised platform with some chains on top. I use access tunnels that go underground so there are no stairs or ramps that the invaders can use to get to the animals. Archers could still be a problem though. Early detection on an invasion can save the lives of your "outdoor dwarfs".

Hope that helps