Inheritance or enum

痞子三分冷 提交于 2020-01-03 08:45:12

问题


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,
...
}

回答1:


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.




回答2:


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.




回答3:


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.




回答4:


"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.




回答5:


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




回答6:


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.




回答7:


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).




回答8:


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!



来源:https://stackoverflow.com/questions/3108081/inheritance-or-enum

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!