Is this correct OO design?
- From: cmo63126@xxxxxxxxx
- Date: 15 Apr 2005 07:02:55 -0700
I'm not sure if this is bad design or not. It seems flawed, but I'm not
sure. Is it wrong to create an instance of a class within the class
itself? Any feedback appreciated.
public class Article
{
int articleID;
string title;
//Constructor: Load Data from DataAccess Class
public Article(int articleID)
{
this.articleID = articleID;
//Set properties of Article Object with values from Database
DataAccess.GetArticleData(this, articleID);
}
public void Update()
{
/*
IS IT BAD OO DESIGN TO CREATE AN INSTANCE OF A CLASS
(OriginalArticle) WITHIN THE CLASS ITSELF(Article)?
*/
Article OriginalArticle = new Article(this.articleID);
//check to see if the new and origianal values match
if(this.title != OriginalArticle.Title)
{
Response.Write("<br>Title doesn't match current Title in
Database...Perform Important Operation Here.");
}
else
{
Response.Write("<br>Title hasn't changed.");
}
}
public string Title
{
get{return title;}
set{title = value;}
}
}
public class DataAccess
{
public static void GetArticleData(Article article, int ArticleID)
{
//load database data, populate Article object from parameter
article.Title = "Original Title from Database";
//article.Field1 = ...
//article.Field2 = ...
//etc...
}
}
public class _Default : Page
{
private void Page_Load(object sender, EventArgs e)
{
Article MyArticle = new Article(2112);
Response.Write("<br>MyArticle.Title: "+MyArticle.Title);
MyArticle.Title = "New Title from MyArticle object instance";
MyArticle.Update();
}
}
results:
MyArticle.Title: Original Title from Database
Title doesn't match current Title in Database...Perform Important
Operation Here.
.
- Follow-Ups:
- Re: Is this correct OO design?
- From: Nick Malik [Microsoft]
- Re: Is this correct OO design?
- Prev by Date: RE: Files(html) in folder permission in ASP.NET
- Next by Date: Re: Good book about VB.NET ?
- Previous by thread: Creating a user using Active Directory
- Next by thread: Re: Is this correct OO design?
- Index(es):
Relevant Pages
|