views:

93

answers:

8

Hi,

I must structure new model for application and I don't what is better:

using inheritance or using enum as type of object:

For example :

Books

class Book
{
public string Name {get;set;}

public string Author {get;set;}

public int NumberOfPages {get;set;}

}

public class Encyclopedie:Book
{

}

public class Novel:Book
{

}

or better use:

class Book
{

public BookType Type {get;set;}

public string Name {get;set;}

public string Author {get;set;}

public int NumberOfPages {get;set;}

}

public enum BookType
{
Encyclopedie = 0,
Novel = 1,
...
}
+14  A: 

Use inheritance if the different types have significant differences (how you process them and treat them). That is, if you are going to use polymorphism at all, you should use inheritance.

If you only need a way to distinguish different types of books, go with the Enum.

Oded
Absolutely agree. I was thinking about how to express "significant differences" just now. Poor English :(
Danny Chen
Agree. Seems he only want's to see it as different types. Inheritance makes a strong coupling, and what happens when you have a book that crosses genres? Switch to c++ to allow multiple inheritance? A [Flag]'ed enum might be a good choice here.
simendsjo
+1 for a very clear answer. The only right answer IMHO.
Cloud
A: 

It really depends. The first solution is better if you need to use Polymorphism. From my personal opinion, I prefer using inheritance.

Danny Chen
+1  A: 

I would say that the 2nd would be better as you are not really extending the book class in your Encyclopaedia, there are no additional properties or functionality you need to give one book type over another.

Phill Duffy
+2  A: 

"best" is subjective and heavily depending on the purpose of the class / model. What is your goal? What do you want to achieve?

At best at this point I can say is, inheritance is useful when the derived class has some fairly unique properties - like Encyclopedie has properties explaining which type of Encyclopedie it actually is and those properties do not, in any way, belong to a novel.

riffnl
A: 

If the different types of books will have different attributes you should definately use an inheritance model. This also allows for polymorphism, which is usually better

If they will all have the same attributes, you might as well go with the enum. But it all depends on the application.

Nubsis
A: 

Hi phenevo,

using an enum to "type" your object sounds a bit of "old C-style" programming.

I mean, it is ok, but when inheritance is available (you're using C#) it is generally a better choice. An enum generally introduces some "trouble" for example when serializing/deserializing data: what if an old version of your application uses a "newer" scenario in which a BookType has an unknown item? (backward/forward compatibility could be a requirement for your app)

Of course, you can handle this with a bounch of "if-then-else" but inheritance seems a cleaner choice in my point of view.

Bye!

Ampelio Attanasi
How would you distinguish between inherited classes when deserializing data? The only way I see is to include the class name with each instance of serialized object(which you can also do by calling tosting() on an enum)
apoorv020
An XmlSerializer returns typed data - sure you can use an enum and switch on it, but why do that when the "type" already offers the same thing?Also: if you just use a single class and an "enum" to type it, you'll have to put *all* the properties needed by the various types in that class. You'll probably have a property "XYZ" needed by type "BookAAA" but not by type "NovelBBB" - and this is not so good, IMHO, when you can avoid this by inheritance.
Ampelio Attanasi
A: 

I think you should make this choice based on what the book "purpose" is. If the books don't need any additional stuff (methods and properties....) enum should be enough. If you have to create a common behavior for every book and something else more specific for each book type you obviously need inheritance (abstract class "book", and concrete classes).

astorcas
+1  A: 

In true object oriented systems, the type of the object is transparent to the client. So the code which handles books should not know what the type of the book is, but only invoke methods on books.

So if you need to implement different behaviour within the book in response to the method invocation, extend Book and override some of its methods. If you don't, then don't.

It appears, given the empty bodies of your subclasses, that they behave in every way the same as books. So you are merely tagging the book with some additional data - the difference between Encyclopaedia and Novel is no more essential to the book than hardback or softback or large print or standard print - a client may use these differently, and each book either is a large print book or it is a standard print book, but these are all attributes of the book rather than essential differences.

I wouldn't necessary use an enum for the book kind, since you may want to add more data - I'd either use a loose tagging system, so you can tag a book with a collection of kinds - so you would have a book tagged as { 'children's', 'ornithological', 'encyclopaedia', } - or allow structure in the roles - so there is a role for 'children's ornithological encyclopaedia' created when it is required, but no fixed enumeration.

Pete Kirkham