Class Design Question
-
OK, I didn't get much of a response in a previous post about program design, and I can see why - I didn't really break the problem down into small enough chunks. So I am following up with some of the same problem expressed in a (hopefully) more concise way. All I really want to do is get suggestions on Class design. Now, I have a feeling that some people will wade in with suggestions to use STL templates, which I don't have a problem with, but, because of the fairly simple nature of the program, and the lack of programming skills of people who may have to make small changes to the code, I think it might be OK to just use a simple "normal" array for example, rather than set up a whole vector implementation (not that I'm au fait even with that, having been away from programming for a good few years, I find I've forgotten everything!) So, here is what my program does (and is linked to an MFC dialog, though that's not relevant probably) User loads a set of text files A listbox is populated A file is selected and a process button clicked Processed data is displayed in simple static controls on the dialog The processing part involves open the text file (in theory should be the same format each time)* read in a few lines of header info, incl array size load in some lines of data, making up a 2D array do stuff with the 2D array data, fairly simple stuff, max size 27*27 because of my time away from programming and because I only ever learned "on the job" with tools code and updates, I never really got comfortable with setting up classes and programs from the ground up. And that's where I would like some help i.e. should I be making a "2DArray" type class that can hold the header info and data and be able to do arbitrary processing on 1D and 2D subsets of the array, or should the class be the "2DArrayfile" object, and then have a separate array class that I can call methods on? It's also important that I do some error checking at each stage, because its being used to check data which impacts clinical treatments - though in fact, the most important thing is correct calculation of the results, so if it fails to load a file properly because somethings out of whack, thats less serious than if it output the wrong value in a calculation! The things I need to do with this 2D Array: Find the centre point, which is the "100%" mark Calculate the size of the array bounded by the 50% values (interpolating between array indexes, each index refers to 1cm of physical distance) Reduce the ph
-
OK, I didn't get much of a response in a previous post about program design, and I can see why - I didn't really break the problem down into small enough chunks. So I am following up with some of the same problem expressed in a (hopefully) more concise way. All I really want to do is get suggestions on Class design. Now, I have a feeling that some people will wade in with suggestions to use STL templates, which I don't have a problem with, but, because of the fairly simple nature of the program, and the lack of programming skills of people who may have to make small changes to the code, I think it might be OK to just use a simple "normal" array for example, rather than set up a whole vector implementation (not that I'm au fait even with that, having been away from programming for a good few years, I find I've forgotten everything!) So, here is what my program does (and is linked to an MFC dialog, though that's not relevant probably) User loads a set of text files A listbox is populated A file is selected and a process button clicked Processed data is displayed in simple static controls on the dialog The processing part involves open the text file (in theory should be the same format each time)* read in a few lines of header info, incl array size load in some lines of data, making up a 2D array do stuff with the 2D array data, fairly simple stuff, max size 27*27 because of my time away from programming and because I only ever learned "on the job" with tools code and updates, I never really got comfortable with setting up classes and programs from the ground up. And that's where I would like some help i.e. should I be making a "2DArray" type class that can hold the header info and data and be able to do arbitrary processing on 1D and 2D subsets of the array, or should the class be the "2DArrayfile" object, and then have a separate array class that I can call methods on? It's also important that I do some error checking at each stage, because its being used to check data which impacts clinical treatments - though in fact, the most important thing is correct calculation of the results, so if it fails to load a file properly because somethings out of whack, thats less serious than if it output the wrong value in a calculation! The things I need to do with this 2D Array: Find the centre point, which is the "100%" mark Calculate the size of the array bounded by the 50% values (interpolating between array indexes, each index refers to 1cm of physical distance) Reduce the ph
You ask a difficult question here. In general a good class design is never easy to make. But try to think in a object-oriented way. First you should define what problem you want to solve. As i can suggest from your post, you made that already. Then try to define the classes. Start with the core of the problem. I try to explain what i do if i invent a new class design from scratch and hope that leads you in the right direction. 1. First i write the problem i like to solve on a piece of paper. I try to put the words in one simple sentence. That helps me to get a crip on the problem. 2. Then i start with the core class. In your case this would be something like C2DArray. I try to keep it as simple as possible first. What do i need the class to do: - read in a text file - store the data kept in the text file - store some header information - do some simple calculations These points already define me some member functions: - C2DArray(const char* filename) - readFile() - calculateSomething() - ... 3. After i got the first simple class i try it out with a simple application. In most cases this is a console application with a lot of printf's. 4. I put more functionality into the class. While i do this, i keep the following in mind: - If a member function gets too big and i don't understand what's going on anymore i split it in several subfunctions - If a class gets too big and i don't understand what's going on anymore i try to generalize parts of the class into a superclass In that way you will become more classes and functions on need and you don't get overwhelmed by a mass of classes from the start. About generalisation, operator overloading etc. Do this only if it is worth the effort and you need it for your application. There are so many class designs where people did operator overloading only cause it looked nice. I am not sure if this is what you needed. Hope it helps you out a bit. And i am sorry if my English isn't that good. It is not my native language. Walter
-
You ask a difficult question here. In general a good class design is never easy to make. But try to think in a object-oriented way. First you should define what problem you want to solve. As i can suggest from your post, you made that already. Then try to define the classes. Start with the core of the problem. I try to explain what i do if i invent a new class design from scratch and hope that leads you in the right direction. 1. First i write the problem i like to solve on a piece of paper. I try to put the words in one simple sentence. That helps me to get a crip on the problem. 2. Then i start with the core class. In your case this would be something like C2DArray. I try to keep it as simple as possible first. What do i need the class to do: - read in a text file - store the data kept in the text file - store some header information - do some simple calculations These points already define me some member functions: - C2DArray(const char* filename) - readFile() - calculateSomething() - ... 3. After i got the first simple class i try it out with a simple application. In most cases this is a console application with a lot of printf's. 4. I put more functionality into the class. While i do this, i keep the following in mind: - If a member function gets too big and i don't understand what's going on anymore i split it in several subfunctions - If a class gets too big and i don't understand what's going on anymore i try to generalize parts of the class into a superclass In that way you will become more classes and functions on need and you don't get overwhelmed by a mass of classes from the start. About generalisation, operator overloading etc. Do this only if it is worth the effort and you need it for your application. There are so many class designs where people did operator overloading only cause it looked nice. I am not sure if this is what you needed. Hope it helps you out a bit. And i am sorry if my English isn't that good. It is not my native language. Walter
Thanks for your help and suggestions. I'm not totally sure if a better idea might be to have an array class itself and then an arrayfile class. That way i could make a generic array class that allows me to get the required functionality but in lots of arbitrary and generalised ways, and I can just have that as a data member of the arrayfile class.
-
Thanks for your help and suggestions. I'm not totally sure if a better idea might be to have an array class itself and then an arrayfile class. That way i could make a generic array class that allows me to get the required functionality but in lots of arbitrary and generalised ways, and I can just have that as a data member of the arrayfile class.
Hehehe! It is working as intended. You are thinking in-depth about your problem domain and the design. The way you mentioned in your last post sounds good to me. Make 2 classes one for the array and another for the file reading and parsing stuff. Do a composition of both, i. e. make the array class a member of the file reading class. Keep in mind to always start simple and extend your design as needed. Walter