Commit 0ea90197 authored by Jordan P. Lefebvre's avatar Jordan P. Lefebvre
Browse files

Starting on Design Principles. Added Single Responsibility principle.

parent 15fe8f20
Pipeline #11109 passed with stage
in 3 minutes and 22 seconds
......@@ -85,9 +85,6 @@ Class members should be prefixed with **m** and be **lazyCamelCase**.
```c++
class CommandLine
{
private:
int mArgc;
char ** mArgv;
};
```
Class methods should be **lazyCamelCase**.
......@@ -95,10 +92,110 @@ Class member getter methods should be the members name **lazyCamelCase** `int a
```c++
class Commandline
{
private:
int mArgc;
public:
int argc() const;
int myMethod() const;
};
```
# Design Principles
Thoughout this section, examples of principles may not always be implemented with good programming practices. This is done for the purpose of conciseness.<br>
Please use good programming practices when performing developing software.
## Single Responsibility
The idea that a class should have a single purpose and that purpose should not change over time.
For example, you may have a class:
```c++
#include <vector>
#include <string>
using namespace std;
class StringVector
{
private:
vector<string> entries;
public:
void push_back(const string& entry)
{
entries.push_back(entry);
}
};
int main()
{
StringVector strings;
strings.push_back("Hello");
strings.push_back("World");
}
```
You may wish to save the content of this class and find yourself implementing a save method:
```c++
#include <vector>
#include <string>
#include <fstream>
using namespace std;
class StringVector
{
private:
vector<string> entries;
public:
void push_back(const string& entry)
{
entries.push_back(entry);
}
void save(const string& filename)
{
ofstream ofs(filename);
for (auto& e : entries)
ofs << e << endl;
}
};
int main()
{
StringVector strings;
strings.push_back("Hello");
strings.push_back("World");
strings.save("strings.txt");
}
```
This creates dual purpose for this class. If the persistance of this class needs to be updated, this method's change in behavior will impact downstream implementers. This creates a cascade of changes and subsequent uncertainty in the level of effort to implement this change.
The <b>Single Responsibility</b> princle dictates that the second purpose, the *save()* method would be implemented separate from *StringVector*.
```c++
#include <vector>
#include <string>
#include <fstream>
using namespace std;
class StringVector
{
private:
vector<string> entries;
public:
void push_back(const string& entry)
{
entries.push_back(entry);
}
};
class PersistenceManager
{
public:
static void save(const StringVector& entries, const string& filename)
{
ofstream ofs(filename);
for (auto& e : entries)
ofs << e << endl;
}
}
int main()
{
StringVector strings;
strings.push_back("Hello");
strings.push_back("World");
PersistenceManager::save(strings, "strings.txt");
}
```
If the method for persistence needs to be changed, another method may be implemented without impacting the existing persistence mechanism. This avoids impacting any downstream implementers of *StringVector*.
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment