简体   繁体   中英

OOP - OOD - Which Method should be in which class?

I wrote programs in procedural language (mostly VB6) since many years. Now I am learning C#. I read a couple of books and get things done but sometimes I ask myself: Should a method be in this class or in that class or somewhere else.

Here is an example: I write a console program which scans all files in a directory tree and will set NTFS security for all files according to some rules. For each “file type” (ie invoices, emails, Excel files) I have template files with the correct security settings.

My program does the following:

  1. Read in the MyApp.exe.config where a log file should be written

  2. Start the log file

  3. Fill a list with details about the “file types” (currently hard coded)
  4. Read in the MyApp.exe.config the locations and filenames of the template files from the above list
  5. More steps

I have the classes Program with the Main method, FileWork which scans through the files, SecurityWork which sets NTFS security rights, and ApplConfig which reads and writes configuration information.

Now my question: Where should I put the method for my step 4 in above list? This concerns settings for the SecurityWork class which are read via the ApplConfig class. Should the method be in the SecurityWork class because it concerns the security settings, or should it be in the ApplConfig class because it reads information from the configuration file or should it be in the Program class? Technically all versions would work and I know how to write them.

I know there are OOP principles like Encapsulation and others involved. What I am looking for is not so much an answer to the above incomplete example but something like a set of questions I should ask myself to decide where methods which concern multiple classes should be.

Similar question were asked and answered before but most of the time I read answers like: you could do it like this or like that, it's more or less up to you.

What do you suggest? Maybe a list with some questions, an article or a book?

To keep this question within the rules of Stackoverflow I am also happy to read an answer to the above example with an explanation of why it should be like this and not like that.

A short addition to this question: I think in many cases it is obvious in which class a method belongs. That is what I learned from books because they use often examples which are straight forward.

The “how do I do this?” happens only in some cases in which two or more classes are involved and the method could be in any of these classes and the program would work. The question is then: How would it work best? What is the best way to do this keeping future amendments in mind.

I appreciate already all the comments and answers up to now. Thanks.

Well, it's not really a question for stackoverflow, but I don't know where it would be better to ask it.
to your example the answer is to put the method for step 4 in the ApplConfig class, and have it send the values it's reading to the SecurityWork class.

Besides Encapsulation, there's this thing that's called the Single responsibility principle , that basically states that classes (and methods) should be responsible for one thing only.
Imagine a scenario where you want the location and file names to be stored in a database rather then in the app.config. By keeping the single responsibility principle all you have to change is one class and everything should work as it did before.

There are a lot of things to be said about the proper way to decide in which class a method belongs, but I think that this is probably one of the easiest to explain as well as understand, and it will give you the answer at least 80% of the time.

I would ask

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM