PC Review


Reply
Thread Tools Rate Thread

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

 
 
Stig Nielsson
Guest
Posts: n/a
 
      27th Sep 2005
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(Input 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(Input input)
{
IFactoryProduct result;
switch(Input.typeField)
{
case: InputType.A:
{
result = new A();
break;
}
case: InputType.B:
{
result = new B();
break;
}
return result;
}
}
}

regards
Stig Nielsson

 
Reply With Quote
 
 
 
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      28th Sep 2005
Stig Nielsson <(E-Mail Removed)> 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 - <(E-Mail Removed)>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
Jeff Louie
Guest
Posts: n/a
 
      28th Sep 2005
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 ***
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
TransferText method -- pass parameterized query? Jeff Norville Microsoft Access 0 9th Nov 2009 03:24 PM
About Factory Method and Abstract Factory (design pattern) Duy Lam Microsoft C# .NET 2 27th Nov 2007 02:55 PM
About Factory Method and Abstract Factory (design pattern) Duy Lam Microsoft Dot NET 0 14th Nov 2007 12:29 PM
Passing multiple values using IN operator through parameterized qu =?Utf-8?B?RWxpemFiZXRo?= Microsoft Access Queries 1 1st Oct 2004 05:15 AM
ADO.NET Parameterized Queries - Better Performance ? Vikram Microsoft ADO .NET 5 22nd Dec 2003 11:12 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:38 AM.