Skip to main content
Tweeted twitter.com/StackProgrammer/status/676647735865573376
deleted 72 characters in body
Source Link
user53019
user53019

Ok, I'm writing a medium size OOP app (C++ to be specific) and I want to learn as muchapplication in C++ as I can about good OOP practise, so I wanta way to do it the "OOP style"practice OOP principles.

I have several classes in my project (surprise!), and some of them need to access run-time configuration parameters. These parameters are read from several sources during application start-up, some. Some are read from a config file in the users home-dir, some are command line arguments (argv).

Thus,So I ended up to implementcreated a class "ConfigBlock"ConfigBlock. This class reads all the parameter sources and stores it in aan appropriate data structure. Examples are path- and filenames which can be changed by user in the config file, or the --verbose CLI flag. Then, one can call GetVerboseLevel() andConfigBlock.GetVerboseLevel() in order to read this specific parameter.

FirstMy question: isIs it good practisepractice to collect all such runtime config data in one class?

Then, my classes need access to all these parameters. I can think of several ways to achieve this, but I'm not sure which one to take. A class' constructor can be a given a reference to my ConfigBlock, like

public:
    MyGreatClass(ConfigBlock &config);

Or they just include a header "CodingBlock.h" which contains a definition of my CodingBlock:

extern CodingBlock MyCodingBlock;

Then, only the classes .cpp file needs to include and use the ConfigBlock stuff. 
The .h file does not introduce this interface to the user of the class. However, the interface to ConfigBlock is still there, however, it's hidden from the .h file. Is

Is it good to hide it this way?

I want the interface to be as small as possible, but in the end, I guess every class which needs config parameters has to have a connection to my ConfigBlock. But, what isshould this connection lookinglook like?

Ok, I'm writing a medium size OOP app (C++ to be specific) and I want to learn as much as I can about good OOP practise, so I want to do it the "OOP style".

I have several classes in my project (surprise!) and some of them need to access run-time configuration parameters. These parameters are read from several sources during application start-up, some are read from a config file in the users home-dir, some are command line arguments (argv).

Thus, I ended up to implement a class "ConfigBlock". This class reads all the parameter sources and stores it in a appropriate data structure. Examples are path- and filenames which can be changed by user in the config file, or the --verbose CLI flag. Then, one can call GetVerboseLevel() and read this specific parameter.

First question: is it good practise to collect all such runtime config data in one class?

Then, my classes need access to all these parameters. I can think of several ways to achieve this, but I'm not sure which one to take. A class' constructor can be a given a reference to my ConfigBlock, like

public:
    MyGreatClass(ConfigBlock &config);

Or they just include a header "CodingBlock.h" which contains a definition of my CodingBlock:

extern CodingBlock MyCodingBlock;

Then, only the classes .cpp file needs to include and use the ConfigBlock stuff. The .h file does not introduce this interface to the user of the class. However, the interface to ConfigBlock is still there, however, it's hidden from the .h file. Is it good to hide it this way?

I want the interface to be as small as possible, but in the end, I guess every class which needs config parameters has to have a connection to my ConfigBlock. But, what is this connection looking like?

I'm writing a medium size OOP application in C++ as a way to practice OOP principles.

I have several classes in my project, and some of them need to access run-time configuration parameters. These parameters are read from several sources during application start-up. Some are read from a config file in the users home-dir, some are command line arguments (argv).

So I created a class ConfigBlock. This class reads all the parameter sources and stores it in an appropriate data structure. Examples are path- and filenames which can be changed by user in the config file, or the --verbose CLI flag. Then, one can call ConfigBlock.GetVerboseLevel() in order to read this specific parameter.

My question: Is it good practice to collect all such runtime config data in one class?

Then, my classes need access to all these parameters. I can think of several ways to achieve this, but I'm not sure which one to take. A class' constructor can be a given a reference to my ConfigBlock, like

public:
    MyGreatClass(ConfigBlock &config);

Or they just include a header "CodingBlock.h" which contains a definition of my CodingBlock:

extern CodingBlock MyCodingBlock;

Then, only the classes .cpp file needs to include and use the ConfigBlock stuff. 
The .h file does not introduce this interface to the user of the class. However, the interface to ConfigBlock is still there, however, it's hidden from the .h file.

Is it good to hide it this way?

I want the interface to be as small as possible, but in the end, I guess every class which needs config parameters has to have a connection to my ConfigBlock. But, what should this connection look like?

Source Link
lugge86
  • 445
  • 1
  • 4
  • 12

Parameter management in OOP application

Ok, I'm writing a medium size OOP app (C++ to be specific) and I want to learn as much as I can about good OOP practise, so I want to do it the "OOP style".

I have several classes in my project (surprise!) and some of them need to access run-time configuration parameters. These parameters are read from several sources during application start-up, some are read from a config file in the users home-dir, some are command line arguments (argv).

Thus, I ended up to implement a class "ConfigBlock". This class reads all the parameter sources and stores it in a appropriate data structure. Examples are path- and filenames which can be changed by user in the config file, or the --verbose CLI flag. Then, one can call GetVerboseLevel() and read this specific parameter.

First question: is it good practise to collect all such runtime config data in one class?

Then, my classes need access to all these parameters. I can think of several ways to achieve this, but I'm not sure which one to take. A class' constructor can be a given a reference to my ConfigBlock, like

public:
    MyGreatClass(ConfigBlock &config);

Or they just include a header "CodingBlock.h" which contains a definition of my CodingBlock:

extern CodingBlock MyCodingBlock;

Then, only the classes .cpp file needs to include and use the ConfigBlock stuff. The .h file does not introduce this interface to the user of the class. However, the interface to ConfigBlock is still there, however, it's hidden from the .h file. Is it good to hide it this way?

I want the interface to be as small as possible, but in the end, I guess every class which needs config parameters has to have a connection to my ConfigBlock. But, what is this connection looking like?