Take the 2-minute tour ×

I am newbie to Java. I have some design questions.

Say I have a crawler application, that does the following: 1. Crawls a url and gets its content 2. Parses the contents 3. Displays the contents

  1. How do you decide between implementing a function or a class? -- Should the parser be a function of the crawler class, or should it be a class in itself, so it can be used by other applications as well? -- If it should be a class, should it be protected or public class?

  2. How do you decide between implementing a public or protected class? -- If I had to create a class to generate stats from the parsed contents for eg, should that class be protected (so only the crawler class can access it) or should it be public?

Thanks Ron

share|improve this question
Since it seems like a simple application, why don't you just go and experiment with what makes the most sense to you right now? If you made the wrong design choices, soon you'll encounter a problem or two, revise your design, and learn that way. ;-) –  Santa May 5 '10 at 0:00
All Great answers! Thanks! –  user333751 Jan 29 '11 at 19:43

3 Answers 3

I think Andy's answer is very good. I have a few additions:

If you believe that a class will be extended in the future, you can set all your private methods (if any) to protected. In this way, any future extending classes can also access these.

I like the rule that a method shouldn't be longer than that you can see its opening and closing brackets ({ }) without scrolling. If a method is longer than that, try to split it up into several methods (private, protected or public by your preference). This makes code more readable, and could also save on lines of code.

So let's say a method is getting big and you split it up into several private methods. If these new methods are only used within the first "mother"-method, it makes sense to move all of that into a class of its own. In this way you will make the original class smaller and more readable. In addition, you will make the functionality of the new class easier to understand, as it is not mixed up with that of the original class.

share|improve this answer

The best guidance I've seen for these types of questions is the "SOLID Principles of OO Design."


The most basic of these principles, and the one that sort of answers your first question is the "Single Responsibility Principle." This states that, "a class should have one, and only one, reason to change." In other words, your classes should each do exactly one thing. If you end up needing to change how that one thing works, you only have one class to change, and hopefully just one place to make the change within that class. In your case, you would probably want a class to retrieve the content from the URL, another class to parse it into some sort of in-memory data structure, another class to process the data (if needed), and yet another class (or classes) to display the content in whatever format you need. Obviously, you can get carried away with classes, but it's typically easier to test a lot of small, single-operation classes, as opposed to one or two large, all-encompassing classes.

The question on public vs. protected depends on how you plan to use this code. If your class could be used independently outside your library, you could think about making it public, but if it accomplishes some task which is specific or tied to your other classes, it could probably be protected. For example, a class to retrieve content from a URL is a good general-purpose class, so you could make it public, but a class that does some specific type of manipulation of data might not be useful outside your library, so it can be protected. Overall, it's not always black and white, but ultimately, it's usually not a huge deal either way.

share|improve this answer

I like to think of classes as "guys" who can do specific stuff "methods".

In your case, theres a guy who can fetch the content of an url if you tell him which url that is.

Then there is this another guy, that is really good at parsing content. I think he does that with a tool called rome, but i'm not sure. he keeps that private (hint ;) )

Then we have that third guy, who displays stuff. He's a bit retarded and only understands stuff that "another guy" produces, but hey thats fine.

Finally the project needs a boss guy, who gives orders to the other 3 guys and passes messages between them.

ps: I never really though about making classes protected or not. Usually they are simply public without any specific reason. As long as it don't hurt, why bother?

share|improve this answer
I also used to think the way you do until I encountered threads. If you think of the objects as guys who can call methods then who/what are the threads? Yet, regarding answering this question, it is ok to think so. –  Val Nov 9 '12 at 13:27

Your Answer


