473,388 Members | 1,385 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

constructor or factory or builder

Hi,

if I consider the Factory Method design pattern, then I should have the
following objects:

Object and ObjectFactory which are usually abstract classes

Then for each subclasses like ConcreteObject1, ConcreteObject2, .... I
must have a ConcreteObjectFactory1, ConcreteObjectFactory1, ...

What is the benefit using a Factory instead of a constructor to
instantiate a ConcreteObject ?

David
Jul 7 '06 #1
1 1723
David Bellot wrote:
Hi,

if I consider the Factory Method design pattern, then I should have the
following objects:
This is not really the factory method design pattern as defined by the
GoF. It's just using factory objects.
Object and ObjectFactory which are usually abstract classes

Then for each subclasses like ConcreteObject1, ConcreteObject2, .... I
must have a ConcreteObjectFactory1, ConcreteObjectFactory1, ...

What is the benefit using a Factory instead of a constructor to
instantiate a ConcreteObject ?
This is not really a C++ language question. You probably want to ask in
comp.object.

But to give you a common example in C++, you can use different
look-and-feels for GUI elements through a factory:

struct AbstractButton
{
virtual ~AbstractButton() {}
//...
};

struct AbstractFactory
{
virtual ~AbstractFactory() {}
virtual std::auto_ptr<AbstractButtonCreateButton() const = 0;
// ...
};

struct SquareButton : AbstractButton { /*...*/ };
struct RoundedButton : AbstractButton { /*...*/ };

struct SquareFactory : AbstractFactory
{
std::auto_ptr<AbstractButtonCreateButton() const
{
return auto_ptr<AbstractButton>( new SquareButton );
}
// ...
};

struct RoundedFactory : AbstractFactory
{
std::auto_ptr<AbstractButtonCreateButton() const
{
return auto_ptr<AbstractButton>( new RoundedButton );
}
// ...
};

struct Dialog
{
Dialog( const AbstractFactory& factory )
{
// ... create and use buttons abstractly, e.g.,
std::auto_ptr<AbstractButtonbutton = factory.CreateButton();
// ... now use AbstractButton's interface here
// and it will apply to either square or rounded buttons
// depending on which factory was passed to this constructor
}
// ...
};

For a more sophisticated implementation of this sort of thing, see
chapters 8 and 9 of _Modern C++ Design_.

Cheers! --M

Jul 7 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Devika | last post by:
hi, I understand the need of virtual destructor.but virtual constructor is not supported in c++.but basically why we need to have virtual constructor?? thx in advance Devika
10
by: Marc Selis | last post by:
Hi, I have a class with a static constructor in which the class registers itself as capable of doing something (using a delegate) The problem is that the static constructor is never called, as...
20
by: Ole Hanson | last post by:
I am accessing my database through an interface, to allow future substitution of the physical datastore - hence I would like to declare in my Interface that my DAL-objects implementing the...
1
by: Michael Carr | last post by:
Does anybody know how to inherit from a class with a private constructor? I am trying to inherit from DataRow as follows: public class MyDataRow : DataRow { private MyDataRow() { } }
8
by: Sam Kuehn | last post by:
How do I accomplish the fallowing (is it even possible). Say I write a UserControl "MyControl.ascx". Now I use LoadControl("MyControl.ascx"). But I really want MyControl to require parameters in...
16
by: plmanikandan | last post by:
Hi, I have doubts reg virtual constructor what is virtual constructor? Is c++ supports virtual constructor? Can anybody explain me about virtual constructor? Regards, Mani
40
by: Sek | last post by:
Is it appropriate to throw exception from a constructor? Thats the only way i could think of to denote the failure of constructor, wherein i am invoking couple of other classes to initialise the...
7
by: Thomas Kowalski | last post by:
Hi everyone, is there a way to enforce that every class that inherit from BASE have to implement a given constructor? Example: class Base { virtual Base (int,int) = 0; } class INHERIT :...
14
by: Dan Rumney | last post by:
I've been taking a look at Douglas Crockford's JSLint. One of the conventions that it expects is that arrays be created using literal notation var arr1 = ; as opposed to using a constructor...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.