Object modelling problem

好久不见. 提交于 2019-12-11 06:44:24

问题


I have got class Car, and each class has list of extras (leather, abs, Folding mirrors etc...)

public Extra
{
public bool Paid {get;set;}
public string Name {get;set;}
public string Code {get;set;}
}

And now, better is:

class Car
{
public Extra Leather {get;set;}
public Extra FoldingMirrors {get;set;}
public Extra Abs {get;set;}
}

or better:

class Car
{
private List<Extra> _listOfExtras=new List<Extra>

public List<Extra> ListOfExtras
{
...
}
}

And the worst part:

My winforms application works offline and one per month is sending datas for me.

I must hold list Of available (foreach car user choose extras) extras in this application so better way I think is option one.

how hold this list of extras in my applications ? It connect with webservice only one time per month.


回答1:


A list or dictionary (for easier access) of extras would be more flexible.

It would mean that as extras were added and/or removed from a car you wouldn't have to modify the Car class which you would have to do if each were added explicitly.

There are various ways to store the list locally. You could serialise to a file - either plain text or XML which would be better as you've got classes that do the serialisation for you, or you could hold it in a local database.




回答2:


Personally (seeing this is subjective), I would use an interface for Extra (eg IExtra). I would also go for the second 'option' of having a list/dictionary of extras contained in the Car class.

If needed, they can be exposed like in option 1, by just looking up the value in the list/dictionary.




回答3:


I would store it in a Dictionary<string, Extra>. You don't want the same extras showing up more than once... Or at least paying for them more than once :-)

Also, Extra should be an interface (or at the very least an abstract class)..

As for storage, you could serialize the Car class out to XML.



来源:https://stackoverflow.com/questions/3084125/object-modelling-problem

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