473,399 Members | 3,302 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,399 software developers and data experts.

factory method

hello, im trying to solve my problem using factory method but im not
sure if im doing it correctly this is my sample code

class Base {
public:
Base();
virtual ~Base;
virtual void draw();
};

class Item1 : public Base {
public:
Item1();
~Item1();
void draw();
};

class Item2 : public Base {
public:
Item2();
~Item2();
void draw();
};

// factory method ?!
Base * getItem(int code)
{
if (code == 1 || code == 2)
return new Item1();
else if (code == 3 || code == 4)
return new Item2();
else
return 0;
}

Base * item = getItem(code);
if (item)
item->draw();
delete item;

the sad part is, "code" should have atleast 50 possible values and i
have atleast 8 classes derived from a single base class. am i doing
this right?! any suggestions would be appreciated, thanks...

Apr 6 '06 #1
3 3139
Well if you have 8 different items to draw then you have to impilement
8 different classes. If there are some common things you are doing in
those 8 classes then you can write some utility class and use it.

Also you can improve the getItem() like this:

Base * getItem(int code)
{
Base* newInstance = 0;
seitch( code )
{
case 1:
case 2: //You may add some more casese for which u have
return Item1.
newInstance = new Item1();
break;

case 3:
case 4: //Same here as above comment.
newInstance = new Item2();
break;

default:
newInstance = 0;
break:
}

return newInstance;
}

Apr 6 '06 #2
ji*********@gmail.com wrote:
hello, im trying to solve my problem using factory method but im not
sure if im doing it correctly this is my sample code

class Base {
public:
Base();
virtual ~Base;
virtual void draw();
};

class Item1 : public Base {
public:
Item1();
~Item1();
void draw();
};

class Item2 : public Base {
public:
Item2();
~Item2();
void draw();
};

// factory method ?!
Base * getItem(int code)
{
if (code == 1 || code == 2)
return new Item1();
else if (code == 3 || code == 4)
return new Item2();
else
return 0;
}

Base * item = getItem(code);
if (item)
item->draw();
delete item;

the sad part is, "code" should have atleast 50 possible values and i
have atleast 8 classes derived from a single base class. am i doing
this right?! any suggestions would be appreciated, thanks...


You could replace the if/else cascase with switch/case, wich makes it a bit
more readable IMHO:

switch (Code)
{
case 1:
case 2:
return new Item1();
case 3:
case 4:
return new Item2();
default:
return 0;
}

Generally, you should use an enum or constants instead of magic numbers.
Another possibility is to make the classes register with the factory. Then
you can write your program in such a way that the factory doesn't need to
know which derived classes exist.

Apr 6 '06 #3

ji*********@gmail.com wrote:
hello, im trying to solve my problem using factory method but im not
sure if im doing it correctly this is my sample code

class Base {
public:
Base();
virtual ~Base;
virtual void draw();
};

class Item1 : public Base {
public:
Item1();
~Item1();
void draw();
};

class Item2 : public Base {
public:
Item2();
~Item2();
void draw();
};

// factory method ?!
Base * getItem(int code)
{
if (code == 1 || code == 2)
return new Item1();
else if (code == 3 || code == 4)
return new Item2();
else
return 0;
}

Base * item = getItem(code);
if (item)
item->draw();
delete item;

the sad part is, "code" should have atleast 50 possible values and i
have atleast 8 classes derived from a single base class. am i doing
this right?! any suggestions would be appreciated, thanks...


I'm wrestiling with something similar to what you're doing so I'm
curious to see how the more seasoned veterans here respond. Implement
a 'Collection' - if you will class. So now:

#include <iostream>
#include <functional>
#include <algorithm>
#include <cassert>
# include <vector>

using namespace std;
class Base;
class Item1;
class Item2;

class ItemInteface
{
public:
virtual void draw( Base& ) { assert(false && "Not implemented."); }

virtual void draw( Item1& ) { assert(false && "Not implemented"); }

virtual void draw( Item2& ) { assert(false && "Not implemented"); }

virtual ~ItemInteface();
} ;

class Base {
public:
Base();
virtual ~Base();
virtual void draw(ItemInteface& i ) { i.draw (*this); }

};
class Item1 : public Base {
public:
Item1();
~Item1();
virtual void draw(ItemInteface& i ) { i.draw (*this); }
};

class Item2 : public Base {
public:
Item2();
~Item2();
virtual void draw(ItemInteface& i ) { i.draw (*this); }
};

#if 0
// factory method ?!
Base * getItem(int code)
{
if (code == 1 || code == 2)
return new Item1();
else if (code == 3 || code == 4)
return new Item2();
else
return 0;
}
#endif

template<class T>
class Collection : public Base
{
vector<T*> t;
public:
Collection()
{
// determine some way to store base objects .. in t. this is 'a
hack' but
// hopefully illustrates the point
for (size_t i = 0; i < 2; ++i)
t.push_back( new T );
}
virtual void invoke( ItemInteface& toDraw ) // just invoke ..
{
cout << "Collection invoked.\n";
for (size_t i = 0; i < t.size(); ++i)
t[i]->draw( toDraw );
}
};
So you have a Collection of 'items' if you will.
An aside:
The difference between what you've alluded to and what I'm trying to do
surrounds the fact that each of my _items_ takes different arguments.
This complicates my 'draw' function ( in mine it's called 'process' )
so I'm stuck.

Apr 6 '06 #4

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...
4
by: Eric | last post by:
Perhaps this question has been posed before (I'd be surprised if it hasn't) but I just gotta know... Is it possible to combine the Singleton and Factory Method design patterns in the same class?...
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...
4
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...
10
by: Mark | last post by:
I have an abstract class, and a set of classes that inherit from my abstract class. The fact that it is abstract is likely irrelevant. I have a static factory method in my abstract class that...
8
by: googlegroups | last post by:
Hi, I need to parse a binary file produced by an embedded system, whose content consists in a set of events laid-out like this: <event 1<data 1<event 2<data 2... <event n<data n> Every...
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...
8
by: Steven D'Aprano | last post by:
I'm writing a factory function that needs to use keywords in the produced function, not the factory. Here's a toy example: def factory(flag): def foo(obj, arg): if flag: # use the spam keyword...
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
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...
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
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...
0
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...
0
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.