472,373 Members | 2,088 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Design Problem...Factory or not

I have run into a design time problem and I look for help.
This is the crux of the design. [Representative]:
class IShape{
public:
virtual void Draw() = 0;
};

class Circle : public IShape{
private:
float _center, _radius;
public:
Circle(float a, float b):_center(a),_radius(b){}
virtual void Draw(){/*Draw it*/}
};

class Square : public IShape{
private:
float _length;
public:
Square(float a):_length(a){}
virtual void Draw(){/*Draw it*/}
};
Constraints:
1. I need to create dynamic collections of different shapes.
2. New shapes made available through plug-ins can be used.
3. New Collections can be defined at run-time.
3. Different shapes can have different constructor parameter lists.
Usage:
typedef std::vector<IShape*> ShapeCollection ;
typedef std::vector<ShapeCollection*> AllShapes;

AllShapes g_MyCollection;

/*based on client layer request create a new collection*/
g_MyCollection.push_back(new ShapeCollection);
g_MyCollection[0]->push_back(new Circle(0.0f, 1.4f));
g_MyCollection[0]->push_back(new Square(5.5f));
My questions are:
1. Should each shape register itself with a factory that supports
variable parameters in the constructor?
[e.g.
http://www.gamedev.net/reference/pro...ory/page3.asp]
2. Should all ShapeCollections register themselves in a registry?
[e.g. defineCollection(array of shapes...how do we take care of
parameters??]
Help

Thanks
Rajiv
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Jul 23 '05 #1
1 1513
Hi,

1. I need to create dynamic collections of different shapes.

Some client needs to create dynamic collections of diff shapes... Do
you know values they would use per new shape up front. No. I would
think for that reason a factory may not be your best bet - others may
differ from me, though. A factory would typically be used by a library
(not visible to the client). The client would instantiate an interface
class (not abstract). This interface class would then typically
instantiate the implementation by using an object factory (opaque to
the client). The only thing that the client needs to specify is
typically which concrete factory to instantiate (For example, Win
Factory, Unix Factory etc). Usually the make functions used to create
the factory all have the same constructor parameters. This is not the
case in your scenario.

I think you should use the virtual constructor idiom(also known as
prototyping or cloning). It works as follows:

class IShape
{
public:
virtual void draw() = 0;
virtual IShape* clone() const = 0;
virtual ~IShape(){ ; }
};

class Square : public IShape
{
public:
Square(float a):_length(a){}
virtual void draw(){/*Draw it*/}
virtual void clone() const( return new(*this); }
virtual ~Square(){ ; }

private:
float len_;
};

class AllShapeHolder
{
public:
//...//
void addShape( const IShape& );

private:
typedef std::vector<IShape*> ShapeCollection ;

ShapeCollection shapes_;
}

void AllShapeHolder::addShape( const IShape& newShape )
{
shapes_.push_back( newShape.clone() );
}

2. New shapes made available through plug-ins can be used.
- Virtual constructor...

3. New Collections can be defined at run-time.
- Virtual constructor...

4. Different shapes can have different constructor parameter lists.
- Most certainly Virtual constructor...

I know of one other idiom that you can use - don't know its name
though. I call it the CreationInterface idiom. It is based on the
fact that to create an item, you don't need to be exposed to it's
interface - creation can be delayed:

template <class T>
class CreationIF
{
T* create() const;
};

//Implementation of circle.
void SomeMethod( const CreationIF<ShapeIF>& cIF )
{
someContainer.push_back( cIF.create() );
}

//Implemenation of concrete creator... h file
class CircleCreator : public CreationIF<ShapeIF>
{
public:
CircleCreator( float center, float rad ) : center_( center ), rad_(
rad ){ ; }

//covariant return types? Circle is a ShapeIF
Circle* create() const;

private:
//...
}

//In cpp file...
Circle* CircleCreator::create() const
{
return new Circle( center_, rad_ );
}

There are cases where the above idiom can be essential. Typically,
using the above idiom the client (who knows the creation params but
does not use the rest of the interface) would not need to be exposed to
unnecessary interface).

Regards - hope this helps.

Werner
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Jul 23 '05 #2

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

Similar topics

3
by: Omer van Kloeten | last post by:
The Top Level Design: The class Base is a factory class with a twist. It uses the Assembly/Type classes to extract all types that inherit from it and add them to the list of types that inherit...
0
by: ma740988 | last post by:
I've provided a stripped down version - as best I could - to get us by. That said, I'm in a quandry with respect to a design here. Consider: // stream.h #ifndef STREAM_H #define STREAM_H #...
5
by: ma740988 | last post by:
Consider: #include "handyfactory.h" #include <iostream> struct Shape { virtual void print() const=0; };
2
by: Mike | last post by:
Hello NG, i am just learning various Design Patterns and now i am not sure, if this design is correct (Builder) or if i should use an other pattern. I have various classes (here ChildA and...
18
by: _dee | last post by:
Question about best use of interfaces: Say there's a 'Master' class that needs to implement a few interfaces: class Master : I1, I2, I3 { } The actual code already exists in smaller...
10
by: sunny | last post by:
Does this following program implement the factory design.if not what are things that i have to change in order to make this following program to be designed to factory design pattern. ...
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...
11
by: digz | last post by:
Hello, Apologies if this is the wrong group for this question. I want to design an interface , where for a custom functionality , the client writes a new class with the function implementation...
4
by: Pallav singh | last post by:
Hi , when should i select Factory Method / Prototype Design Pattern during my design phase ?? as both look similar to me Thanks in Advance Thanks Pallav
13
by: Rafe | last post by:
Hi, I am in a situation where I feel I am being forced to abandon a clean module structure in favor of a large single module. If anyone can save my sanity here I would be forever grateful. My...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...
0
by: jack2019x | last post by:
hello, Is there code or static lib for hook swapchain present? I wanna hook dxgi swapchain present for dx11 and dx9.
0
DizelArs
by: DizelArs | last post by:
Hi all) Faced with a problem, element.click() event doesn't work in Safari browser. Tried various tricks like emulating touch event through a function: let clickEvent = new Event('click', {...

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.