473,542 Members | 2,846 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Design Problem...Facto ry 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<ISh ape*> ShapeCollection ;
typedef std::vector<Sha peCollection*> AllShapes;

AllShapes g_MyCollection;

/*based on client layer request create a new collection*/
g_MyCollection. push_back(new ShapeCollection );
g_MyCollection[0]->push_back(ne w Circle(0.0f, 1.4f));
g_MyCollection[0]->push_back(ne w 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 ShapeCollection s register themselves in a registry?
[e.g. defineCollectio n(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++.m oderated. First time posters: Do this! ]

Jul 23 '05 #1
1 1566
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<ISh ape*> ShapeCollection ;

ShapeCollection shapes_;
}

void AllShapeHolder: :addShape( const IShape& newShape )
{
shapes_.push_ba ck( 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 CreationInterfa ce 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<Shap eIF>& cIF )
{
someContainer.p ush_back( cIF.create() );
}

//Implemenation of concrete creator... h file
class CircleCreator : public CreationIF<Shap eIF>
{
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++.m oderated. 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
3122
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 from it. During run time, using a static method, the class creates an instance of the derived class using the Activator class and returns it. This...
0
1112
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 # pragma warning (disable : 4786) // if using microsofts compiler ver6
5
1689
by: ma740988 | last post by:
Consider: #include "handyfactory.h" #include <iostream> struct Shape { virtual void print() const=0; };
2
2278
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 ChildB) derived from class Base. Now i want to create an object, but i don't want to know which class to instantiate.
18
1963
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 classes that each
10
2326
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. #include<iostream>
1
1732
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 subclass type I am having to create a factory subclass to handle differences in logic. They are becoming one to one. Is this ok? Should I look at...
11
3083
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 and then puts the name of the Class as a configuration parameter or something and then the main program loads the new class like a module and the...
4
4998
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
2267
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 problem is that classes in several modules share a common base class which needs to implement a factory method to return instances of these same...
0
7723
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7326
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...
0
7673
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...
0
4882
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...
0
3382
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...
0
3383
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1803
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
1
946
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
624
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...

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.