开发者

Make this more OOPey? - good structure?

I just want advice on whether I could improve structure around a particular class which handles all disk access functions

The structure of my program is that I have a class called Disk which gets data from flatfiles and databases on a, you guessed it, hard disk drive. I have functions like

LoadTextFileToStringList, WriteStringToTextFile, DeleteLineInTextFile etc

which are kind of "generic methods"

In the same class I also have some more specific methods such as GetXFromDisk where X might be a particular field 开发者_JAVA技巧in a database table/query.

Should I separate out the generic methods from the specialised. Should I make another class which inherits the generic methods. At the moment my class is static as there is no need to have an internal state of the class.

I'm not really OOPing am I?

Thanks Thomas


If you are using only static static functions you are not really OOPing as you said. It is writing procedural code in OO language.

You should look to create classes which represent objects in your problem domain like File and TextFile. These classes should have operations like DeleteLine, WriteLIne, Load etc.

Also, in which ever language you are programming, it is likely to have a good File IO library. Try to use that in your code as much as possible. If needed just write wrappers over the library classes to provide some additional functionality.


Well, what you seem to have in your code is a Utilities class where you bundle in all the file methods.
This could indicate some design issue but IMHO it is ok, since it is common to have utility classes in OOP designs.
It haves the benefit of being able to add extra methods or modify existing ones easy since you will not have any derived classes extending the Utility class to be affected.
For example java has static methods everywhere. E.g. Collection class.
I would suggest to have the class's contructor be private and have the naming such that is obvious that this is a Utilities class.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜