473,793 Members | 2,742 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Best performance of a parameterized factory method - 'is' operator or an enumeration ?

I am wondering what is the most efficient way to make a parameterized
factory method, and before I start spending time on performance
measurements myself, I would like to hear the NG's opinion:

I want my parameterized factory method to create either an object of
type A or an object of type B, depending on the type of the Input
parameter in the factory method. The type can be tested using the 'is'
operator, but alternatively, the input could have an enumerated type
field, which can be tested in a switch statement.

The 'is' operator approach is easist to do, but my guess is that using
the enum approach cannot be less performant than the 'is' operator
approach (but it uses more memory). Are there any significant
performance differences between the two examples below ? (Assume that
it not is an option to implement the creational code in the derived
input classes which would be the best OO way to it):

// 1)
// 'is' operator approach
class A : IFactoryProduct {...}
class B : IFactoryProduct {...}

class Input {...}
class Ainput : Input {...}
class Binput : Input {...}

class SomeClass
{
public IFactoryProduct FactoryMethod(I nput input)
{
if(input is Ainput) return new A();
if(input is Binput) return new B();
}
}

// 2)
// Approach using an enumeration.
// The Input class now has a InputType field.

class A : IFactoryProduct {...}
class B : IFactoryProduct {...}

enum InputType { A, B};
class Input { ... public InputType typeField; ... }
class Ainput : Input {...}
class Binput : Input {...}

class SomeClass
{
public IFactoryProduct FactoryMethod(I nput input)
{
IFactoryProduct result;
switch(Input.ty peField)
{
case: InputType.A:
{
result = new A();
break;
}
case: InputType.B:
{
result = new B();
break;
}
return result;
}
}
}

regards
Stig Nielsson

Nov 17 '05 #1
2 1716
Stig Nielsson <st****@gmail.c om> wrote:
I am wondering what is the most efficient way to make a parameterized
factory method, and before I start spending time on performance
measurements myself, I would like to hear the NG's opinion:


Well, the first thing I'd ask is: "does the performance here really
matter"? Do you have any evidence that it's a bottleneck in your
application? Unless you're calling it millions of times a minute, it's
very unlikely to matter much - so go with whichever ends up being more
elegant.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #2
Stig... As Jon suggest I don't think this is going to be a bottleneck.
The real
question is how extensible is this design. Using an enum locks you into
a set
domain of choices, but provides a level of compile time safety. If you
use an int
index you can extend the factory and add new choices at a later date by
overrriding the GetInstance(int i) method. This is the gang of four
approach. A
new approach is a type based class factory which supports run time
dynamic
behavior.

http://www.geocities.com/Jeff_Louie/OOP/oop18.htm

Regards,
Jeff

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #3

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

Similar topics

4
2981
by: Alex Vinokur | last post by:
Copying files : input to output =============================== C/C++ Performance Tests ======================= Using C/C++ Program Perfometer http://sourceforge.net/projects/cpp-perfometer http://alexvn.freeservers.com/s1/perfometer.html
2
2980
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 Intel compiler it has stopped working. I think the singleton pattern static instance thing may be at the root of the problem, but I'm not sure. I had to add calls to instance() in regCreateFn, which gets the behaviour more in line with what I was...
3
1588
by: Paramesh | last post by:
Hello friends, My friend asked me this question: This question regards proprietary software (of which I am one of the developers), so I cannot post actual code for this question. I will try to use illustrative examples. I have two libraries for distribution; one is an essential library and the other is an add-on (providing specialized extensions and
2
1585
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 http://www.dofactory.com/Patterns/PatternAbstract.aspx). Could anybody try to explain me in his own words how this pattern work and what the purpose is? thanks in advance
4
1732
by: Randy | last post by:
Compiling the following code #include <iostream> #include "cenum.h" using namespace std; class Test { CEnum MyCars("CAR", "Mustang, Nova, Pinto, Barracuda");
2
1792
by: Stephan Rose | last post by:
Still been beating on my Multi Unit Scalar structure and...it's coming along well. Since it is a core component of my app that virtually everything builds on, even the smalles amount of performance gain is worth it. But I think I have hit a wall. One million operations now average around 98ms give or take. If it's Power 0/1 (linear unit) or Power 2+ (area, volume, etc) unit makes no measurable difference. The additional loop makes...
4
2899
by: =?Utf-8?B?bW9mbGFoZXJ0eQ==?= | last post by:
In VB6, we created a number of ActiveX DLLs that all shared a similar interface. The main application would load these in dynamically (late-bound.) This worked well for our situation because we could update the DLLs independently of the main EXE, and performance was not a problem. We want to do the same thing in C# (at least test it), but we are not exactly clear what the best practice is. We would prefer to do something similar, but we...
3
1777
by: Stephen Torri | last post by:
Here is my attempt at implementing a object factory. The purpose of this is to replace a large switch statement in a factory class with the functors. I get an error at line 88, marked, "expected primary-expression before ')' token". I am using Modern C++ Design chapter 8 as a guide. Stephen --------------------- #include <map>
2
2776
by: sdanda | last post by:
Hi , Do you have any idea how to improve my java class performance while selecting and inserting data into DB using JDBC Connectivity ......... This has to work for more than 8,00,000 of records ..... Can you give some performance tips if you have known 1) For this I am using oci driver ( because I m using oracle 10g) instead of thin driver 2) In that programme I m using prepared statement instead of statement 3) I am...
0
9671
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10212
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10000
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
9035
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...
1
7538
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5436
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
5560
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4112
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
2
3720
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.