Could you give some simple example?
Lets say you have some entities stored in xml file. To read and modify those files you can create a class (pseudocode):
class FileParser{
public:
List<Data> readData( string file );
void saveData( List<Data>, string out_file );
};
class FileParser{
public:
List<Data> readData( string file );
void saveData( List<Data>, string out_file );
};
To copy to clipboard, switch view to plain text mode
The only responsibility of this class is to hide all the details associated with Data storage in your files and provide an interface to access them.
Now, maybe you'll want to display some data in a ListWidget, no problem, you have method to access a list of entities stored in a file ( List<Data> readData(file) ), so you can create a method:
void ListWidget::displayData( file ){
FileParser parser;
List<Data> data = parser.readData(file);
foreach( Data d, data ){
this->displayEntity(d);
}
void ListWidget::displayData( file ){
FileParser parser;
List<Data> data = parser.readData(file);
foreach( Data d, data ){
this->displayEntity(d);
}
To copy to clipboard, switch view to plain text mode
or you can create a function that prints the data to stdout somehow.
There could be, for example, a HardwareMonitor that you use to read CPU temperature instead of file parser:
class HardMonitor{
public:
int cpuTemperature();
private:
HardwareMonitorImpl _impl; //!< this object can hide the details of implementation for different operating systems
};
class HardMonitor{
public:
int cpuTemperature();
private:
HardwareMonitorImpl _impl; //!< this object can hide the details of implementation for different operating systems
};
To copy to clipboard, switch view to plain text mode
If you want to display CPU temp in your GUI you wont escape creating a HardMonitor object (same for previous example). But the fact that details of accessing underlying data are hidden makes your application more flexible, eg. it wont hurt that much if you want to replace xml storage schema with some other data format, or provide new way to get CPU temperature. Client classes that uses FileParser or HardMonitor wont be affected by this change, as the only thing they see is the interface.
I hope this will be of any use for you.
Bookmarks