Simple Object Oriented Design

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • rick walsh

    #1

    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.headl ine;
    label2.Text = objmynews.date;
    label3.Text = objmynews.bodyt ext;

    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.headl ine;
    label2.Text = objmynews.date;
    mynews objmynews = new mynews(2)
    label3.Text = objmynews.headl ine;
    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



  • Morten Wennevik

    #2
    Re: Simple Object Oriented Design

    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)newsA rray[x];
    ((Label)labelAr ray[x*3]).Text = item.headline;
    ((Label)labelAr ray[x*3 + 1]).Text = item.date;
    ((Label)labelAr ray[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/

    Comment

    • rick walsh

      #3
      Re: Simple Object Oriented Design

      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)
      {
      GenerateNewsHTM L(id)
      }

      public string NewsHTML
      {
      get { return strNewsHTML; }
      set { strNewsHTML=val ue; }
      }


      public void GenerateNewsHTM L(int id)

      {
      newsitem objnewsitem = new newsitem (id)
      NewsHTML = objnewsitem.hea dline + "<br>"+ objnewsitem.dat e + "<br>" +
      objnewsitem.bod ytext;
      }

      }


      The page would then have the following

      createHTML objdisplay = new createHTML(1)
      div_content.inn erHtml = objdisplay.News HTML


      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 GenerateNewsHTM L
      "Morten Wennevik" <MortenWennevik @hotmail.com> wrote in message
      news:oprx9hume1 hntkfz@localhos t...
      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)newsA rray[x];
      ((Label)labelAr ray[x*3]).Text = item.headline;
      ((Label)labelAr ray[x*3 + 1]).Text = item.date;
      ((Label)labelAr ray[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/


      Comment

      • Morten Wennevik

        #4
        Re: Simple Object Oriented Design

        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)
        {
        GenerateNewsHTM L(id);
        }

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

        // This is not supposed to be accessible to the public, use private
        private void GenerateNewsHTM L(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.hea dline + "<br>"+
        objnewsitem.dat e + "<br>" +
        objnewsitem.bod ytext;
        }

        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)
        {
        GenerateNewsHTM L(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/

        Comment

        • Eric Newton

          #5
          Re: Simple Object Oriented Design

          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:

          MyNewsHtmlWrite r, 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
          eric@cc.ensoft-software.com [remove the first "CC."]

          "rick walsh" <rick@medifile. co.uk> wrote in message
          news:OkSuXGRpDH A.2488@TK2MSFTN GP12.phx.gbl...[color=blue]
          > 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.headl ine;
          > label2.Text = objmynews.date;
          > label3.Text = objmynews.bodyt ext;
          >
          > 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.headl ine;
          > label2.Text = objmynews.date;
          > mynews objmynews = new mynews(2)
          > label3.Text = objmynews.headl ine;
          > 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
          >
          >
          >[/color]


          Comment

          Working...