Inheritance

Using inheritance to define content types

Lets say I'm building a simple web site and I managed to identify three kinds of pages; a start page, a text page and a news page. You could go about and and define all of those creating three classes inheriting from N2.ContentItem. However, a better approach would be to analysing the types looking similarities and trying to answer the question this type xxx is a specialized yyy.

 Start page Text page
News page
Title x
x
x
Menu
x
x
x
Company logo
x
x
x
Large image
x
  
Small image with link
x
  
List of the latest news
x
  
A date
  x
An introductio   x
Large area text x
x

In the context of our example it wouldn't be out of hand to say that the news page is a kind of text page. Lets see how these relations could look in a class diagram.

Example of a class diagram

A few things to note:

  • I decided that the logo is the same on all pages. Therefore I'm only adding it to the start page and then I'll make sure to it from the start page (instead of the current page) in my master page.
  • Since I'm considering the news page as a kind text page in this situation I'm inheriting that
  • For the menu I'll just use the available menu adapters
  • I'm re-using the Title property defined in the N2.ContentItem class to store the pages' names
  • I'm re-using the Published property defined in the N2.ContentItem on the news page to store the date the news was published (and therefore assuming I'm not going to display a news from the future)

Here I decided that the news page is a kind of text page. Another approach could be putting the text property on a common abstract base class and having both these classes inheriting from it.

In thise case we have used inheritance to extend the content model. N2 also supports extending pages using composition (has a).

While compiling and running this already allows me to create all these kinds of pages in the tree structure, there is still some work to do before I have something usable...