Creating a Web-Based Discussion Forum

 < Free Open Study > 

In this section, we'll implement a small web application to illustrate the concepts of good and bad design we've been discussing. We'll create a discussion forum, as seen on many web sites. Before we can start designing our forum we need to define its features. This is an important stage in the development process, and it's important to fully state the requirements of our application:

  • The discussion forum contains a number of topics

  • Each topic may contain zero or more responses

  • A user can view the list of topics, and also view all the responses made to a topic

  • A user can post a response to a topic, but to ensure that responses are not anonymous, the user must be logged in

  • We'll allow the users to delete their own responses too

This is a fairly simple set of requirements, so building the application shouldn't be too difficult. However, before we start implementing the functionality, we need to understand the underlying domain on which the application will operate.

Entities Within the Business Domain

An important aspect of object-orientated (OO) analysis and design is to identify the entities that are present in the application's domain. For our discussion forum we have three entities:

  • Topic - represents a topic on the discussion forum

  • Response - a response to a topic on a discussion forum, of which there may be zero or more

  • User - the author of a topic or response

Our next step is to model the entities and the relationships between them using UML. This allows us to represent the domain clearly and concisely, so that we can communicate it effectively to both developers and non-developers within the team.

You can learn more about UML in "UML Distilled", 2nd edition (Addison Wesley, ISBN 0-201657-83-X), or "Instant UML" (ISBN 1-861000-87-1).

The Class Model for the Discussion Forum

From our model of the domain and through a process of steps beyond the scope of this book, we eventually arrive at a design for the classes in our system that will represent the entities within our business domain. This could be a simple process such as picking out the nouns in a requirements specification, or by looking at the entities present in a UML analysis model of the business.

The classes required for our application map almost exactly onto the entities discovered in the business domain, and this is often the case in real world projects. Here is the class diagram:

click to expand

As you can see, we have Topic, Response and User classes that map to our entities, and we also have a Posting. This is an abstraction of both Topic and Response and represents the commonality between them. In this case, both topics and responses have some text (the message) and a user associated with them.

The User Class

The User class in this example is just a wrapper for a user id and a password. In the future, however, we might want to modify it to contain further information, so a reference to the User object is stored in the Posting rather than just the username. The complete User class is as follows:

    package forum;    public class User {      private String id;      private String password;      User(String id, String password) { = id;        this.password = password;      }      public String getId() {        return;      }      String getPassword() {        return this.password;      }      public boolean equals(Object o) {        if (o instanceof User) {          User u = (User)o;          return getId().equals(u.getId());        } else {          return false;        }      }    } 

The Posting Class

A Posting object is an abstraction of both a topic and a response. Here's the class:

    package forum;    public abstract class Posting {      protected int id;      protected String text;      protected User user;      public Posting(User user, String text) {        this.user = user;        this.text = text;      }      public int getId() {        return;      }      public void setId(int id) { = id;      }      public User getUser() {        return this.user;      }      public String getText() {        return this.text;      }    } 

As mentioned, topics and responses have an id, some text (the message) and an associated User. In a similar way to the User class, Posting is again a wrapper around this basic information.

The Topic Class

The Topic class is a little more than a simple wrapper this time. It extends Posting and therefore inherits all of the basic information illustrated previously. In addition to this, Topic introduces a title and a collection of responses that have been made.

Topic also introduces some functionality - the ability to add a response, remove a response, get a response(s), and finally get a count of the number of responses that have been added so far.

start sidebar

Many users would use a real world discussion forum concurrently. For this reason, we would need to ensure that data is not corrupted by synchronizing access to the data. However, for simplicity's sake, this example does not include such synchronization code.

end sidebar

    package forum;    import java.util.*;    public class Topic extends Posting {      private String title;      private ArrayList responses = new ArrayList();      public Topic(int id, User user, String title, String text) {        super(user, text); = id;        this.title = title;      }      public String getTitle() {        return this.title;      }      public void add(Response response) {        responses.add(response);        response.setId(responses.size() - 1);        response.setTopic(this);      }      public void remove(Response response) {        response.setDeleted(true);      }      public Response getResponse(int id) {        return (Response)responses.get(id);      }      public Collection getResponses() {        return responses;      } 

The getResponseCount() method gets the number of (non-deleted) responses that have been made to this topic:

      public int getResponseCount() {        int count = 0;        Response response;        Iterator it = responses.iterator();        while (it.hasNext()) {          response = (Response); 

We don't include those responses that have been deleted:

          if (!response.isDeleted()) {            count++;          }        }        return count;      }    } 

The Response Class

Finally we have the Response class. Again this extends Posting, this time adding a reference to the parent Topic and a flag to indicate whether the response has been deleted:

    package forum;    public class Response extends Posting {      private Topic topic;      private boolean deleted = false;      public Response(User user, String text) {        super(user, text);      }      public boolean isDeleted() {        return this.deleted;      }      public void setDeleted(boolean b) {        this.deleted = b;      }      public void setTopic(Topic topic) {        this.topic = topic;      }      public Topic getTopic() {        return this.topic;      }    } 

 < Free Open Study > 

Professional Java Servlets 2.3
Professional Java Servlets 2.3
ISBN: 186100561X
EAN: 2147483647
Year: 2006
Pages: 130

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: