473,385 Members | 1,602 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Simple Object Oriented Design

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

Nov 15 '05 #1
4 1422
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.

--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Nov 15 '05 #2
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
"Morten Wennevik" <Mo************@hotmail.com> wrote in message
news:oprx9hume1hntkfz@localhost...
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.

--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Nov 15 '05 #3
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.

--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Nov 15 '05 #4
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.

--
Eric Newton
C#/ASP Application Developer
er**@cc.ensoft-software.com [remove the first "CC."]

"rick walsh" <ri**@medifile.co.uk> wrote in message
news:Ok**************@TK2MSFTNGP12.phx.gbl...
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

Nov 15 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Martin | last post by:
When was inheritance intruduced into object oriented programming? More generally, does anyone know or have any sources on when the different features were introduced into object oriented...
11
by: DrUg13 | last post by:
In java, this seems so easy. You need a new object Object test = new Object() gives me exactly what I want. could someone please help me understand the different ways to do the same thing in...
8
by: Dale | last post by:
I've searched Amazon and read probably 100 reviews but can't find what seems to be any book that is widely accepted as the definitive book on object oriented programming design and techniques. And...
4
by: scottrm | last post by:
I am fairly new to oo design and I am looking at developing an object oriented asp.net application which will be built on top of a relational database. I have read quite a bit of the theory but...
2
by: Chris Asaipillai | last post by:
Hi there Im starting a new course this Saturday. OBJECT ORIENTED DEVELOPMENT WITH VISUAL BASIC .NET and will cover the following concepts and principles. a.. Be able to build VB.NET...
4
by: Shawnk | last post by:
This post is intended to verify that true value semantics DO NOT EXIST for the Enum class (relative to boolean operations). If this is true then (thus and therefore) you can not design state...
46
by: ajba74 | last post by:
Hi fellows, I am reading some books to learn the C programming language, and sometimes I have the feeling that when somebody becomes a C expert, he must learn a more modern and object-oriented...
5
by: virtualadepts | last post by:
I have code here that explains my object oriented design model. I've been reading about other design models from what is documented on wikipedia about the key book on the subject:...
17
by: Chris M. Thomasson | last post by:
I use the following technique in all of my C++ projects; here is the example code with error checking omitted for brevity: _________________________________________________________________ /*...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.