Simple Object Oriented Design

R

rick walsh

I am trying to write an application that displays news articles on a web
page.

I have recently moved to C# and currently I am trying to learn how to use
good Object Oriented design.

I assume that I should create a class called mynews. This will hold member
information such as headline, date, bodytext etc.

This can then be called by a page to display a single news item. ie
mynews objmynews = new mynews(1)

label1.Text = objmynews.headline;
label2.Text = objmynews.date;
label3.Text = objmynews.bodytext;

The mynews class would talk to the database only once to do this.

However, if I want to produce a listing of news items I would have to
instantiate this class many times resulting in many calls to the database.
For example:

mynews objmynews = new mynews(1)
label1.Text = objmynews.headline;
label2.Text = objmynews.date;
mynews objmynews = new mynews(2)
label3.Text = objmynews.headline;
label4.Text = objmynews.date;

This is clearly not effeicient. How should I code this?

Should I write an additional class? Is this good oo design?

Help I'm stuck!!!

Many thanks.

Rick
 
M

Morten Wennevik

Well, for one, it's more like newsitem than mynews :p, and if all it does
is contain headline, date, body you might just as well make it a struct

struct newsitem
{
string headline;
string date;
string bodytext;
}

I assume you have some way of entering news in some way. Store each
newsitem in an array or database/xml file or something. Equally store
each label in an array (or perhaps a label item holding 3 labels)

for(int x = 0; x < maxNews; x++)
{
newsitem item = (newsitem)newsArray[x];
((Label)labelArray[x*3]).Text = item.headline;
((Label)labelArray[x*3 + 1]).Text = item.date;
((Label)labelArray[x*3 + 2]).Text = item.bodytext;
}

A more object oriented way might be to pass the newsitem to a labelitem
and have it update its own three labels.
 
R

rick walsh

Cheers for your help. It is much appreciated.

I had several thoughts since I initially posted the query.

the newsitem class also handles adding a news item to the database etc....

I was thinging of creating a class that would handle HTML generation. For
example:

class createHTML
{

private string strNewsHTML

public display(int id)
{
GenerateNewsHTML(id)
}

public string NewsHTML
{
get { return strNewsHTML; }
set { strNewsHTML=value; }
}


public void GenerateNewsHTML(int id)

{
newsitem objnewsitem = new newsitem (id)
NewsHTML = objnewsitem.headline + "<br>"+ objnewsitem.date + "<br>" +
objnewsitem.bodytext;
}

}


The page would then have the following

createHTML objdisplay = new createHTML(1)
div_content.innerHtml = objdisplay.NewsHTML


However, I want the createHTML class to also manage and create listings of
news. Should it instantiate multiple newsitem class objects or use another
method?

the GenerateNewsHTML
Well, for one, it's more like newsitem than mynews :p, and if all it does
is contain headline, date, body you might just as well make it a struct

struct newsitem
{
string headline;
string date;
string bodytext;
}

I assume you have some way of entering news in some way. Store each
newsitem in an array or database/xml file or something. Equally store
each label in an array (or perhaps a label item holding 3 labels)

for(int x = 0; x < maxNews; x++)
{
newsitem item = (newsitem)newsArray[x];
((Label)labelArray[x*3]).Text = item.headline;
((Label)labelArray[x*3 + 1]).Text = item.date;
((Label)labelArray[x*3 + 2]).Text = item.bodytext;
}

A more object oriented way might be to pass the newsitem to a labelitem
and have it update its own three labels.
 
M

Morten Wennevik

You could create a class that handles all internal news stuff
It will store all newsitems and keep track of latest news and listings
This would also separate the news item structure from the database
insertion.

Although it may be good object orienting to have a news item store itself,
one can argue if the news item should be aware of any outside stuff, like
where it should be stored. I would let make a class for handling news
items, and formatting them for html in whatever format.

A createHTML class like you mentioned could do all this stuff, and have
the web program only requests a full page, or parts of a page.

class createHTML
{
private string strNewsHTML;

public void display(int id)
{
GenerateNewsHTML(id);
}

public string NewsHTML
{
// You only need to be able to publish the page upon request
// GenerateNewsHTML have access to the private string
get { return strNewsHTML; }
// set { strNewsHTML=value; }
}

// This is not supposed to be accessible to the public, use private
private void GenerateNewsHTML(int id)
{
// no need to use the property as this is already in the same class
// do not use new newsitem as it should already be made
newsitem objnewsitem = getnewsitem(id);
strNewsHTML = objnewsitem.headline + "<br>"+
objnewsitem.date + "<br>" +
objnewsitem.bodytext;
}

private newsitem getNewsItem(int id)
{
newsitem item;
// depending on how you store the news items, retrieve it here
return item;
}

// another way to create the html without having to call display,
// and then get the html is to have it do so in a single method
public string getHtml(int id)
{
GenerateNewsHTML(id);
return NewsHTML;
// You could also abandon the property alltogether
}

public string getLatestNews()
{
string latestNews = "";
// create a string out of a selection or all news items
return latestNews;
}
}

I'm sure there are better ways to do it, but this might have been my
approach.
 
E

Eric Newton

All of the other replies in mind,

the MyNews class shouldn't really know A) how to write HTML B) how to
store... these two distinct "groups of operations" should be handled by more
specialized classes that take a MyNews type as parameters:

MyNewsHtmlWriter, MyNewsDbWriter

this gives way to abstracted UI and DB storage mechanisms that will know How
to "store in a DB" vs "store in an XML file" and "Display As HTML" vs
"Display via Windows Form"

Cool, eh? Just maintain the abstraction and you'll have a lot of leverage
down the road.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top