Quantcast
Channel: Oliver C. Grant
Viewing all articles
Browse latest Browse all 25

CPP back to basics: Designing classes Notes

$
0
0

Classes should be designed for change and extension.
concise and focused on one prupose (SRP)
developed iwth extensibility in mind (open-closed, OCP)
split into smaller pieces to favor reuse (DRY), so it’s easier to change.

Use the strategy pattern for the functions in your base class code that will change.
Even better, use a template for that function.

Non-virtual Interface Idiom (NVI)
Nothing is virtual in the public part of the base class except the destructor. You have:
public:
bool write (const Thing& thing);
private:
virtual bool doWrite(const Thing& thing);

and then in the base class you can define the write function to do anything that should always happen in write():
if (thing.name().empty() { ERROR}
startTiming
bool success = doWrite(blob); // This gets defined in derived classes.

Perhaps later you can change without changing calling code by doing:
private:
virtual bool prepareWrite() = 0;
virtual bool doWrite( etc.)


Viewing all articles
Browse latest Browse all 25

Trending Articles