Class Diagram - Example

Here is a brief descrition for a writing a text document:

Suppose that you're writing a document in some of famous text processing tools, like MicrosoftWord for example. You can start typeing a new document, or open an existing one. You type a text by using your keyboard.

Every document consists of several pages, and every page consists of header, document's body or/and footer. In header and footer you may add date, time, page number, file location e.t.c.

Document's body has sentences. Sentences are made up of words and punctual signs. Words consists of letters, numbers and/or special characters. Also in the text you may insert pictures and tables. Table consists of rows and columns. Every cell from table may hold up text or pictures.

After finishing the document, user can choose to save or to print the document.

This is simplified explanation of creating a text document. If we extract the list of nouns form previous text, a following list can be achieved:

document, text processing tool, MicrosoftWord, text, keyboard, header, footer, document's body, date, time, page number, location of file, page, sentence, word, punctual sign, letter, number, special character, picture, table, row, column, cell, user

Nouns colored in red are candidate classes, and candidate attributes for our model.

Let's start with the document. As you can see this example deals with documents, thus a document will be the central class in our class diagram. Document has a several pages, therefore a numberOfPages will be one of the attributes for the Document class. For the operations we have: open(), save(), print() and new(). Every document consists of pages. The Page will be also a candidate for the class.

document-class The Document class

The Page class will hold pageNumber as an attribute, and operations allowed here can be: newPage(), hideHeader() and hideFooter(). Operations for the header and footer tells us that the Header and The Footer can be also a classes.

page-class The Page class

The Header class and the Footer class has common attributes: date, time, pageNumber and fileLocation. These attributes are optional for every header or footer and user may configure them. This will guide us that a common class can be introduced. This will be a good time to make an inheritance. Parent class will be BottomUp (this name is chosen because headers and footer appear in upper and bottom parts of every page) and will hold common attributes for header and footer, and these operations: display(), hide() and change(). Header and Footer classes (children of this class) will have only operations: newHeader() and newFooter().

bottomUp-class The BottomUp class and it's children

Before examining the document's body or text, lets take a look at the makeing of text actions. Document's text is made up of sentences. Sentences are made up of words and words are made up of characters. If words are array of characters and sentence is array of words, then a sentence is also an array of characters. Therefore a document's body can be an array of characters. For this purpose to make a document's text we'll use the Character class with its children. The Character class wil have ASCIIcode and type as attributes (type tells the type of the character - normal, italic, bold or underline), and normal(), bold(), italic() and underline() as operations. The Character class childrens will be: Letter, PunctualSign, SpecialCharacter and Number. Also in the document's body can be found tables and pictures. These are new classes in our class diagram.

character-class The character hierarchy

The Table class has numbRows and numbColumns as attributes and newRow(), newColumn() and newTable() as operations. Every table consists of one or more cells. And in every cell, text or pictures can be placed.

tablePicture-class The Table class and the Picture class

Haveing all this classes in front of us we can draw out the associations between them and gain the completed class diagram for this problem.

classDiagram-document The class diagram for the makeing of text document

Given class diagram is not finished in detail. If you know more about typeing a document, then add information you know to the given diagram. This model can grow and grow, and makeing all the necessary steps of analysis and design you may reach the point, where a new text processing tool is modeled.