Sign in to follow this  
baker

Hi, What is a better design?

Recommended Posts

baker    158
to have my local databases defined in terms of my own class, or keep everything general but with added functions? for example myclass { String s; //a bunch of functions i define that will help me modify/update string s } #include myclass.hpp class localdata { map<int, myclass> mylocaldata; } or set up my localdata like below and make my functions that modify/update string into localdata? class localdata { map<int, string> mylocaldata; //function to string }

Share this post


Link to post
Share on other sites
Nathan Baum    1027
It's impossible to say for certain without knowing what "localdata" or "myclass" actually are.

I'm going to work on the assumption that whatever "myclass" is imposes certain rules about what the value of "s" can be. These rules are often called invariants. The methods on "myclass" guarantee that they won't leave the object in a rule-breaking state.

In this kind of situation, it will likely be much easier to ensure that these invariants are maintained if you use the "myclass" type and put methods on it, as opposed to relying upon "localdata" to uphold the invariants.

The latter is not impossible, and may even be preferable in some cases, but if, for example, you need to get a "myclass" or "string" out of the "localdata" and tote it around your program, possibly calling methods on it and changing its state along the way, you'll almost certainly find it'll be easier to avoid breaking your program -- or, at least, you'll know that you've broken it -- if the object that you're toting around knows how to handle itself.

It also means that the parts of your program which need to know about "myclass" but don't strictly need to know about "localdata" won't need to. If "localdata" is responsible for ensuring that the rules are upheld, then parts of your program which ideally wouldn't need to be dependent upon it would be, and that would make things a lot more messy if (or more likely when) you needed to change how "localdata" behaved.

Share this post


Link to post
Share on other sites
baker    158
hi nathan thanks for the answer. i think i understand what you are saying(im kinda slow in the head).


basically myclass is just a string with functions that modify and search on that string. mylocaldata was going to be a class composed of the string in different containers.

for example i will need to make 3 different containers for my string, a vector and 2 maps. each container will basically be storing the strings until a certain condition is met. for example 1 container is a history map. here i will store every copy of string that i send to another computer.

i think what i will do now is keep the string and its functions in myclass, declare my containers in mylocalclass, and in mylocalclass create functions that will update my containers.

thanks

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this