Interview Question


Country: United States




Comment hidden because of low score. Click to expand.
0
of 0 vote

A simple example to explain:

class Widget
{
public:
virtual void Display() = 0;
};

class WindowsMenuItem : public Widget
{
public:
void Display()
{
cout << "Displaying Menu Item on Windows" << endl;
}
};

class WindowsEditBox : public Widget
{
public:
void Display()
{
cout << "Displaying Edit Box on Windows" << endl;
}
};

class MotifMenuItem : public Widget
{
public:
void Display()
{
cout << "Displaying Menu Item on Motif" << endl;
}
};

class MotifEditBox : public Widget
{
public:
void Display()
{
cout << "Displaying Edit Box on Motif" << endl;
}
};

class AbstractFactoryClass
{
public:
virtual Widget* CreateMenuItem() = 0;
virtual Widget* CreateEditBox() = 0;
};

class WindowsFactory : public AbstractFactoryClass
{
public:
Widget* CreateMenuItem()
{
return new WindowsMenuItem();
}

Widget* CreateEditBox()
{
return new WindowsEditBox();
}
};

class MotifFactory : public AbstractFactoryClass
{
public:
Widget* CreateMenuItem()
{
return new MotifMenuItem();
}

Widget* CreateEditBox()
{
return new MotifEditBox();
}
};

int _tmain(int argc, _TCHAR* argv[])
{
AbstractFactoryClass* factory;
Widget *editBox, *menuItem;

if( Windows )
{
factory = new WindowsFactory();
editBox = factory->CreateEditBox();
menuItem = factory->CreateMenuItem();
}
else
{
factory = new MotifFactory();
editBox = factory->CreateEditBox();
menuItem = factory->CreateMenuItem();
}

editBox->Display();
menuItem->Display();

return 0;
}

- learner123 May 13, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
2
of 2 votes

That doesn't explain anything.

- Anonymous May 13, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

The drawback of abstract factory design is that you have to have platfrom-specific code, e.g. as shown above:
if( Windows )
{
...
}

The same can be achieved by conditional compiling where the platform-specific implementation files are included. This way there is not need for special "WindowsFactory". Instead you can just use Factory class implemented for each platform in a separate .cpp file.

- Eduard K. May 28, 2014 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More