Was trying to implement and example code to smart pointer from link. Is it a good practice? To go with all unique pointers or should we use shared pointers in this case? How can i improve this ?
#include <iostream>
#include <string>
#include<memory>
void LOG(std::string strText)
{
std::cout << strText << std::endl;
}
class Widget {
public:
virtual void draw() = 0;
};
class WindowsBtn : public Widget
{
public:
void draw() {
LOG("Windows Button");
}
};
class WindowsMenu : public Widget
{
public:
void draw() {
LOG("Windows Menu");
}
};
class LinuxBtn : public Widget
{
public:
void draw() {
LOG("Linux Button");
}
};
class LinuxMenu : public Widget
{
public:
void draw() {
LOG("Linux Menu");
}
};
/*
class WidgetFactory
{
public:
virtual Widget* CreateButton() = 0;
virtual Widget* CreateMenu() = 0;
};
class WindowsFactory : public WidgetFactory{
public:
Widget* CreateButton() {
return (new WindowsBtn);
}
Widget* CreateMenu() {
return (new WindowsMenu);
}
};
class LinuxFactory : public WidgetFactory{
public:
Widget* CreateButton() {
return (new LinuxBtn);
}
Widget* CreateMenu() {
return (new LinuxMenu);
}
};
class Client
{
WidgetFactory * factory;
public:
Client(WidgetFactory* factory) :factory(factory)
{}
void draw(){
Widget * wd = factory->CreateButton();
wd->draw();
display1();
display2();
}
void display1()
{
Widget * w[] = {
factory->CreateButton(),
factory->CreateMenu()
};
w[0]->draw();
w[1]->draw();
}
void display2()
{
Widget * w[] = {
factory->CreateMenu(),
factory->CreateButton()
};
w[0]->draw();
w[1]->draw();
}
};
*/
class SmartWidgetFactory
{
public:
virtual std::unique_ptr<Widget> CreateButton() = 0;
virtual std::unique_ptr<Widget> CreateMenu() = 0;
};
class SmartWindowsFactory : public SmartWidgetFactory {
public:
std::unique_ptr<Widget> CreateButton() {
return std::make_unique<WindowsBtn>();
}
std::unique_ptr<Widget> CreateMenu() {
return std::make_unique<WindowsMenu>();
}
};
class SmartLinuxFactory : public SmartWidgetFactory {
public:
std::unique_ptr<Widget> CreateButton() {
return std::make_unique<LinuxBtn>();
}
std::unique_ptr<Widget> CreateMenu() {
return std::make_unique<LinuxMenu>();
}
};
class SmartClient
{
std::unique_ptr<SmartWidgetFactory> factory;
public:
SmartClient(std::unique_ptr<SmartWidgetFactory> oFactory)
{
factory = std::move(oFactory);
}
void draw() {
std::unique_ptr<Widget> wd = factory->CreateButton();
wd->draw();
display1();
display2();
}
void display1()
{
std::unique_ptr<Widget> w[] = {
factory->CreateButton(),
factory->CreateMenu()
};
w[0]->draw();
w[1]->draw();
}
void display2()
{
std::unique_ptr<Widget> w[] = {
factory->CreateMenu(),
factory->CreateButton()
};
w[0]->draw();
w[1]->draw();
}
};
int _tmain(int argc, _TCHAR* argv[])
{
/*WidgetFactory *factory;
factory = new LinuxFactory;
Client *c = new Client(factory);
c->draw();*/
std::unique_ptr< SmartWidgetFactory > oFactory = std::make_unique<SmartLinuxFactory>();
std::unique_ptr<SmartClient> oClient = std::make_unique<SmartClient>( std::move(oFactory) );
oClient->draw();
return 0;
}
SmartClient(std::unique_ptr<SmartWidgetFactory> oFactory)
used value instead of reference. \$\endgroup\$