C# Object Design

  • Thread starter Thread starter Alan Kordy
  • Start date Start date
A

Alan Kordy

I am new to OOP and C#, and I am just a beginner to learn those stuff.

Hopefully someone can give me some guidance about object design.

Let say, I want to create a class called Book. This class will have a public
method to create new book.

public class Book {
public int New (String ISBN, String BookTitle) {
...
...
}
}

My question is, can we create this method with static modifier? Like:

public class Book {
public static int New (String ISBN, String BookTitle) {
...
...
}
}

So, if we created this method with static modifier, we don't even need to
instantiate an object when we want to create a book, as we can just use the
static method. May I know which way is the correct way when we design a
class with above siatuation? What is the pros and cons?

Thanks
 
The pro's to having a static member are that you don't have to create an instance of an object. So, using what you have below - you could run this statement:

Book.New("blah","book title");

The downside is want to keep along those lines, every single class member would need to be static as well. With that, you could really only have the equivalence of one 'instance'. If that's your intent, it would probably be easier to use the "singleton" "design pattern", which let's you create an instance of a class, but always assures there is never more than one. And if you try to create a new one, it will return you the one instance that exists.

The thing you might be missing out on, is it is many times GOOD to have multiple instances of a class. Because each instance is seperate from each other (except for static members). So, from below - you could have a Books (note: plural) class, which could base off of a collection or array and use an indexer.. and it could hold mulitiple Book (note: singular) objects. So in code you could do something like this:

string strTitle = Books["0735614229"].Title; // Books uses the ISBN as the indexer

-or-

Book objNewBook = Books.AddNew("0735614229","Applied Microsoft ..NET Framework Programming");
// Create a new Book object, and return it into a new local variable..

Just food for thought.. hth

I am new to OOP and C#, and I am just a beginner to learn those stuff.

Hopefully someone can give me some guidance about object design.

Let say, I want to create a class called Book. This class will have a public
method to create new book.

public class Book {
public int New (String ISBN, String BookTitle) {
...
...
}
}

My question is, can we create this method with static modifier? Like:

public class Book {
public static int New (String ISBN, String BookTitle) {
...
...
}
}

So, if we created this method with static modifier, we don't even need to
instantiate an object when we want to create a book, as we can just use the
static method. May I know which way is the correct way when we design a
class with above siatuation? What is the pros and cons?

Thanks
 
Back
Top