473,386 Members | 1,969 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,386 software developers and data experts.

Trouble with factory pattern

Hi,

I'd like to have a function factory that returns objects of a class hierarchy.
What's the best way to deal with the fact that different subclasses will have
different constructor arguments?

How do you keep the factory interface as clean as possible? O:-)

TIA
Jul 19 '05 #1
6 5456
On Thu, 16 Oct 2003 16:22:04 +0200, Boogie El Aceitoso <fr****@telefonica.net> wrote:
I'd like to have a function factory that returns objects of a class hierarchy.
What's the best way to deal with the fact that different subclasses will have
different constructor arguments?
If different subclasses _require_ different constructor arguments this
should be reflected in the factory functions' argument lists. It's not
a C++ question but a design question. If there is a requirement then the
client code will have to supply those arguments, in one form or another.

How do you keep the factory interface as clean as possible? O:-)


That depends on how you're planning to use that, as well as personal
preference (what _you_, and/or your coworkers, regard as "clean").

Jul 19 '05 #2

"Boogie El Aceitoso" <fr****@telefonica.net> wrote in message
news:l4********************************@4ax.com...
Hi,

I'd like to have a function factory that returns objects of a class hierarchy. What's the best way to deal with the fact that different subclasses will have different constructor arguments?


Well, you either have to supply other constructors for the different
subclasses, or provide default parameters. Of course, you can also have
class hierarchy in your factory design, and have different subclasses of
factories, and create the correct one at startup.
Jul 19 '05 #3
On Thu, 16 Oct 2003 11:10:10 -0400, "jeffc" <no****@nowhere.com> wrote:

"Boogie El Aceitoso" <fr****@telefonica.net> wrote in message
news:l4********************************@4ax.com.. .
Hi,

I'd like to have a function factory that returns objects of a class

hierarchy.
What's the best way to deal with the fact that different subclasses will

have
different constructor arguments?


Well, you either have to supply other constructors for the different
subclasses, or provide default parameters.


It's a hierarchy of classes with an interface similar to std::cout. They print
text on different GUI widgets and each subclass must receive, in it's
constructor, a pointer to the corresponding widget.

I'd like to have a factory function that returns an std::list with objects of
different subclasses.

What's the best way to implement such design? O:-) How can I avoid cluttering
the factory interface?

TIA
Jul 19 '05 #4
WW
Boogie El Aceitoso wrote:
It's a hierarchy of classes with an interface similar to std::cout.
They print text on different GUI widgets and each subclass must
receive, in it's constructor, a pointer to the corresponding widget.

I'd like to have a factory function that returns an std::list with
objects of different subclasses.

What's the best way to implement such design? O:-) How can I avoid
cluttering the factory interface?


A - make your widgets be inherited from the same base

B- use the prototype pattern

Just two fast ideas... they may not be applicable to your situation.

--
WW aka Attila
Jul 19 '05 #5
Responding to El Aceitoso...
I'd like to have a function factory that returns objects of a class hierarchy.
What's the best way to deal with the fact that different subclasses will have
different constructor arguments?

How do you keep the factory interface as clean as possible? O:-)


If the subclasses have different arguments (as opposed to argument
values), then Factory is not going to work well because it depends upon
inclusion polymorphism, which requires a common (abstract) interface.

To deal with your sort of situation the answer lies in parametric
polymorphism. Basically, the <concrete> Factory itself goes and gets
the data that it needs by navigating back to the Context object or to
some sort of specification object. It then uses that data to do the
right thing.

The former is most useful when the Context has all the necessary data,
but only some of it is needed for each constructor. Use an Abstract
Factory pattern and instantiate the relationship between Context and the
relevant concrete Factory. The concrete Factory navigates back to the
Context to get what it needs and then invokes the right constructor.
(The same thing can be done if the necessary data resides in other
objects so long as they can be unambiguously reached via relationships
through the Context object.)

The specification object is useful when the decision data is not readily
available as Context attributes. For example, the subclass to create
may be dictated by an XML string from an external configuration file.
Typically the external configuration data will be placed in a
specification object and a relationship between it and the Factory will
be instantiated along with that to the Context.

A common trick in this situation is to have a flock of private
construction methods in the Factory and a jump table to them that is
indexed by some identity attribute in the specification object. The
private method then parses the rest of the specification data and
invokes the right constructor.


*************
There is nothing wrong with me that could
not be cured by a capful of Drano.

H. S. Lahman
hs*@pathfindermda.com
Pathfinder Solutions -- Put MDA to Work
http://www.pathfindersol.com
(888)-OOA-PATH


Jul 19 '05 #6
Boogie El Aceitoso <fr****@telefonica.net> might (or might not) have
written this on (or about) Thu, 16 Oct 2003 16:22:04 +0200, :
Hi,

I'd like to have a function factory that returns objects of a class hierarchy.
What's the best way to deal with the fact that different subclasses will have
different constructor arguments?

How do you keep the factory interface as clean as possible? O:-)


1. Pass some prototype objects to the factory. The factory builds new
objects by cloning the prototypes.

2. Pass the default values for the constructor arguments to the
factory.

3. Create an interface that has primitive 'make' functions for each of
the objects that the factory builds. Pass appropriate derivatives of
this interface to the factory. The factory will use these methods to
build the primitive objects and assemble them.

....
Robert C. Martin | "Uncle Bob"
Object Mentor Inc. | unclebob @ objectmentor . com
501 N. Riverside Dr.| Tel: (800) 338-6716
Suite 206 | Fax: (847) 775-8174 | www.objectmentor.com
| | www.XProgramming.com
Gurnee, IL, | Training and Mentoring | www.junit.org
60031 | OO, XP, Agile, C++, Java, C# | http://fitnesse.org
Jul 19 '05 #7

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

Similar topics

17
by: Medi Montaseri | last post by:
Hi, Given a collection of similar but not exact entities (or products) Toyota, Ford, Buick, etc; I am contemplating using the Abstraction pattern to provide a common interface to these products....
2
by: Ryan Mitchley | last post by:
Hi all I have code for an object factory, heavily based on an article by Jim Hyslop (although I've made minor modifications). The factory was working fine using g++, but since switching to the...
10
by: Chris Croughton | last post by:
What do people call their factory functions? 'new' is not an option (yes, it can be overloaded but has to return void*). The context is: class MyClass { public: // Factory functions...
2
by: max | last post by:
Hello, I analyze this design pattern for a long time but I do not understand how this pattern work and what the purpose is? (I looked a this site...
1
by: =?Utf-8?B?RXJpYw==?= | last post by:
I am using the factory method to solve a problem where a factory can produce product. I have a base factory class and a base product class. The problem that I am having is that for every product...
16
by: RSH | last post by:
Hi, I am a fairly seasoned developer and have recently started looking at software design patterns. I am starting with the Abstract Factory Pattern and was wondering if someone could help me...
2
by: Duy Lam | last post by:
Hi everyone, Sorry, I don't know what group to post this problem, I think may be this group is suitable. I'm styduing DAO (Data Access Object) pattern in this link...
1
by: DAXU | last post by:
Hi, I am bit confused about the differences between plugin pattern and factory pattern. Can someone explain the differences? Many Thanks Jerry
5
by: CSharper | last post by:
I have created a Factory pattern code. One thing I noticed after coding, each factory has some methods they are exactly same and doing the same code using the values specific to each factory, if I...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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.