snippetcppMinor
Family and vehicles example using the strategy pattern
Viewed 0 times
theexamplevehiclesusingstrategyandfamilypattern
Problem
I have studied the strategy pattern and understood it w.r.t the example given in the PDF that I referred. I wanted to make sure that my understanding is correct, so I have written a simple program in C++ and designed the strategy pattern using the family and vehicles as the subject. Please provide corrections/suggestions for the program.
#include
class Vehicle
{
public:
virtual void useVehicle() = 0;
};
class FordFigo : public Vehicle
{
public:
void useVehicle()
{
std::cout useVehicle();
}
};
class Father : public Family
{
public:
virtual void display()
{
std::cout display();
fam->setVehicle(new FordFigo());
fam->driveVehicle();
fam = new Daughter();
fam->display();
fam->setVehicle(new HondaActiva());
fam->driveVehicle();
return 0;
}Solution
In "modern C++" it is unusual to see (and use) RAW pointers.
Here you have a pointer to a vehicle. But there is no indication of who owns the vehicle. Does the family member own the vehicle or is a shared resource? Can a family member have no vehicle in which case what is the appropriate response to
Anyway. Don't use pointers. In terms of memory management they do not indicate who the owner of the pointed at object is; Note: it is the responsibility of the owner to delete the object. If you don't know who should delete it you have memory leaks (and there is a lot in your code).
Also by default pointers are not set to
What happens if
You should mark virtual implementations as override
Never use dynamic memory memory management when local variables will do.
class Family
{
public:
Vehicle *myVehicle;Here you have a pointer to a vehicle. But there is no indication of who owns the vehicle. Does the family member own the vehicle or is a shared resource? Can a family member have no vehicle in which case what is the appropriate response to
driveVehicle()? Maybe with no vehicle they default to walking?Anyway. Don't use pointers. In terms of memory management they do not indicate who the owner of the pointed at object is; Note: it is the responsibility of the owner to delete the object. If you don't know who should delete it you have memory leaks (and there is a lot in your code).
Also by default pointers are not set to
NULL. So if you do not explicitly initialize them in a constructor you have a random value (so initialize them).What happens if
myVehicle is NULL here?void driveVehicle()
{
myVehicle->useVehicle();
}You should mark virtual implementations as override
void display() override // indicates this is the implementation
// of a virtual function.
{
std::cout << "I am Daughter" << std::endl;
}Never use dynamic memory memory management when local variables will do.
Family *fam = new Father();
// This should be:
Father father;Code Snippets
class Family
{
public:
Vehicle *myVehicle;void driveVehicle()
{
myVehicle->useVehicle();
}void display() override // indicates this is the implementation
// of a virtual function.
{
std::cout << "I am Daughter" << std::endl;
}Family *fam = new Father();
// This should be:
Father father;Context
StackExchange Code Review Q#75471, answer score: 2
Revisions (0)
No revisions yet.