When we're teaching our dogs new things, it's best to do so with limited distraction. The reason you went to the library or some other quiet place to study holds true when your dog is studying as well. But at some point we need to start challenging the dog's reliability and response under increasingly distracting circumstances. I think we take how this process was done in our own lives for granted. In school, you learned to count before learning arithmetic, and then algebra, and then calculus, with multiple intermediate steps in between. Your dog needs that same kind of methodical approach if you want them to be reliable in terms of behavior and obedience out in public.
This idea, what trainers call "proofing", requires the handler to find the dog's "threshold", or tipping point, of response. When you're out walking your dog and they notice something, we typically see a few key indicators of arousal in our dogs: tail goes up, brow is wrinkled, ears are forward, mouth closes. Any combination of these indicators lets you know that you've reached the "tipping point" or "threshold". Continue to move closer to whatever stimulus is present will result in your dog reaching a point where exhibiting self control is not only difficult, but sometimes impossible. Your job if you have a reactive or fearful dog, or are trying to increase the reliability of obedience, is to work at that tipping point (right where they take notice but are still able to exert self control) until your dog shows that they are able to stay calm. The next time you work, you may find you can move a bit closer, and the time after, even closer.
It's important to note that your dog's threshold will change based on what distractions you are working against. Just because your dog can respond reliably with a person walking by 5 feet away, doesn't mean they can do the same thing with a dog walking by 5 feet away. And just because your dog was able to do something yesterday, doesn't mean they won't have difficulty doing the same thing today. Read your dog, adjust accordingly, and work with what you have in front of you. You will never go wrong if you pay attention to your dog, let them tell you when they are at their tipping point, and then work there. And if they do have trouble, move to a quieter place or ask them to do something easier to remind them how easy it is to be successful. When instead, we work on what "should be" (what we think they should be able to do), instead of "what is" (what the dog is telling us themselves), that's when we run into problems. So pay attention to your dog. They never lie.