473,785 Members | 2,789 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 ConcreteObjectF actory1, ConcreteObjectF actory1, ...

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

David
Jul 7 '06 #1
1 1748
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 ConcreteObjectF actory1, ConcreteObjectF actory1, ...

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 ~AbstractFactor y() {}
virtual std::auto_ptr<A bstractButtonCr eateButton() const = 0;
// ...
};

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

struct SquareFactory : AbstractFactory
{
std::auto_ptr<A bstractButtonCr eateButton() const
{
return auto_ptr<Abstra ctButton>( new SquareButton );
}
// ...
};

struct RoundedFactory : AbstractFactory
{
std::auto_ptr<A bstractButtonCr eateButton() const
{
return auto_ptr<Abstra ctButton>( new RoundedButton );
}
// ...
};

struct Dialog
{
Dialog( const AbstractFactory & factory )
{
// ... create and use buttons abstractly, e.g.,
std::auto_ptr<A bstractButtonbu tton = factory.CreateB utton();
// ... 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
2034
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
10698
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 no other class references it. The only way to communicate with the class is using the delegate. Here is my code:
20
4263
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 interface and accessing the datastore MUST pass in a UserToken in the constructor of the object. Is this not possible? Am I forced to add the UserToken as a property on the object instead? /Ole
1
2123
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
5249
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 the constructor for example MyContorl oMyControl = new MyContorl(employeeid). However I need to load the control at runtime so the have to call it this way LoadControl("MyControl.ascx") and I get an error that I have not supplied any parameter...
16
3759
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
36118
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 object. TIA Sek
7
9382
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 : public Base {
14
2634
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 var arr2 = new Array(); I've been a-googling, but the best reason for this that I can find is
0
9480
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10092
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9950
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8973
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6740
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4053
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2879
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.